1

I'm a newbie at unix shell programming and I have a really hard time getting an good explanation about forks and their trees. All I know for now is that a fork kind of copies a process (child) from the main one (parent).

More specifically I can't quite get from a piece of code how a process tree would be like.

For example, at this backbone of code:

pid1=fork();
if (pid1!=0) 
{   
    pid2=fork(); 
    pid3=fork(); 
}
else 
{ 
    pid4=fork(); 
} 

It seems to me that there are 3 processes under the original process (pid1, pid2, pid3) and a child of one of these three (pid4), maybe pid2's.

I tried to run it with some more awfully written code:

int pid1, pid2, pid4, pid4;

printf("I'm the original Process %d with parent %d \n", getpid(), getppid());

pid1=fork();

if(pid1!=0)
{
    pid2=fork();
    printf("I'm P2 %d with parent %d \n", getpid(), getppid());
    sleep(1);

    pid3=fork();
    printf("I'm P3 %d with parent %d \n", getpid(), getppid());
    sleep(1);
}
else
{
    pid4=fork();
    printf("I'm P4 %d with parent %d \n", getpid(), getppid());
    sleep(1);
}

Edit: I put some sleep(1) at the code above, thanks to dbush, so I can see the ppid's

enter image description here

So, how is the tree forming with these types of code?

Coursal
  • 1,387
  • 4
  • 17
  • 32
  • 2
    Possible duplicate of [Why there are many PIDs printed while using fork system call?](http://stackoverflow.com/questions/19323483/why-there-are-many-pids-printed-while-using-fork-system-call) – cHao Mar 22 '17 at 20:40
  • 1
    Nitpick: the code you show doesn't produce the output you show; there are newlines in the output that aren't present in the source. It's really frustrating trying to debug the wrong code. You should probably add a 'while' loop to `wait()` for children to die. For example, `int corpse; int status; while ((corpse = wait(&status)) != -1) { …print information about dead child… }`. This would give you better information about the tree structure. The parent process 1 is the system process that waits for orphaned processes to exit. – Jonathan Leffler Mar 22 '17 at 20:49
  • @JonathanLeffler I believe sleep(1)'s have kinda sort out the problem of ppid=1, is that right? – Coursal Mar 22 '17 at 21:30
  • I can't see any `sleep(1)` calls in your code — so I can't tell whether it will work OK or not. Please remind yourself about how to create an MCVE ([MCVE]) — you've been around long enough to have an idea what I'm talking about. – Jonathan Leffler Mar 22 '17 at 21:32
  • I've updated the code – Coursal Mar 22 '17 at 21:33
  • For future reference: there are three distinct groups of results from `fork()` that you should be looking out for: 0 (success, and this is the child), positive (success, this is the parent, and the return value is the child's pid), and negative (failure; there is no child). – cHao Mar 23 '17 at 14:33
  • @cHao This is extremely helpful and straight to the point. Thank you so much. – Coursal Mar 23 '17 at 19:15

1 Answers1

2

You'll get a total of 5 children:

                  c2--------------
                    |             
                    |             
     c1---------------------------
       |           p4             
       |                          
       |                          
       |                          
       |                c5--------
       |                  |       
       |                  |       
       |       c3-----------------
       |         |       p3       
       |         |                
       |         |      c4--------
       |         |        |       
       |         |        |       
p---------------------------------
      p1        p2       p3

The first fork happens at point p1, which create child c1. This child goes into the else part of the code and runs the fork at point p4, creating child c2.

Back in the parent, it forks at point p2 creating child c3. Both of these processes then fork at point p3, creating children c4 and c5.

The reason you're seeing some processes report their parent as 1 is because the parent is finishing before the child. Try putting sleep(1) at the bottom of this block to give the parent processes time to stay around so the children can see them.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • Based on your answer and the pid's & ppid's I got with sleep(1), is this the tree that gets created? http://i65.tinypic.com/15nu8gh.png – Coursal Mar 22 '17 at 21:04
  • No; your diagram seems to have 3 lines coming out of 'shell', which is at best confusing and most likely just wrong. – Jonathan Leffler Mar 22 '17 at 21:38