Question

I found this code from U of Regina's CS website. http://www2.cs.uregina.ca/~hamilton/courses/330/notes/unix/fork/fork.html

#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;
}

The output of the program is as follows:

$ ./a.exe
0. I am process 2196
1. I am process 2196
1. I am process 6560
2. I am process 6560
2. I am process 4472
2. I am process 2196
2. I am process 2288

I would like to make sure that I understood this program correctly. Does the creation diagram look something like this:

        2196
     /        \
   2196       6560
      \       /    \
      2288    6560  4472

The first process created is 2196(parent) and that created 6560(child). Since fork is called again 6560(now parent) 4472 is created. Meanwhile, 2196(from the first fork) still running and created 2288(child). So in total, 4 processes were made (2 parents and 2 children). Is this correct?

Was it helpful?

Solution

Your understanding is almost correct. However, you cannot be sure about parent of 4472 and 2288, they could be either 2196 or 6560.

To find out, you can change the code to the following :

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

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

Note

In general, with n fork() calls, you will end up with 2 ^ (n) unique processes.

For example, n = 2 here, and thus you get 2 ^(2) = 4 unique processes.

OTHER TIPS

Yes, that is mostly correct because you do not use wait after the forks. It is not sure which process exactly creates which process, but the main theory about the number of childs, etc, is correct. Your extended graph would be something like this (disregard the exact process numbers, they could change, but the form of the graph):

1. stage:           2196
                  /      \
                 /        \
2. stage:       2196       6560
               /  \       /   \
3. stage:    2196 2288   6560  4472

The exact process identifiers could be figured out by printing both the current pid (getpid) and the parent pid (getppid).

Strictly speaking, this could even get more complex with threads introduced, etc...

You could always see which process is related to which process by printing getppid (get parend process id).

In summary, you start with one process, 2196. This forks itself into two, 2196 + 6560, these each of which split into two processes, making four. It's impossible to tell which is the parent of which from what you print (adding parent pid will give you the ability to see that), but there are clearly four processes at the end.

Not quite, your tree contain the first initial process twice, but it should be only once, and the first child you have three times, but it should also be only once. The initial process will have two children, and the first child will have one child.

However, it's impossible to know the exact tree as we can't tell which parent create pid 4472 and pid 2288. You need to also print out the parent pid (getppid) to know which child belongs to which parent.

So the tree could look like this

          2196
         /    \
     6560      2288
    /
4472

Or it could look like this

          2196
         /    \
     6560      4472
    /
2288
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top