#include <unistd.h>
#include <sys/wait.h>
int main ()
{
W(A);
int child=fork();
W(B);
if (child)
wait(NULL);
W(C);
}
I think that ABBCC will be one of the outputs, but also one more of the next ones: ACCBB, ABB, ABCBC, ABCAA should be correct. W means write and W(A), W(B), W(C) mean to write A, B, C. Please help.
ACCBB
is not possible.
I'll add a subscript p for letters printed by the parent, c for the child.
The parent process prints ApBpCp in that order, and the child process prints BcCc in that order.
Ap will be printed first, because it's printed before anything is forked. The parent waits for the child to exit before printing Cp, so this has to come last. So this means the first and last characters are A
and C
.
In between this, there are no constraints on the order of execution between the two processes. So the following are possible:
BpBcCc
BcBpCc
BcCcBp
The first two look the same in output, since we can't tell the difference between Bp and Bc
So the possible results are:
ABBCC
ABCBC
This all assumes that there's no buffering that might delay the output. I.e. this is just an exercise to examine the order that statements can be executed in concurrent processes, not really about I/O.