Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

UNIX/C review code and write a description: /* parent1.c */ #include <stdio.h> #

ID: 3716281 • Letter: U

Question

UNIX/C review code and write a description:

/* parent1.c */
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
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 <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
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 <stdio.h>
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().?

Explanation / Answer

Hi, First of let me tell you that you have written parent1.c, that's quite confusing. But no worries.

So here you have taken two file, bith of it have main function. As :
% cc parent1.c -- > would yeild the executable a.out.

And, % cc child.c -o child ----> would yeild an executable named child.

Now I should tell you about fork system call. So fork creates a new process, and start execution of child process from the next instruction. so child process will share the text segment of parent process. And process who have called the fork() will call as parent process and the api would return the pid of child proc to parent and 0 to itself. That is the way how we identify the child and parent proc.

getpid: -> return the process id of executing process.

wait() -> this is quite trikcy. let me explain you by taking example.let say a process A call wait and it have two child B and C. so wait will stuck untill B or C will get terminated.

And now ,

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);
}

this part of code will be called by parent process only. And child process will share the arguments with parent too. So depend on the number of arguments it would call child or mySpell or exit(1).

So let move one by one based on numbe rof argumnets passed.

lets simple... ./a.out -> argc =1
So child will be executed. now lets remove sleep(2) of parent so immediately child will get schedule as parent would get stuck over wait. and on chile sleep also it would not resume. resume once the child quited. and prints the pids.

Now for two arguments. who ever get schedule parent or mySpell. you can increse or remove the sleep(2). but it would wait to child done.

execve -> execute the given first argument binary like shells.

Thanks.