#include <stdio.h>
#include <stdlib.h> // exit
#include <unistd.h> // fork, getpid
int main(int argc, char *argv[])
{
printf("hello world (pid:%d)\n", (int) getpid());
int ret_fork = fork();
if (ret_fork < 0) {
// fork failed; exit
fprintf(stderr, "fork failed\n");
exit(1);
} else if (ret_fork == 0) {
// child (new process)
printf("hello, I am child (pid:%d)\n", (int) getpid());
} else {
// parent goes down this path (original process)
printf("hello, I am parent of %d (pid:%d)\n",
ret_fork, (int) getpid());
}
return 0;
}
When I excute this, I got:
hello world (pid:3082)
hello, I am parent of 3083 (pid:3082)
hello, I am child (pid:3083)
Why are second row is printed faster than third row?
I want to know how it works in sequence.