0

I was just reading an article online about fork().According to what I get,the fork() system call creates a subprocess and the program counter is updated to the next instruction and the control is passed on to the child process and then control comes back to the parent process.On exectuting the following program:

 printf("%d\n",fork());

I expected the output to be :

0 positive value

since the successful call to fork() returns 0 to the child process and process ID of child to parent,and child will run first after the call to fork(),but I was amazed to find the output as:

positive value 0

Where am I going wrong?

1

There is no defined order in which processes will execute after a fork, so either answer can occur.

  • @PhilipKendali,I read somewhere that all subprocesses must terminate before a process must wait before its children die. – user1369975 Jul 13 '15 at 17:24
  • This is nothing about processes terminating, so I don't see why that's relevant (even if I could parse the English, which I can't). – Philip Kendall Jul 13 '15 at 17:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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