1
#include<iostream>
#include<unistd.h>
#include<stdio.h>
using namespace std;
int main()
{
    fork();
    fork();
    fork();
    fork();
    printf("*"); /*This prints 16 stars*/ 
    return 0;
}

When working with fork(), why does it print 16 *'s?

I understand that fork() generates a new child process that both execute the same process which would explain why one fork generates 2 stars but, with four forks it prints 16 which I can see that it doubles with each fork().

But I am not understanding why. Is each fork executing the functions and parameters below it?

4
  • possible duplicate of How does fork() work? Commented Nov 4, 2014 at 23:38
  • And myriad others. (Most deleted for cause.) Commented Nov 4, 2014 at 23:38
  • Anyway using namespace std; in a C program? It's bad enough in C++. Commented Nov 4, 2014 at 23:39
  • 1
    I'm going to vote to keep this open because the repeated forks in the parent and the children increase at an exponential rate. I think the explanation will help the OP and future visitors develop insight into what's really going on with the summation of processes. Commented Dec 22, 2014 at 8:43

2 Answers 2

5

Because the first fork will split into two process, the second fork() call will be called by those two processes, splitting those two processes into 4. This will continue until all the fork() calls have been called in each process. So you end up having 2^4 = 16 calls to printf("*")

In the "diagram" each bar represents the number of processes that are executing when the function is called. So the function is executed as many times as there are bars.

       |   fork()             // The first processes creates a second (2 total)
       |   fork()    |        // Those 2 processes start 2 more       (4 total)
      ||   fork()    ||       // Those 4 processes start 4 more       (8 total)
    ||||   fork()    ||||     // Those 8 processes start 8 more       (16 total) 
||||||||  printf()   |||||||| // resulting in 16 calls to printf()

Is each fork executing the functions and parameters below it?

Yes, as you can see from the diagram, when a process forks the created process (and the one that created it) continues execution on the next instruction after the fork.

Sign up to request clarification or add additional context in comments.

1 Comment

Clear depiction, you gave a great explanation!
2

When you call fork(), it create a new process. You duplicate your application, and then the 2 applications continues to run and execute new instructions after the fork() call

printf("i'm the main thread\n");
fork();
printf("i'm executed 2 times\n");
fork(); //this fork is so executed 2 times, so 2 new processes, so 4 processes for all
printf("i'm excecuted 4 times\n");
fork(); //this fork is executed 4 times to ! So you have now 8 processes;
// and etc ..
fork(); //this fork is executed 8 times, 16 process now !
printf("*"); // executed 16 times

The new processes share all memory before the fork(), old changed states are for the current thread. if you want to do anothers things depending the process :

pid_t p;
int i = 1;
p = fork();
if(p == 0)
{
    printf("I'm the child\n");
    i = 2;
    exit(0); //end the child process
 }
 printf("i'm the main process\n");
 printf("%d\n", i); //print 1

1 Comment

Note that a fork() creates a process, not a thread. So the comment I'm the main thread is a bit off.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.