开发者

Confused with fork()

I am having a difficult time understanding what the fork() command does under different scenarios. Here is some sample code from my book:

int main() {
    int a = 12;
    int b = 9;
    int fid = fork();

    if (fid == 0) {
        a++;
    }
    else {
        wait(NULL);
        b = b - 5;
    }

    printf("program exit. a = %d, b = %d\n", a, b);
    retur开发者_如何学JAVAn 0;
}

Can someone walk me through what the fork() command is doing in this case and perhaps give some more examples to clarify?


                 [main]
                 a = 12
                 b = 9

                 fork()
                   |
                   |
    +--------------+--------------+
    |                             |  
    |                             |
[parent]                       [child]

fid = 1234                     fid = 0
wait(NULL)                     a++
   ...                         printf("a = 13, b = 9");
   ...                         return 0
   ...                         <exit>
b = b - 5
printf("a = 12, b = 4");
return 0
<exit>

After fork() executes there are two copies of the program. Each process gets its own copies of the variables, so there are now two a's, two b's, etc. The only difference between the two programs is the value returned from fork(): in the child process fork() returns 0; in the parent process it returns the PID of the child.

The child process increments a, prints the values of a and b, and exits.

The parent process first waits for the child process to terminate. Only after the child has finished does it continue on, subtracting 5 from b, printing out a and b, and then exiting.

The wait(NULL) ensures that the child process's printout always comes before the parent's, so you will always get the same output in a reliable order. Without it you wouldn't be able to depend on the order of the two printouts. They would be the same messages, just in an unpredictable order.


  1. a is set to 12, b is set to 9.

  2. fork is called, we now have two processes.

  3. The parent gets the PID of the child, and goes to the else clause. The child gets 0, and goes to the if clause.

  4. The parent waits for the child to finish.

  5. The child increments its copy of a. So a is now 13 in the child and 12 in the parent.

  6. The child exits, outputting 13 and 9.

  7. The parent subtracts 5 from its copy of b, so b is now 4 in the parent.

  8. The parent exits, outputting 12 and 4.

Note that the exact order of execution of the child and parent after fork is not guaranteed, but it doesn't change the results because the parent waits for the child to finish before it does anything.

Also, note that having both processes exit normally is bad practice. When one process exits normally, it runs cleanup handlers that can confuse the other process, for example by changing the file position on shared file descriptions, causing data corruption.


When you call fork(), the entire process including all memory/variables etc... are duplicated.

So after the fork call, each process has it's own copy of a and b which start as 12 and 9 respectively.

Process 0, will increment it's own copy of a. Process 1 will decrement (by 5) it's own copy of b.

So they should print:

Process 0: a = 13, b = 9
Process 1: a = 12, b = 4


Its o/p would be Parent :: a=12 , b=4 Child :: a=13 , b=9

as both process execute simultaneously but both have different copies of local variables a,b so local var. affected by one process will not be visible to another process as both are working on their own copies

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜