简体   繁体   English

了解fork()系统调用的工作方式

[英]Understanding how fork() system call works

I have this C code sequence: 我有这个C代码序列:

    printf("\nThe PID of this (main) process is: %d\n", getpid());

    if(fork() != -1) { // #1
        printf("\n\nParent 1 PID: %d\n", getpid());
        for(int i = 0; i < 10; i++) {
            printf("\n%d", i);
        }

        if(fork() != -1) { // #2
            //sleep(1);
            printf("\n\nParent 2 PID: %d\n", getpid());
            for(char i = 'a'; i != 'f'; i++) {
                printf("\n%c", i);
            }
        }
        else { // #3
            sleep(3);
            printf("\n\nChild 2 PID: %d\n", getpid());
            for(char i = 'F'; i != 'J'; i++) {
                printf("\n%c", i);
            }
        }
    }
    else { // #4
        sleep(4);
        printf("\n\nChild 1 PID: %d\n", getpid());
        for(int i = 10; i < 20; i++) {
            printf("\n%d", i);
        }
    }

I expect that I will have 4 processes: two parents and two childs. 我希望我有四个过程:两个父母和两个孩子。 At line #1 I call fork() for first time, and everything from line #1 to line #4 will be executed in first parent process. 在第1行,我第一次调用fork() ,从第1行到第4行的所有内容都将在第一个父进程中执行。 In the parent process (1) I call fork() one more time, so from line #2 to line #3 I will have the parent 2 process, and from line #3 to #4 child 2 process. 在父进程(1)中,我再调用一次fork() ,因此从第2行到第3行,我将拥有父2进程,从第3行到#4是子2进程。

What I expect to be printed: 我希望打印的内容:

Parent 1 PID: ....
0
1
2
3
4
5
6
7
8
9
Parent 2 PID: ....
a
b
c
d
e
Child 2 PID: ....
F
G
H
I
Child 1 PID: ....
10
11
12
13
14
15
16
17
18
19

What I actually got: 我实际得到的是:

Parent 1 PID: 3877

0
1
2
3
4
5
6
7
8


Parent 1 PID: 3878

0
1
2
3
4
5
6
7
8
9

Parent 2 PID: 3877

a
b
c
d
e9

Parent 2 PID: 3878
9


a
b
c
d
Parent 2 PID: 3879

a
b
c
d
e9

eParent 2 PID: 3880

a
b
c
d
e

What I do wrong? 我做错了什么?

This line isn't doing what you think: 这条线没有按照您的想法做:

if(fork() != -1) { // #1

That will succeed for both the parent and the child (as long as fork is possible, which is almost always the case). 父母和孩子都会成功(只要有可能使用fork ,几乎总是这样)。 You mean to test against 0 here. 您的意思是在这里测试0。 The parent will get 0, the child will get >0. 父母将获得0,孩子将获得> 0。 -1 is an error. -1是错误。

In your case, what you've marked as the "child" legs should never be executed unless there are errors. 就您而言,除非有错误,否则切勿执行已标记为“子”腿的操作。 I don't think that's what you meant. 我认为那不是你的意思。 What you're seeing are the initial 2 (parent and child) forks plus 4 (parent+child * 2) second forks. 您看到的是最初的2个(父代和子代)叉子加上4个(父代和子代* 2)第二个叉子。 That's 6 forks, which is what the output indicates. 这是6个fork,这就是输出所指示的内容。

from man fork : man fork

RETURN VALUE
       On success, the PID of the child process is returned in the parent, and 0 is returned in the child.  On failure, -1 is returned in the parent, no child process is created, and errno
       is set appropriately.

this means, that you should expect 0 in the child process and the childs pid in the parent process, so your code should look something like this: 这意味着,您应该在子进程中期望0,在父进程中期望childs pid,因此您的代码应如下所示:

switch(pid = fork()) {
  case -1:  //error handling here
  case 0:   // child process code here
  default:  // parent process code here.
}

merry christmas :) 圣诞节快乐 :)

For understanding detail functioning of how Fork System call works ? 为了了解Fork System调用如何工作的详细功能? you can go to this link : http://linuxtrainers.wordpress.com/2014/12/31/how-fork-system-call-works-what-is-shared-between-parent-and-child-process/ 您可以转到以下链接: http : //linuxtrainers.wordpress.com/2014/12/31/how-fork-system-call-works-what-is-shared-between-parent-and-child-process/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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