int main()
{
int i;
printf("hello before fork \n");
printf("i : %d\n",i);
i=fork();
printf("\n");
if(i==0)
{
printf("Child has started\n\n");
printf("child printing first time \n");
printf("getpid : %d getppid : %d \n",getpid(),getppid());
sleep(5);
printf("\nchild printing second time \n");
printf("getpid : %d getppid : %d \n",getpid(),getppid());
}
else
{
printf("parent has started\n");
printf("getpid : %d getppid : %d \n",getpid(),getppid());
printf("\n");
}
printf("Hi after fork i : %d\n",i);
return 0;
}
Output:
[04mca8@LINTEL pp]$ ./a.out
hello before fork
i : 134514088
Child has started
child printing first time
getpid : 8354 getppid : 8353
parent has started
getpid : 8353 getppid : 5656
Hi after fork i : 8354
[04mca8@LINTEL pp]$
child printing second time
getpid : 8354 getppid : 1
Hi after fork i : 0