I just found out about fork, and as I understand it, a child process starts execution from a fork call (would fork be recursive otherwise?).
However, in this code ( ideone link ):
int main() {
printf("%d: Common code1\n", getpid());
if (fork() != 0) {
printf("%d: Parent code\n", getpid());
} else {
printf("%d: Child code\n", getpid());
}
printf("%d: Common code\n", getpid());
}
Conclusion:
27380: Common code1
27380: Parent code
27380: Common code
27380: Common code1
27383: Child code
27383: Common code
I do not understand why the 4th line is printed? I could understand if it was printed from a child process and the main fork, but it was printed from the parent, and fork does not name main.
source
share