Question: UNIX/C review code and write a description: /* parent1.c */ #include #include #include int main(int argc, char * argv[]){ if(fork()==0){ char *cmd[] = { mySpell,

UNIX/C review code and write a description:

/* parent1.c */ #include #include #include int main(int argc, char * argv[]){ if(fork()==0){ char *cmd[] = { "mySpell", argv[1], NULL }; //How many parameters do you see? if(argc==1) execve("child", NULL, NULL); else if(argc==2) execve("mySpell", cmd, NULL); else exit(1); exit(0); } printf("Process[%d]: Parent in execution... ", getpid()); sleep(2); 42 if(wait(NULL)> 0) printf("Process[%d]: Parent detects terminating child ", getpid()); printf("Process[%d]: parent terminating... ", getpid()); } Below is the original child.c file. % more child.c /* parent1.c */ #include #include #include int main(int argc, char * argv[]){ if(fork()==0){ char *cmd[] = { "mySpell", argv[1], NULL }; //How many parameters do you see? if(argc==1) execve("child", NULL, NULL); else if(argc==2) execve("mySpell", cmd, NULL); else exit(1); exit(0); } printf("Process[%d]: Parent in execution... ", getpid()); sleep(2); 42 if(wait(NULL)> 0) printf("Process[%d]: Parent detects terminating child ", getpid()); printf("Process[%d]: parent terminating... ", getpid()); }

Below is the original child.c file.

% more child.c #include int main(){ printf("Process[%d]: child in execution... ", getpid()); sleep(2); printf("Process[%d]: child terminating... ", getpid()); } We now have the mySpell script that we saw earlier. % more mySpell #!/bin/sh # An improved spelling checker file=$1 for word in spell $file do # grep tries to find the line where $word occurs in $file line=grep -n $word $file #print out a line echo " " #print out the following line echo "Missplled word: $word" #print out the line where the misspelled word occurs echo "$line" done

We compile the child.c into an executable child, % cc child.c -o child then compile the parent1.c program. % cc parent1.c If we call it without giving it any parameter, thus argc=1, the executable of parent1.c executes the child program.

=====================================================================

Questions:

1. Play with the programs as shown in this section. You may change some of the stuff, such as the time that the child process waits, to see if there is any change of the printout. 2. Find out the general format, and discuss the various usage, of those strange functions, particularly, the exec family, fork(), getpid(), and wait().?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!