A)From the program above, identify the values of pid at lines A, B, C, and D. (A
ID: 3883538 • Letter: A
Question
A)From the program above, 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. Also assume that fork will always succeed.)
B) List all of the possible outputs (just write the A/B/C/D text; no need for pids) that could be generated by the program.
int main() ( pid t pid, pid1; /* fork a child process */ pid = fork(); if (pid 0) { /* error occurred */ terinttstdec, "Fork Failed"); return 1; else if (pid == 0) { /* child process printf('' child: Rad = %d", Rid); printf( "child: pidl %d", = pid1); /* B*/ else /*parent process/ pid! = geteid(); printf("parent : Rid-%d", pid); printf("parent : pid! =%d", wait(NULL); pid1); /* D*/Explanation / Answer
Please find the given program below :
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
pid_t pid, pid1;
pid = fork();
if (pid<0){
fprintf(stderr, "Fork failed");
return 1;
}
else if (pid==0)
{
pid1=getpid();
printf("child: pid=%d ", pid);
printf("child: pid1=%d ", pid1);
}
else
{
pid1=getpid();
printf("parent: pid=%d ", pid);
printf("parent: pid1=%d ", pid1);
wait(NULL);
}
return 0;
}
A) If the actual pid of parent and child are 2600 and 2603 respectively then by executing the program the values can be obtained as,
A = 0
B = 2603
C = 2603
D = 2600
The process is allowed to run with pid 2603 for child and 2600 for parent , At the line A the pid of child is zero and at the line B the non zero pid of child process is for parent.
B) The possible outputs generated by the program at the lines A, B, C, D are printed as ,
If the pid's are assumed to be 2600 for parent and 2603 for child then output is ,
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.