Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

See In Example1, the parent process is being called first ,where as in the Example 2 the child process has been called first . I couldn't understand this behavior of fork() .Please clarify.

Example1:

main()
{ 
  int pid;
  if((pid=fork())>=0)
  {
     if(pid)
     {
       printf("parent\n");
     }
     else
     {
       printf("Child\n");
     }
  }
  else
    printf("Failed \n");

  if(pid)
    printf("Parent:After fork call\n");
  else
    printf("Child:After fork call\n");

}

O/p:-

 [home]$ ./a.out
 parent
 Parent:After fork call
 [home]$ Child
 Child:After fork call

Example 2

 main()
 {
   int pid;
   if((pid=fork())>=0)
   {
     if(pid)
     {
       printf("Parent\n");
       system("ls");
     }
     else
     {
       system("pwd");
     }
   }
   else
     printf("Failed \n");

   if(pid)
     system("ps");
   else
     printf("Child:After fork call\n");
 }

O/p:-
 Parent
 /home/neeraj/Vector/OS
 Child:After fork call
 a.out  sendsig    sendsig.c  test2.c  test3.c  test.c
 PID TTY          TIME CMD
 1943 pts/0    00:00:00 bash
 31719 pts/0    00:00:00 a.out
 31720 pts/0    00:00:00 a.out <defunct>
 31723 pts/0    00:00:00 ps
share|improve this question

closed as too localized by Keltari, Scott Chamberlain, Nifle, Tog, Diago Jan 22 '13 at 9:14

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

    
There's no parent in the output of the second example? –  Daniel Beck Jan 22 '13 at 6:21
    
See if(pid) The 6th line in the code –  E F Jan 22 '13 at 6:21
    
There's printf("parent\n"); system("ls"); in the code of Example 2, but I only see a.out sendsig sendsig.c test2.c test3.c test.c in the output. No parent. –  Daniel Beck Jan 22 '13 at 6:24
    
i edited that ,it should be ok now –  E F Jan 22 '13 at 6:31
    
If you don't force any ordering, then the ordering can come out any which way. –  David Schwartz Jan 22 '13 at 6:41

1 Answer 1

up vote 1 down vote accepted

Basically ls takes a lot longer than pwd to return (pwd is just listing a variable in memory where ls must talk to the filesystem), that is where the difference is.

This is because you are creating a new thread and both operations are happening at the same time, some operations take longer than others. When you where just writing text out you may have had the parent reliably always finish first, but now that you are doing real work whatever thread finished its work first will finish first.

share|improve this answer
    
Thank you @Scott Chamberlain –  E F Jan 22 '13 at 6:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.