Does (0) expect anything when called from both processes created from fork ()?

I'm trying to understand how fork works and I know that when you call fork () a new process is created that resumes from the same line where the heap and stack are copied into it. For the parent, fork () returns the PID of the child, and for the child it returns 0.

I came across this question: "How many processes does the following sequence create?"

fork();fork();wait(0);fork();wait(0);

      

Answer: 8, including the parent, but I don't understand if expectations (0) are expected or not. I found this related to the "wait" manual:

If at least one child process is running when the wait () call is made, the caller will block until one of its child processes exits. At this point, the caller resumes execution. If the child process is down when the wait () call is made, that wait () has no effect. That is, as if there is no wait ().

My understanding of this can be done as follows:

enter image description here

Where the leftmost branch is the parent and the black circles are where the forks are made. This makes me think that waiting (0) does nothing, because there are no children to wait, so it is simply ignored. However, I made a slight modification to the code by adding an "index" for each process.

#include <stdio.h>
int main()
{

    int index = 0;
    if (fork()==0)
    index++;
    if (fork()==0)
    index++;
    wait(0);
    if (fork()==0)
    index++;
    wait(0); 
    printf("%d\n",index);
}

      

And this prints 21312021

After commenting out wait (0), so the code is:

#include <stdio.h>
int main()
{

    int index = 0;
    if (fork()==0)
    index++;
    if (fork()==0)
    index++;
    if (fork()==0)
    index++;
    printf("%d\n",index);
}

      

It prints 0112223 , so something is clearly different. Why are the results different?

+3


source to share


2 answers


I don't see your image, but here's one that should accurately reflect the runtime behavior of this program. Adding explicit calls exit(0)

helps a little with understanding:

fork() ----------------------------,
  |                                |
fork() -----,                    fork() -------,
  |         |                      |           |
  |       wait(0)                  |         wait(0)
  |         |                      |           |
  |       fork()------,            |         fork() ------, 
  |         |         |            |           |          |
  |         |       wait(0)        |           |        wait(0)
  |         |         |            |           |          |
  |         |       exit(0)        |           |        exit(0)
  |         |         |            |           |          | 
  |       wait(0) <---´            |         wait(0) <----´
  |         |                      |           |
  |       exit(0)                  |         exit(0)
  |         |                      |           |
  |         |                    wait(0) <-----´
  |         |                      |
  |         |                    fork() -------,
  |         |                      |           |
  |         |                      |         wait(0)
  |         |                      |           |
  |         |                      |         exit(0)
  |         |                      |           |
  |         |                    wait(0) <-----´
  |         |                      |
  |         |                    exit(0)
  |         |                      |
wait(0) <---+----------------------´
  |
fork()------,
  |         |
  |       wait(0)
  |         |
  |       exit(0)
  |         |
wait(0) <---´
  |
exit(0)

      



here each child process is drawn to the right of the parent process. Everyone wait(0)

will wait for those lines that arose from fork()

above.

It is impossible to show one thing for sure: the first process with its first wait()

is in a position where 2 child processes are running, so it will wait for which ends first . +

in my picture it should be understood as either (or I can't think of a better way to show it graphically).

+2


source


wait(0)

will block the parent process until any of its children have finished and there are children waiting because all parent processes are doing the same wait(0)

(the child that forks becomes the parent of the new child).



+1


source







All Articles