Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
492 views
in Technique[技术] by (71.8m points)

linux - How do 2 or more fork system calls work?

Here's a code where I use 2 fork() system calls one after another - How does it actually work?

 #include <unistd.h>
 #include <iostream.h>
 using namespace std;

 int main()
 {
    cout << "0. I am process " << getpid() << endl;
    (void) fork();
    cout << "1. I am process " << getpid() << endl;
    (void) fork();
    cout << "2. I am process " << getpid() << endl;
}

I get the output as :
0. I am process 27701
1. I am process 25915
1. I am process 27701
2. I am process 27781
2. I am process 26170
2. I am process 27701

This is the next program where I've used 3 fork system calls, how do I get such an output? If I were to solve this code manually, what would be the logic?

#include <unistd.h>
#include <iostream>
using namespace std;

int main()
{
    cout << "0. I am process " << getpid() << endl;
    (void) fork();
    cout << "1. I am process " << getpid() << endl;
    (void) fork();
    cout << "2. I am process " << getpid() << endl;
    (void) fork();
    cout << "3. I am process " << getpid() << endl;
}

Here I get the output as :
0. I am process 27116
1. I am process 26147
2. I am process 27371
2. I am process 26147
3. I am process 24416
3. I am process 27371
3. I am process 27508
3. I am process 26147
1. I am process 27116
2. I am process 21406
2. I am process 27116
3. I am process 27369
3. I am process 21406
3. I am process 26752
3. I am process 27116

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

fork() works the same way every time you call it. A new process is created as an exact copy of the current process, and both continue execution as if they both just returned from the fork() function call, just with different return values. In your case you throw away that return value, so they're just identical processes.

Let's draw a picture for your first example. Sample output from a run I just made (since the output you put in your question is incomplete):

0. I am process 25597
1. I am process 25597
2. I am process 25597
1. I am process 25598
2. I am process 25599
2. I am process 25598
2. I am process 25600

You start with a single process with PID 25597. It prints the 0 line, and then forks. That yields two processes:

            25597          # prints "0"
             /
            /  
           /    
         25597 25598       # both print "1"

So far so good. Now both of these new processes call fork() again. The complete tree ends up looking like this:

                   25597
                    /
                   /  
                  /    
                 /      
                /        
             25597      25598       # both print "1"
              /          /
             /          /  
            /          /    
         25597  25599 25598 25600   # all four print "2"

The actual locations of 25599 and 25600 can't be guessed from the output, unfortunately - they could be the other way around, too.

For your 3-fork() example, you just have to do the same thing, but it will have another level in the diagram - you'll end up with 8 processes each printing the "3" line.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...