Rating

IT Networking Fundamentals basic level(Computer Networking) rating

Fork System Call in OS | Fork() function in C++ | tarGATE CS

Fork System Call

The fork is a system call which when called by the Parent Process creates a new process called Child Program. This child process copies the image of the parent program along with the program counter. Suppose the program counter contains the value 7 that means the next instruction to be executed will be 8th. And this 8th instruction will be executed by both the parent and child process. 
The order in which they will execute the same instruction will be a situation of doubt which is being decided by the compiler being used. Let's take one example into consideration in order to understand it easily:

Example 1:


Fork System Call in OS | Fork() function in C | tarGATE CS
Code Snippet having one Fork Statement

In this code, after executing the 4th statement, the child process will be created. After 4th instruction, both ( Parent and Child process) will execute the program. That means the output of this program will be:
Fork System Call in OS
The output of example 1
The value of variable "a" will be printed by both the parent and child processes. It will be printed in one line as there was no end-line character or endl in the cout statement. 

Let's consider one more example of Fork in order to understand the concept of nested fork:

Example 2

Fork System Call in OS
 Code Snippet having two Fork Statement
In this example, when the fork call at the 4th statement will be executed it will create one child process (say C1). 5th statement will be executed twice, one by parent process (say P) and once by child C1. As a result, 2 more child processes will be created, C2 by the parent process and C3 by child process C1 while executing the 5th statement. So at last statement 6th will be executed 4 times, one by parent program and 3 times by 3 different child processes (one each). The output of example 2 will be:
Fork System Call in OS
The output of example 2
To make it easier for you to understand here is a  video lecture of this whole concept with examples:





Let's take this discussion towards a deeper level, what if we further increase the count of fork statements. Consider this example of code having three fork statements:

Example 3:

Fork System Call in OS
Code snippet with three fork statement


In this example, when the fork call at the 4th statement will be executed it will create one child process (say C1). 5th statement will be executed twice, one by parent process (say P) and once by child C1. As a result, 2 more child processes will be created, C2 by the parent process and C3 by child process C1 while executing the 5th statement. Now, our program execution reaches the 6th statement, which will be executed by, process P, C1, C2, C3. Fork system call at 6th statement will result in 4 more child processes (say C4, C5, C6, C7 ) created by process P, C1, C2, C3 respectively. This will sum up to 8 processes in total that will be running the 7th statement and the output of this will be as shown under. Remember the order in which they will execute the 7th statement will be decided by the compiler being used.
Fork System Call in OS | Fork() function in C | tarGATE CS
The output of example 3

Formula for number of child processes:

Now we will discuss the formula for calculating number of child processes. By this concept u can calculate no of child processes with just one glance at the code snippet.




What does Fork() returns:

As we know every function returns something (if return type is not void), so like any other function FORK() also returns a value. This value is eigher negative, positive, or zero.
  1. Negative: It will return a negative value when fork couldn't create child process successfully.
  2. Positive:  It returns the process id of the child process being created by it when Parent Process calls it.
  3. Zero: It will return the value ZERO if being called by the child process created by this statement itself.
If there is a fork statement as a condition of if-block ( as shown under ) then it will create a child process and also returns a value. The value returned by fork() here will be Positive when called by the parent process and will be Zero when called by the child process.

Fork System Call in OS
fork with if block
In order to make concepts of returning a value by fork more understandable, here is a video lecture on some examples of the fork from previous years of competitive exams:







A fork is a function which doesn't work in the Windows environment, you need to work in LINUX in order to test this concept.

Stay Home, and Study #WithMe.

#tarGATE CS signing off 😇



Oldest