简体   繁体   中英

How fork() function works in this program?

I'm having some trouble with this program. I know what a fork() function does. It is used to create a new process from an existing process. The new process is called the child process, and the existing process is called the parent. The parent returnes the child's pid and the child returns 0. That said, I find hard to understand what the two fork functions do in this program.

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

int main()
{
    int i,y,x=1;
    for (i=0;i<4;i++)
        if(x && fork())
        {
            y = i;
            x = 0;
        }
    if (x) y = i;
    fork();
    printf("%i\n",y);
}

First assert that: fork don't fail. That is not true, but more simple

if (x && fork()) -> is true if x == 1 and if the fork return for the father then false. So the child will create a child too (except the last one) but the father just create one child.

After the loop you have 1 + 4 new process. This 5 processus do the last fork(), you have 10 process.

The output result is not deterministic because of the scheduling of tasks.

Original process opens up a process when it enters the loop's if . The child does not enter the if since fork() == 0 . Now the parent has x == 0 and no longer enters the following iterations' if s (short circuit && prevent fork s).

note: short circuit if(x && fork()) prevents from forking twice since x == 0 => no need to evaluate fork() (true for all processes that forked once). A process that forked once never enters loop's if because x == 0 from that point on.

What you get is each loop value twice because each new process forks once the following iteration and outside the loop right before the print. There are no forks other than the first for each spawned process in the loop.

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

int main()
{
    int i,y,x=1;
    for (i=0;i<4;i++)
        if(x && fork()) // only un-forked process (x=1) executes fork()
        {
            // only parent process (fork() != 0) execute this
            y = i;
            x = 0;
        }
    if (x) y = i; // set up i=4 for last forked process (has x=1)
    fork();
    printf("%i\n",y);
}

The process spawning process would look something like this:

分叉过程,并且在最后一次打印之前

tip: When investigating code like this you can add output statements ( printf ) or use a debugger.

Actually you can learn something from this program, when you look at the pids the forks return and estimate from that, which fork produces what output:

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

int main()
{
    int i,y,x=1;
    pid_t p,c;
    for (i=0;i<4;i++)
        if(x && 0 != (p=fork()))
        {
            y = i;
            c = p;
            x = 0;
        }
    if (x) y = i;
    p=fork();
    printf("%i %d %d\n",y, p, c);
}

For example I get this output:

0 24413 24412
0 0 24412
2 24417 24416
1 24415 24414
1 0 24414
3 24419 24418
2 0 24416
3 0 24418
4 24420 0
4 0 0

The first line is the child of the first child, the next line is the first child itself and so on. The last line is the parent.

Think over that and you can learn how fork works.

Err, sorry, I was completely wrong, nearly: fork returns a positive pid to the parent, not to the child of course. So each line with positive p and c is the parent and so on. I leave the rest to figure out for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM