NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

NAME - ANSHUMAN RAHI
ROLL_NO - 1806287


ASSIGNMENT 2

1 .Write a shell script that will store all the files in different directory named as x_1, x_2,… where
the maximum size of each directory is X.

sol -:
echo "Enter total Number of cpies to want to make in different directory of a file"
read n
k=$n
echo "Enter total Number of files you want to create"
read f
while [ $n -gt 0 ]
do
mkdir temp
size=$(du -h temp | awk '{print $1}');
name=${size}_${n}
mv temp $name
n=$(( $n -1 ))
done

sizes=$size
#echo "$sizes"
while [ $f -gt 0 ]
do
echo "Enter file name and its content"
read c
gedit $c
n=$k
while [ $n -gt 0 ]
do
names=${sizes}_${n}

cp $c $names
n=$(( $n - 1 ))
done
rm $c
f=$(( $f - 1 ))

done




2. Write a shell script to extract the data from the date command, and display the result in numerical
form with 12 hour format. (if the date command gives “Thu Jan 2 14:21:54 IST 2014” then display
the result in “02/01/2014/2:21:54 PM IST”).

Sol -:

echo "Today's date is t n"

date


echo "nnDate in the format of dd/mm/year/hr:min:sec in 12 hour format is n"

date +"%d/%m/%y/%r"





Q3) Create a different process tree using fork system call .

Sol -:

#include<unistd.h>
#include<stdio.h>


int main()
{

if(fork() && fork())
fork();
printf("t child id = %d and its parent id %dn",getpid(),getppid());

}










Q4.) Write a program where a process waits for all its child processes.

Sol :-
#include<stdio.h>
#include<unistd.h>

int main(){

int n;
n = fork();
n=fork();
n=fork();

if(n == -1)
printf("Failed to create child process as kernal is busyn");
else if( n == 0)
printf("It is a child process: child is %d called by parent id is %d n",getpid(),getppid());
else
printf("It is a parent process: waiting for child id %d to complete and its id is %d
n",getpid(),getppid());

return 0;
}





5. Write a program to implement the merge sort using multiple threads.

Sol -:



#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
int *array;
int length;
struct arg
{
int start;
int end;
};

void printArray(int A[], int end)
{
int i;
for (i=0; i <= end; i++)
printf("t%d ", A[i]);
printf("n");
}
void *mergeSort(void * arg)

{

struct arg *arguments=(struct arg*)arg;
struct arg argArr[2];
if (arguments->start < arguments->end )
{
int middle=(arguments->end+arguments->start)/2;
argArr[0].start=arguments->start;
argArr[0].end=middle;
argArr[1].start=middle+1;
argArr[1].end=arguments->end;
pthread_t tids[2];

for (int i=0; i<2; i++)
{
pthread_create(&tids[i], NULL, mergeSort, (void*)&argArr[i]);
}
for (int i=0; i<2; i++)
{
pthread_join(tids[i], NULL);
}
int length=(argArr[1].end - argArr[0].start +1);
int *back=(int*)malloc(length * sizeof(int));
int i = 0, j = argArr[0].start, k = middle+1,p;
while (j <= middle && k <= argArr[1].end)
{
if (array[j] <= array[k])
{
back[i++] = array[j++];
}
else
{
back[i++] = array[k++];
}
}
if (j==middle+1)
{
while (k <= argArr[1].end)
{
back[i++] = array[k++];
}
}else
{
while (j <= middle)
{
back[i++] = array[j++];
}
}
p=0;
for (i=argArr[0].start; i<=argArr[1].end; i++) {
array[i]=back[p++];
}
}
pthread_exit(0);
}

int main(int argc, const char * argv[]) {
struct arg arguments;
length=12;
array = (int*) malloc(sizeof(int)*length);
for (int i=0; i<length; i++) {
array[i]=rand()%500;
}
arguments.start=0;
arguments.end=length;
pthread_t id;
pthread_create(&id,NULL,mergeSort,(void *)&arguments);
pthread_join(id, NULL);
printArray(array,arguments.end);
return 0;
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.