简体   繁体   English

在Linux中,wait()如何工作?

[英]How does wait() work in Linux?

Can anyone please explain why the output is like this? 谁能解释为什么输出是这样的? I am very much confused about how these processes are being executed (in which order?) and also about waitpid() / wait() . 我对如何执行这些进程(按什么顺序?)以及waitpid() / wait()感到非常困惑。 Here is the code: 这是代码:

#include<stdio.h>
main()
{
    int pid1, pid2, pid3;
    pid1=fork();
    if(pid1 == 0){
        printf("PID of child 1 is :%d\n",getpid());
        //sleep(2);
    }
    pid2=fork();
    if(pid2  == 0){
        printf("PID of child 2 is :%d\n",getpid());
        //sleep(2);
    }
    pid3=fork();
    if(pid3  == 0){
        printf("PID of child 3 is :%d\n",getpid());
        //sleep(2);
    }
    else{
        printf("PID of parent is :%d\n",getpid());
        waitpid(pid1,0,0);
        waitpid(pid2,0,0);
        waitpid(pid3,0,0);
    }
}

Actual output: 实际输出:

PID of child 1 is :4963

PID of parent is :4962

PID of parent is :4963

PID of child 2 is :4966

PID of parent is :4966

PID of child 2 is :4964

PID of parent is :4964

PID of child 3 is :4967

PID of child 3 is :4965

PID of child 3 is :4969

PID of child 3 is :4968

Expected output: 预期产量:

  1. PID of parent because pid1 is not 0 and never be 0 here. 父级的PID,因为pid1不为0,因此永远不为0。

  2. Then waits until pid1 ie child1 gets terminated and prints PID of child 1 然后等待直到pid1即child1被终止并打印孩子1的PID

  3. Then right now child2 and child3 are not forked yet so they are skipped 然后,child2和child3尚未分叉,因此已被跳过

  4. Then again PID of parent, pid of child1, pid of child2 然后再次是父级的PID,子级1的pid,子级2的pid

  5. Then PID of parent, pid of child1, pid of child2 and pid of child3. 然后是父级的PID,子级1的pid,子级2的pid和子级3的pid。

So where am I going wrong please? 那我哪里出问题了?

Here we go... 开始了...

pid1=fork()

At this point two processes are going. 至此,正在进行两个过程。 The parent [PID 4962] and the child that was just spawned [PID 4963]. 父母[PID 4962]和刚产卵的孩子[PID 4963]。 The parent has pid1 = 4963 [the child's PID], and the child [child1] has pid1 = 0. So, the child will print out: 父级的pid1 = 4963 [孩子的PID],而子级[child1]的pid1 =0。因此,孩子将打印出:

"PID of child 1 is: 4963"

And both processes go on their merry way until they get to: 并且这两个过程都按照自己的喜好进行,直到达到:

pid2=fork()

Here, The parent [PID 4962] and child1 [PID 4963] both spawn a child process. 此处,父项[PID 4962]和子项1 [PID 4963]均产生子进程。 We will call the child that the original parent spawns child2 [maybe PID 4964] and the child that child1 spawns, we will call child1_1 [maybe PID 4966]. 我们将称其为原始父代产生的子代child2 [也许是PID 4964],而将child1产生的子代称为孩子1_1 [也许是PID 4966]。 Now, the original parent has pid2 = 4964 [maybe] and child2 has pid2 = 0. Child1 has pid2 = 4966 [maybe] and child1_1 has pid2 = 0. Thus, both child2 and child1_1 will print something out: 现在,原始父级的pid2 = 4964 [也许],而child2的pid2 =0。Child1的pid2 = 4966 [也许],而child1_1的pid2 =0。因此,child2和child1_1都将输出:

"PID of child 2 is: 4966"
"PID of child 2 is: 4964"

Now, ALL of those processes get to this: 现在,所有这些过程都可以做到这一点:

pid3=fork()

Ouch. 哎哟。

The original parent, child1, child2, and child1_1 all spawn a child process. 原始的父对象child1,child2和child1_1都产生一个子进程。 What you ultimately end up with is something like this: 您最终得到的是这样的:

For the original parent, child1, child2, and child1_1, pid3 != 0 For their four child processes, pid3 == 0 对于原始的父代child1,child2和child1_1,pid3!= 0对于其四个子进程,pid3 == 0

So, those four child processes all report their PIDs like this: 因此,这四个子进程都按如下方式报告其PID:

"PID of child 3 is: xxxx"

But original parent [4962], child1 [4963], child2 [maybe 4964], and child1_1 [maybe 4966] print: 但是原始的父项[4962],child1 [4963],child2 [也许4964]和child1_1 [也许4966]打印:

"PID of parent is: xxxx"

and then wait for their child processes with PIDs pid1, pid2, and pid3 to return. 然后等待其PID为pid1,pid2和pid3的子进程返回。

Keep in mind that all of these processes are running concurrently, and that explains why you cannot necessarily predict the order in which the print statements will be carried out. 请记住,所有这些过程都是同时运行的,这说明了为什么您不一定可以预测打印语句的执行顺序。

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

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