I wrote this program
main()
{
int pid;
pid=fork();
if(pid==0)
printf("\nI am child\n");
else
printf("\nI am parent\n");
return 0;
}
Whose output when executed is
./a.out
I am parent
I am child
When I run with the strace program, the output is [Last part]
arch_prctl(ARCH_SET_FS, 0x7fd1bbf52700) = 0
mprotect(0x7fd1bbd47000, 16384, PROT_READ) = 0
mprotect(0x7fd1bbf70000, 4096, PROT_READ) = 0
munmap(0x7fd1bbf54000, 103886) = 0
clone(child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7fd1bbf529d0) = 5109
I am child
fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 0), ...}) = 0
--- SIGCHLD (Child exited) @ 0 (0) ---
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fd1bbf6d000
write(1, "\n", 1
) = 1
write(1, "I am parent\n", 12I am parent
) = 12
exit_group(13) = ?
The output shows that parent is run first, but the strace output seems to show that child runs first, since its printed first.
What is the rule?