Compile and run the following code and presuming your executable is in a.out, run this command from the shell prompt:
./a.out ; echo "Parent Terminated Normally with a return value of $?"
Explain, in your own words, why you see the screen output you do and how that output relates to both the content of the program AND the nature of the shell command used to invoke it. Hint: This has everything to do with how processes “communicate” their exit statuses to other processes.
#include <stdlib.h>
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char **argv)
{ pid_t fork_returned_pid;
int count;
int return_status;
fork_returned_pid = fork();
if (fork_returned_pid < 0)
{ printf("The fork() didn't work! Terminate\n");
exit (0); }
if (fork_returned_pid != 0)
{ wait(&return_status); // This line is new
if (WIFEXITED(return_status))
printf("\nChild Terminated Normally with a return value of
%d\n",
WEXITSTATUS(return_status));
}
for (count = 0; count < 10; count++)
{ if (fork_returned_pid == 0)
printf(" child says: count = %d\n", count);
else
printf("parent says: count = %d\n", count);
}
printf("\n");
if (fork_returned_pid == 0)
exit(10);
else
exit(20);
}
In this program , we get output as
./main
child says: count = 0
child says: count = 1
child says: count = 2
child says: count = 3
child says: count = 4
child says: count = 5
child says: count = 6
child says: count = 7
child says: count = 8
child says: count = 9
Child Terminated Normally with a return value of 10
parent says: count = 0
parent says: count = 1
parent says: count = 2
parent says: count = 3
parent says: count = 4
parent says: count = 5
parent says: count = 6
parent says: count = 7
parent says: count = 8
parent says: count = 9
/* We get output as given above as in this program we are using fork system call which creates one child process . So there are two processes ,one is parent and other is child process. Child process execcutes the code after the statement fork system call. How we can distinguish between child and parent is using the return value .When we call fork system call.In this program variable fork_returned_pid . If fork_returned_pid is equal to zero then it's child process and if it is greater than zero (in parent process , fork_returned_pid value will be PID of child process) then it's parent process. At this point we don't know which process is given time slice to execute the code. Suppose say parent process got time slot to get executed, in parent process , we have wait system call called as first statemt after fork . Parent process waits until it's child process exits or signal is recieved. So child process starts executing and it prints the message
child says: count = 0 , these messages are printed till child says: count = 9 , after that chld exists and parent process receives this status and starts executing from where it has left and we see the message from parent process as
parent says: count = 0
parent says: count = 1
parent says: count = 2
|
|
so on till parent says: count = 9
Hope it helps !!
Get Answers For Free
Most questions answered within 1 hours.