The fork() system call using C++ in Linux

In this tutorial, we will learn what fork() system call does and how it is implemented in Linux systems (only). There are some example questions to help you understand better.

What is fork() system call?

fork() creates a new child process. If we call fork() in the parent program, it creates a child process which shares an exact copy of the address space but a different one. Both parent and child processes have different address spaces, but they share the same memory segment.

syntax – 

#include<sys/types.h>
#include<unistd.h>

pid_t fork(void);

The fork() system call returns the child process ID to the parent. As both share the same memory segment, how do we distinguish if the process executing is the child or the parent? We can distinguish child and parent processes because fork() returns inside the child process and returns a positive integer inside the parent process.
To demonstrate this, we have 2 programs.

Program to just print output without knowing which process prints which output:

#include<iostream>
#include<unistd.h>
#include<sys/types.h>
int main()
{
  //create a child process
  //thus making 2 processes running at the same time
  fork();

  cout<<"Michael Jackson"<<endl;
  return 0;
}

Output –

Michael Jackson
Michael Jackson

Program to differentiate between the child and the parent process:

#include<iostream>
#include<unistd.h>
#include<sys/types.h>
int main()
{
  //create a variable to hold the process ID
  pid_t pid;

  //create a child process
  //thus making 2 processes run at the same time
  //Store the Process ID in 'pid'
  pid = fork();

  //Check if 'pid' is 0
  //If yes, then it's parent process
  //Else, it is the child process
  if(pid==0)
    cout<<"Output from the child process."<<endl;
  else
    cout<<"Output from the parent process."<<endl;
  
  return 0;
}

Output – 

Output from the parent process.
Output from the child process.
          OR
Output from the child process.
Output from the parent process.

There can be 2 possible outputs because we never know the order in which the OS executes the processes. When the run this program 3-4 times, you can see the difference in the output.

How many fork() to be given?

This is an interesting question. Because, we already know that the parent process and the child process, both share the same memory segment. Now what this means is that both these processes are executing the same program. But the only difference is, inside the child process, the called fork() returns 0(zero). And the execution is continued after the fork() call. The interesting part here is when one child process creates another set of child processes. This increases in the power of 2, because when one process creates another child process, there are then 2 processes.
The below program shows an example of what I’m trying to say –

#include<iostream>
#include<unistd.h>
#include<sys/types.h>
int main()
{
  //create child processes
  //so that they create more processes
  //within them
  fork();
  fork();
  fork();
  cout<<"Will Smith"<<endl;
  
  return 0;
}

Output – 

Will Smith
Will Smith
Will Smith
Will Smith
Will Smith
Will Smith
Will Smith
Will Smith

The number of times the output is printed is equal to the number of processes created. Here, there are 8 processes created and therefore the output is printed 8 times. The total number of processes = 2n. So here, the total number of processes created is 23= 8.
The total number of child processes created is always 2n-1.

The below diagram will help you understand how many processes are created –

fork ();   // 1st call
fork ();   // 2nd call
fork ();   // 3rd call

      P1            // There will be 1 child process 
    /     \         // created by the 1st call. 
  P2      P2        // There will be 2 child processes
 /  \    /  \       //  created by 2nd call. 
P3  P3  P3  P3      // There will be 4 child processes 
                    // created by 3rd call.

So in total, there are 8 processes; 7 child processes and 1 parent process.

Other interesting articles :

Leave a Reply

Your email address will not be published. Required fields are marked *