0

I want to know how fork, wait and exit communicate with each other. What is passed in &n and exit(0) returns what to the parent process?

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(void){

    int n=5;
    fork();
    wait(&n);
    printf("%d\n", n);
    exit(0);
}

The output I am getting is

sh-4.3$ main
5
0
sh-4.3$
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
vish22
  • 21
  • 4
  • 3
    The manpages for each should give you all the details you need. – Michael Albers Mar 04 '16 at 03:31
  • I want to get the concepts clear using a program(example).It helps me to understand things better.Can you please explain? – vish22 Mar 04 '16 at 03:32
  • The `wait(2)` manpage has a very extensive example in it that includes `fork`. See http://linux.die.net/man/2/wait – Michael Albers Mar 04 '16 at 03:34
  • vish22, suggest you run it and examine the output. You'll get a lot more satisfaction if you work it out (or at least try) yourself. As it stands, this seems to indicate you've put NO effort into figuring it out. As stated, start by looking at the fork/wait doco. – paxdiablo Mar 04 '16 at 03:35
  • Is the program working, because **wait(&n);** with n=5; looks completely wrong, should be **wait(n);** whilh will make the process wait 5 milliseconds for the fork to occur. – Arif Burhan Mar 04 '16 at 03:37
  • Wrong wait, Arif. This one waits for a child process to finish and gives its return code. That little comment should also help you out, vish :-) – paxdiablo Mar 04 '16 at 03:38
  • (2) the result of fork should be tested for success, and the child process terminated, although I think **exit(0);** will do this. A program may terminate abnormally before it reaches exit(), leaving lots of processes running away using memory, and no way to reach them without using the shell to kill them. – Arif Burhan Mar 04 '16 at 03:40

2 Answers2

1

fork() creates a new process which is almost the exact copy of the one that makes the call. The newly created process is called the child. Beware that exact copy is a strong assumption! That really means that the newly created process is at the time of its creation in the exact state of its parent (this is a slightly rough assumption, but for the sake of simplicity think of it as is). Then one process makes a call to fork and then two processes return from the call! fork is a kind of cloning machine (one sheep enters, and then two exit from the machine!). Normally you should test the returned value of fork (see man for details).

Then from now two processes are running independently!

One does a call to wait, while the other does the same on its own. Effect of wait is different in both. For the original process, wait will wait for the child to terminate and then capture information about that event. The child calls wait but as it has no child wait does nothing and returns immediately. It then print the value of its variable n which as not been modified: 5. And then it exits with a value 0. So it terminates. This will permit the parent process to awake from it's wait call and capture the exit value of its child (0). It then prints it 0 and exits.

Beware that some variants of this scenario may happen, while this will not change the observable behavior, concurrent runs may run differently.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
  • Thanks for the explanation Jean.I have one doubt that why doesn't the parent process also print 5.I mean shouldn't there be 5 two times in the output? – vish22 Mar 04 '16 at 21:01
0

You can find out about those functions on man pages, fork, wait, exit.

For fork I would look up usage examples however, as it is kind of complicated to explain.

Community
  • 1
  • 1
pilkch
  • 777
  • 7
  • 17