Understanding fork() in for loop Understanding fork() in for loop unix unix

Understanding fork() in for loop


Crunching the first full iteration from initialization through all processes finishing the first loop context, the following will happen:

1. Initializer

i is initialized from i = fork();

  • Process parent: i = pid(child1)
  • Child process child1: i = 0

Hitting the conditional test in each process something interesting happens

2. Process : parent

i < fork() will fork another child process, child2. If the returned pid(child2) is greater than pid(child1), the conditional test is met and the parent process continues to the loop body

3. Process : child1

  • i is zero from the initializer

  • i < fork() will fork a child process, child3. If the returned pid(child3) is greater than the value of i (which is zero, so it always will be), the conditional test is met and the child1 process continues to the loop body

4. Process: child2

  • Inherits i from parent, where i is pid(child1)
  • This process was spawned from evaluating i < fork(), so fork() will eval as 0. Therefore...
  • i < fork() will evaluate to be child1(pid) < 0, which will never be true. The conditional test fails, and the for-loop is terminated.

5. Process: child3

  • Inherits i = 0 from its parent, child1.
  • This process was spawned from evaluating i < fork(), so fork() will eval as 0. Therefore...
  • i < fork() will evaluate to be 0 < 0, which will never be true. The conditional test fails and the for-loop is terminated.

At this point, parent and child1 are the only two processes that make it to the loop body. The other two (child2 and child3) both failed their conditional checks. As a result, the following things happen:

6. The Final Steps

  • parent process is replaced with execlp("echo", "sono", argv[0], 0);
  • child1 process is replaced with execlp("echo", "sono", argv[0], 0);
  • child2 process invokes system("echo i+$i"); and terminates.
  • child3 process invokes system("echo i+$i"); and terminates.

This is important: At no time do any of the aforementioned processes evaluate the for-conditional more than once. Anything that succeeds the conditional test will be replaced with an execlp() process launch. Anything that fails the conditional test will leave the loop and terminate after the system() call. Therefore, once any process makes it past the conditional (either by success or failure) it will never fork() another process.

In other words, This is NOT a fork() bomb. If either the loop body or the suffix code past the loop forked another instance of this process, it would easily be a fork() bomb, but neither do so.


Note: it is possible that a pid-rollover, where process ids reset and start "filling in the holes", causes the first initial fork to introduce a child2 pid that is less than the child1 pid. If that happens, (however unlikely) the results would only change to this:

  • parent process invokes system(“echo i+$i”); and terminates.
  • child1 process is replaced with execlp("echo", "sono", argv[0], 0);
  • child2 process invokes system("echo i+$i"); and terminates.
  • child3 process invokes system("echo i+$i"); and terminates.

Not bloody likely, but then neither is winning a lottery, and people think it will happen to them all the time.


that code is incredibly twisted and wrong. But when you don't know what a code does, just run it "manually"

int main(int argc, char **argv) {    int i;    for(i = fork(); i < fork(); i++)        execlp(“echo”, “sono”, argv[0], 0);    system(“echo i+$i”);}

parent process:

i = fork();

here i contains the PID of the newly created process

execlp(“echo”, “sono”, argv[0], 0);

as @Whozcraig corrected me in the comment (and shame on me for making that 1st year mistake)

it runs echo and outputs "sono a.out" on the output (considering the name of prog is a.out). execlp() replaces the current process with its arguments and stops execution here.

before edit I was saying it was fork()ing indefinitely

child process:

i = fork();

here i contains the value 0, indicating this is the newly created process.

execlp(“echo”, “sono”, argv[0], 0);

is out again… replacing current process with its arguments and stops execution once echo is over.

*before edit, I was saying it was fork()ing again.

So at first look it looks like you got a twisted and crazy fork bomb, that will grow exponentially. But in the end, you got a twisted and crazy single fork that only executes the execlp() content twice.

N.B.: system(“echo i+$i”); is nonsense, as $i has no meaning in the context of this code. Even though it will never be executed.

in the end your instructor is really perverse.