Weird behavior of parent and child in fork

69 Views Asked by At

After the execution of the below code, the root command line does not appear automatically. If the sleep statements are reversed, then the root command line is displayed.

Why this behavior?

#include<stdio.h>
#include<unistd.h>
int main()
{
    if(fork()==0)/*creating fork*/
    {
        sleep(10);
        printf("In child.. %d.\n",getpid());
    }
    else
    {
        sleep(5);
        printf("In parent.%d ..\n",getpid());
    }
}
1

There are 1 best solutions below

0
On

I wager that the shell prompt does reappear, but before "in child" is written. I suppose the output you see is "In parent", "PROMPT#" and "In child", in that order. – Robᵩ