简体   繁体   中英

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() . 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.

  2. Then waits until pid1 ie child1 gets terminated and prints PID of child 1

  3. Then right now child2 and child3 are not forked yet so they are skipped

  4. Then again PID of parent, pid of child1, pid of child2

  5. Then PID of parent, pid of child1, pid of child2 and pid of child3.

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]. The parent has pid1 = 4963 [the child's PID], and the child [child1] has pid1 = 0. So, the child will print out:

"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. 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]. 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:

"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. 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

So, those four child processes all report their PIDs like this:

"PID of child 3 is: xxxx"

But original parent [4962], child1 [4963], child2 [maybe 4964], and child1_1 [maybe 4966] print:

"PID of parent is: xxxx"

and then wait for their child processes with PIDs pid1, pid2, and pid3 to return.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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