I am a beginner and am trying to learn how to fork() and wait() functions work.
Can someone run my code and tell me what my output should be?
Right now I am getting: A B C A B C A D E
However, a buddy of mine says it should be: A B C A D E A B C
And another says it should be: A B C C D E
Because of the wait() functions, I thought the child processes had to finish before the parent. That is why I expect the output to end in an 'E'.
What would be some possible outputs then? I don't understand when I run it I get ABCABCADE. Shouldn't 'A' only ever be printed once for the initial child process?
#include <stdio.h>
#include <unistd.h>
#include <wait.h>
int main(void) {
int pid;
pid= fork();
if (pid == 0) {
fprintf(stdout, "A\n");
pid= fork();
if (pid==0) {
fprintf(stdout, "B\n");
pid=fork();
fprintf(stdout, "C\n");
}
else {
wait(NULL);
fprintf(stdout, "D\n");
}
}
else {
fprintf(stdout, "E\n");
wait(NULL);
}
// your code goes here
return(0);
}