C 了解fork()系统调用的工作原理

C 了解fork()系统调用的工作原理,c,unix,process,fork,pid,C,Unix,Process,Fork,Pid,我有一个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); }

我有一个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);
        }
    }
我得到的是:

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

我做错了什么?

这句话并没有像你想的那样:

if(fork() != -1) { // #1
这对父级和子级都会成功(只要
fork
是可能的,几乎总是这样)。你的意思是在这里测试0。父级将获得0,子级将获得>0-1是一个错误


在您的情况下,除非有错误,否则您标记为“子”腿的内容永远不应该执行。我想你不是这个意思。您看到的是最初的2个(父级和子级)分叉加上4个(父级+子级*2)第二个分叉。这是6个叉子,这是输出所指示的。

这一行没有做您认为的事情:

if(fork() != -1) { // #1
这对父级和子级都会成功(只要
fork
是可能的,几乎总是这样)。你的意思是在这里测试0。父级将获得0,子级将获得>0-1是一个错误


在您的情况下,除非有错误,否则您标记为“子”腿的内容永远不应该执行。我想你不是这个意思。您看到的是最初的2个(父级和子级)分叉加上4个(父级+子级*2)第二个分叉。这是6个叉子,这是输出指示的。

来自
人叉

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.
这意味着您应该期望子进程中为0,父进程中为childs pid,因此您的代码应该如下所示:

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

圣诞快乐:)

来自
manfork

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.
这意味着您应该期望子进程中为0,父进程中为childs pid,因此您的代码应该如下所示:

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

圣诞快乐:)

了解Fork系统调用如何工作的详细功能? 您可以转到以下链接:
了解Fork系统调用如何工作的详细功能? 您可以转到以下链接:
什么是
fork()!=-1
检查?如果呼叫失败,将返回-1。因此,我检查调用是否没有失败。那么
else
块做什么呢?
e9
s是由于向后
printf
s造成的。将
\n
放在
printf
的末尾可以解决这个问题。@Oliver Charlesworth,现在我看到了问题:如果调用失败,
If
语句变为false,然后执行
else
语句。
fork()!=-1
检查?如果呼叫失败,将返回-1。因此,我检查调用是否没有失败。那么
else
块做什么呢?
e9
s是由于向后
printf
s造成的。把
\n
放在
printf
的末尾可以解决这个问题。@Oliver Charlesworth好吧,现在我看到了问题:如果调用失败,
如果
语句变为false,然后执行
else
语句。我喜欢圣诞节,但我不会嫁给它;v) 不过,祝你圣诞快乐。哈哈,谢谢:-D你可能已经猜到我不是以英语为母语的人;)来取密码,留下来上免费语法课:)+1我喜欢圣诞节,但我不会嫁给它;v) 不过,祝你圣诞快乐。哈哈,谢谢:-D你可能已经猜到我不是以英语为母语的人;)来取密码,留下来上免费语法课:)+1