Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database In Depth Relational Theory For Practitioners

Authors: C.J. Date

1st Edition

0596100124, 978-0596100124

More Books

Students also viewed these Databases questions