I have the following code and am asked to how many times “A”, “B”, “C”, “D”, “E” will be printed
fun() {
printf("A");
fork();
printf("B");
if (fork() != 0) {
printf("C");
fork();
printf("D");
}
printf("E");
}
so it should be:
A
A
B
E
im not sure if my answer above is correct? and what the line if(fork() !=0 ) do?
1 thread prints a, 2 threads print b. each of the 2 forks, but only the 2 parents go into the if statement and print c. Each of those two fork and all 4 procs print d. Then, each of the 6 procs (two children from if-fork and 4 threads coming out of if print e.
You can’t determine the order, but the number of each letter is:
A x1
b x2
c x2
d x4
e x6