fork()理解,关于执行和子代

时间:2021-12-01 13:53:31

I'm just starting to fork() and I'm having some difficulties understanding the parallel execution. I've found this example code and I want to know if the first time it will go true or false (I know if pid1==0 it means it's a child, etc). I also want to know how many copies (children will be created) and some details on the general execution.

我刚刚开始fork(),我在理解并行执行方面遇到了一些困难。我找到了这个示例代码,我想知道它是第一次是真还是假(我知道如果pid1 == 0这意味着它是一个孩子,等等)。我还想知道有多少副本(将创建子项)和一般执行的一些细节。

I have tried to run it and added the return 0; (my original source didn't have it) just to see if exits... but as you can see it "waits"

我试图运行它并添加返回0; (我的原始来源没有它)只是为了看看是否退出...但你可以看到它“等待”

http://i.imgur.com/D3XEFgs.png

int main(void)
{
    int pid1, pid2, pid3, pid4;
    pid1=fork();
    if (pid1!=0) 
    { 
        pid2=fork(); 
        pid3=fork(); 
        printf("\t\t IF(TRUE) pid1=%d and pid2=%d and pid3=%d\n",
               pid1, pid2, pid3);
    }
    else
    {
        pid4=fork();  
        printf("\nIF(False) FATHER is talking with pid1=%d and pid4=%d\n",
               pid1, pid4);
    }
    return 0;
}

2 个解决方案

#1


This program creates five descendant processes, and makes six calls to printf, of which four will be the IF(TRUE) message and two will be IF(FALSE). Here is an ASCII-art diagram of the control flow; every time it branches, both sides are executed, with the parent going straight down and the child to the right. The numbers are the fork calls initializing the pid1, pid2, ... variables, the letters T and F are the IF(TRUE) and IF(FALSE) messages, and the _ is the return 0 at the end of the function. This program does not wait for anything; all control flow paths reach the return 0 eventually.

该程序创建五个后代进程,并对printf进行六次调用,其中四个将是IF(TRUE)消息,两个将是IF(FALSE)。这是控制流程的ASCII艺术图;每次分支时,双方都会被执行,父母直接向下,孩子向右。数字是初始化pid1,pid2,...变量的fork调用,字母T和F是IF(TRUE)和IF(FALSE)消息,_是函数末尾的返回0。这个程序不等任何事情;所有控制流路径最终到达返回0。

|
1
|\------\
2       4
|\--\   |\
3   3   | |
|\  |\  | |
T T T T F F
| | | | | |
_ _ _ _ _ _

The output messages may appear in any order. And it's possible that the topmost parent process will exit (returning control to the shell) before all of the descendant processes have written their messages, so (as is visible in your screeenshot) the shell prompt may get jumbled up with the messages, too.

输出消息可以按任何顺序出现。并且在所有后代进程都写入消息之前,最顶层的父进程可能会退出(将控制返回到shell),因此(在screeenshot中可见)shell提示也可能与消息混淆。

Note that, going by the text of the messages, you have your if conditional backward: pid1 != 0 is true in the parent, not the child.

请注意,按照消息的文本,你有if条件向后:pid1!= 0在父级而不是子级中为真。

#2


Both true and false will be used on the first time.

真假都将在第一时间使用。

fork() copies the program (creates 1 child) and both programs continue execution from that point. The child process will take one branch and the parent the other.

fork()复制程序(创建1个子节点),两个程序从该点继续执行。子进程将占用一个分支,而父进程将占用另一个分支。

The number of fork()s is as follows:

fork()的数量如下:

You start with 1 process

你从1个过程开始

After pid1 -> +1 processes

在pid1 - > +1进程之后

Parent Branch

After pid4 -> +1 processes

在pid4 - > +1进程之后

Child Branch

After pid2 -> +1 processes

在pid2 - > +1进程之后

BOTH of the newly created processes run fork() for pid3, so after pid3 -> +2 processes

两个新创建的进程都为pid3运行fork(),所以在pid3 - > +2进程之后

You get 5 children + the original process.

你得到5个孩子+原始过程。

#1


This program creates five descendant processes, and makes six calls to printf, of which four will be the IF(TRUE) message and two will be IF(FALSE). Here is an ASCII-art diagram of the control flow; every time it branches, both sides are executed, with the parent going straight down and the child to the right. The numbers are the fork calls initializing the pid1, pid2, ... variables, the letters T and F are the IF(TRUE) and IF(FALSE) messages, and the _ is the return 0 at the end of the function. This program does not wait for anything; all control flow paths reach the return 0 eventually.

该程序创建五个后代进程,并对printf进行六次调用,其中四个将是IF(TRUE)消息,两个将是IF(FALSE)。这是控制流程的ASCII艺术图;每次分支时,双方都会被执行,父母直接向下,孩子向右。数字是初始化pid1,pid2,...变量的fork调用,字母T和F是IF(TRUE)和IF(FALSE)消息,_是函数末尾的返回0。这个程序不等任何事情;所有控制流路径最终到达返回0。

|
1
|\------\
2       4
|\--\   |\
3   3   | |
|\  |\  | |
T T T T F F
| | | | | |
_ _ _ _ _ _

The output messages may appear in any order. And it's possible that the topmost parent process will exit (returning control to the shell) before all of the descendant processes have written their messages, so (as is visible in your screeenshot) the shell prompt may get jumbled up with the messages, too.

输出消息可以按任何顺序出现。并且在所有后代进程都写入消息之前,最顶层的父进程可能会退出(将控制返回到shell),因此(在screeenshot中可见)shell提示也可能与消息混淆。

Note that, going by the text of the messages, you have your if conditional backward: pid1 != 0 is true in the parent, not the child.

请注意,按照消息的文本,你有if条件向后:pid1!= 0在父级而不是子级中为真。

#2


Both true and false will be used on the first time.

真假都将在第一时间使用。

fork() copies the program (creates 1 child) and both programs continue execution from that point. The child process will take one branch and the parent the other.

fork()复制程序(创建1个子节点),两个程序从该点继续执行。子进程将占用一个分支,而父进程将占用另一个分支。

The number of fork()s is as follows:

fork()的数量如下:

You start with 1 process

你从1个过程开始

After pid1 -> +1 processes

在pid1 - > +1进程之后

Parent Branch

After pid4 -> +1 processes

在pid4 - > +1进程之后

Child Branch

After pid2 -> +1 processes

在pid2 - > +1进程之后

BOTH of the newly created processes run fork() for pid3, so after pid3 -> +2 processes

两个新创建的进程都为pid3运行fork(),所以在pid3 - > +2进程之后

You get 5 children + the original process.

你得到5个孩子+原始过程。