hello
i am trying to figure out the output of this specific code
int main()
{
int i , parent_id , ans ;
parent_id = getpid() ;
for ( i = 1 ; i <= 5 ; i++ )
{
if ( getpid() == parent_id )
{
ans = fork() ;
wait() ;
}
if ( ans == 0 )
{
sleep(i) ;
printf ( " \n I am a child my pid is %d " , getpid() ) ;
exit(0) ;
}
}
printf ( " \n I am the father my pid is %d " , parent_id ) ;
return 0 ;
}
the output is very strange. it prints the first child first , the father second and then all the other children.
i thought exit(0) suppose to kill the child.
i thought the program will print all the children first and then the father
but it doing something different that i dont understand.