Question 9
Suppose that we have Parent.c and Child.c as follows. They are compiled and linked to generate the Parent and Child executable. Below is Parent.c:
L1: #include <sys/wait.h>
L2: #define NULL 0
L3: int main(void)
L4: {
L5: if(fork() == 0)
L6: printf("I am child process with PID: %d
", getpid());
L7: execvp("Child", NULL);
L8: exit(0);
L9: }
L10: else
L11: {
L12: sleep(5);
L13: if(wait(NULL) > 0)
L14: printf("Process[%d]: In", getpid());
L15: printf("Process(%d]: ... In", getpid());
L16: }
Below is Child.c:
int main(void)
{
printf("Process[%d]: child in execution ...
", getpid());
sleep(1);
printf("Process[%d]: child terminating ...
", getppid());
printf("HHHH:%d
", getpid());
}
a What is the purpose of execvp() at L7?
b How many times will the "HHHH" at L20 be printed out? Please briefly explain.
c At L19, what process ID will be printed out by getppid()? Note: no need to explain.
Question 9
Suppose that we have Parent.c and Child.c as follows. They are compiled and linked to generate the Parent and Child executable. Below is Parent.c:
L1: #include <sys/wait.h>
L2: #define NULL 0
L3: int main(void)
L4: {
L5: if(fork() == 0)
L6: printf("I am child process with PID: %d
", getpid());
L7: execvp("Child", NULL);
L8: exit(0);
L9: }
L10: else
L11: {
L12: sleep(5);
L13: if(wait(NULL) > 0)
L14: printf("Process[%d]:
", getpid());
L15: printf("Process[%d]
", getpid());
L16: }
Below is Child.c:
int main(void)
{
printf("Process[%d]: child in execution
", getpid());
sleep(1);
printf("Process[%d]: child terminating
", getppid());
printf("HHHH%d
", getpid());
}
a What is the purpose of execvp() at L7?
c At L19, what process ID will be printed out by getppid()? Note: no need to explain.