Using the program below, identify the values of pid at lines A, B, C, and D. (Assume that the actual pids of the parent and child are 2600 and 2603, respectively.)
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
int main()
{
pid_t pid, pid1;
/* fork a child process */
pid = fork();
if (pid lt; 0) { /* error occurred */
fprintf(stderr, "Fork Failed");
return 1;
}
else if (pid == 0) { /* child process */
pid1 = getpid();
printf("child: pid = %d",pid); /* A */
printf("child: pid1 = %d",pid1); /* B */
}
else { /* parent process */
pid1 = getpid();
printf("parent: pid = %d",pid); /* C */
printf("parent: pid1 = %d",pid1); /* D */
wait(NULL);
}
return 0;
}
Hi,
here it is given that the actual pid of parent = 2600 and pid of child = 2603.
Here let us check how the program works:
Here a fork() call is used whenever a fork() is called a new process is created.
If pid is less than 0 then an error occurs and if the pid = 0 the child process starts so initially pid = 0 and in pid1 acual value of pid is get and then print statement occurs.
So here pid =0 and pid1 = 2603.So A= 0 and B= 2603.
Next the parent process takes palce .So now the pid1 value changes to the original parent id and the pid is the childs actual pid value and it is printed.
So here pid = 2603 and pid1 = 2600.So C = 2603 and D = 2600
Now the values of pids are given below:
A = 0 ,B = 2603 , C = 2603 and D= 2600
Thank you..
Get Answers For Free
Most questions answered within 1 hours.