0

This is the example I was given and I was asked how many processes are created.

c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(void){
    int pid = (int)fork();
    pid = fork();
    pid = fork();
    printf("%d\n", pid);
    if (pid == 0){
        fork();
    }
    fork();

    return 0
}

Initially I see 5 forks so 2^5 - 1 = 31 processes. However it quickly became apparent that was not the case as the if (pid == 0){ fork(); } throws me off.

I modified the code to get some sort of count:

c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int count = 1;

int main(void){
    int pid = (int)fork(); 
    pid = fork(); 
    pid = fork(); 
    if (pid == 0){ 
       printf("2 ");
       fork(); 
    }
    fork(); 
    printf("1 ");
    return 0;
}

Result:
1 1 1 1 2 1 1 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 1 2 1 2 1 1 2 1 2 1 2 1

That is 24 1's and 8 2's Using this information I would assume 24-1 = 23 processes were created 8 of which by the if loop but I simply can't explain it and if I should minus 1 in the first place. I would greatly appreciate some input on this thanks.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Nomad
  • 11
  • 3
  • 1
    Use a pen and paper to draw the tree of processes generated. – Some programmer dude Feb 21 '23 at 14:59
  • If doing this turns to complex to understand at a glance, then all manner of warning lights should go off in your head screaming "BAD DESIGN". As is often the case with fork(). In case you must insist on using fork() then for the love of Unix slop encapsulate each spawn in a module of its own. That is, as soon as you've called the damn thing check if you are the parent or the child then call a function parent() or child() etc and take it from there. Everything else is madness and obfuscation. – Lundin Feb 21 '23 at 15:06
  • 1
    Add many more print statements — and include PID and PPID in each print operation, calling the `getpid()` and `getppid()` functions each time. The inconsistency of casting `fork()` once is a bit puzzling. It's also worth noting the information in [`printf()` anomaly after `fork()`](https://stackoverflow.com/q/2530663/15168). Don't trust printf operations with buffered output. – Jonathan Leffler Feb 21 '23 at 15:07
  • Re “That is 24 1's and 8 2's”: Only for large values of 8, notably 16. – Eric Postpischil Feb 21 '23 at 15:08
  • @EricPostpischil: It's like he managed to mentally unwind the printf anomaly. – Joshua Feb 21 '23 at 15:48

2 Answers2

3

We can see the program always execute to the single return statement.

Each process that reaches the fifth fork() becomes two processes.

The fourth fork(), inside the if, creates one process but only if it is in the child of the previous fork, so, each pair of parent-child processes that reach this if becomes three processes (a parent, its child, and the new child of this fork()). Then the fifth fork(), discussed above, multiplies that to six. So each two processes that reach this point ultimately become six. Equivalently, for one process that reaches this point, there are ultimately three.

Each process that reaches the third fork() becomes two processes. Then the subsequent fork() calls multiply that by three, making six.

Each process that reaches the second fork() becomes two processes. The subsequent fork() calls multiply that by six, making 12.

Each process that reaches the first fork() becomes two processes. The subsequent fork() calls multiply that by 12, making 24.

Thus one original process results in 24 total processes, so the first process is the ancestor of 23 others.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • I'm really struggling with your 3rd paragraph, so far from the top I have: P0 / | \ P1 P4 P2 / \ \ P3 P6 P5 / P7 and I'm really struggling to visualise the next step. Well that didn't turn out well appending my answer – Nomad Feb 21 '23 at 15:50
2

After the first fork, you have one parent and one child (total 2).

After the second fork, you have two parents and two children (total 4).

After the third fork, you have four parents and four children (total 8).

Each of the 4 children then forks, giving you a total of 12.

Then all the processes fork, giving 24.

pmacfarlane
  • 3,057
  • 1
  • 7
  • 24