int main () {
if (fork() == 0) {
if (fork() == 0) {
printf("3"); fflush(stdout);
}
else {
pid_t pid; int status;
if ((pid = wait(&status)) > 0) {
printf("4"); fflush(stdout);
}
}
}
else {
printf("2"); fflush(stdout);
exit(0);
}
printf("0"); fflush(stdout);
return 0;
}
For each of the following strings, circle whether (Y = yes) or not (N = no) this string is a possible output:
a. 32040 Y N
b. 34002 Y N
c. 30402 Y N
d. 23040 Y N
e. 40302 Y N
Program :
#include<stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include<sys/wait.h>
int main () {
if (fork() == 0) {
if (fork() == 0) {
printf("3"); fflush(stdout);
}
else {
pid_t pid; int status;
if ((pid = wait(&status)) > 0) {
printf("4"); fflush(stdout);
}
}
}
else {
printf("2"); fflush(stdout);
exit(0);
}
printf("0"); fflush(stdout);
return 0;
}
ANS :
a. 32040 (N)
b. 34002 (N)
c. 30402 (N)
d. 23040 (Y)
e. 40302 (N)
Get Answers For Free
Most questions answered within 1 hours.