fork()

Fork system call use for creates a new process, which is called child process, which runs concurrently with process (which process called system call fork) and this process is called parent process. After a new child process created, both processes will execute the next instruction following the fork() system call. A child process use same PC (program counter), same CPU registers, same open files which use in parent process.

It take no parameters and return integer value. Below are different values returned by fork().

Negative Value : creation of a child process was unsuccessful.
Zero : Returned to the newly created child process.
Positive value : Returned to parent or caller. The value contains process ID of newly created child process.

1.Predict the Output of the following program.

#include<stdio.h> 
#include<sys/types.h> 
#include<unistd.h> 
intmain( )
   { 
   fork();            
       //make two process which run same program after this instruction   
   printf("Hello world!\n");    
   return 0;    
   }

Output:

Hello world!
Hello world!

2. Calculate number of times hello is printed.

#include<stdio.h> 
#include<sys/types.h> 
int main( )    
{   
    fork( );   
    fork( );    
    fork( );   
 printf("hello\n");   
 return 0;    
}

Output:

hello
hello
hello
hello
hello
hello
hello
hello

Number of times hello printed is equal to number of process created. Total Number of Processes = 2n where n is number of fork system calls. So here n=3, 2^3 = 8

Let us put some label names for the three lines:

  fork ();    // Line 1
  fork ();    // Line 2
  fork ();    // Line 3

       L1       // There will be 1 child process created by line 1
    /     \
  L2      L2    // There will be 2 child processes created by line 2
 /  \    /  \
L3  L3  L3  L3  // There will be 4 child processes created by line 3

So there are total eight processes (new child processes and one original process).

3.Predict the Output of the following program

#include<stdio.h> 
#include<sys/types.h> 
#include<unistd.h> 
void forkexample( )          
   {               
    //child process because return value zero     
    if(fork( )==0)         
    printf("Hello from Child!\n"); 
  //parent process because return value non-zero.     
   else             
   printf("Hello from Parent!\n");           
   }
 
int main( )        
   {			     
   forkexample( );     
   return 0;        
   }

Output:

1.
Hello from Child!
Hello from Parent!
     (or)
2.
Hello from Parent!
Hello from Child!

In the above code, a child process is created, fork() returns 0 in the child process and positive integer to the parent process.
Here, two outputs are possible because parent process and child process are running concurrently. So we don’t know if OS first give control to which process a parent process or a child process.

Exercise:

  1. A process executes the following code.
for(i= 0; i < n; i++)  
fork( );

The total number of child processes created is: (GATE CS 2008)
(A) n
(B) 2^n – 1
(C) 2^n
(D) 2^(n+1) – 1;

  1. Consider the following code fragment:
if (fork()== 0) 
{
a= a + 5;  
printf("%d,%d\n", a, &a);  
} 
else 
{
a= a –5;  
printf("%d,%d\n", a, &a);  
}

Let u, v be the values printed by the parent process, and x, y be the values printed by the child process. Which one of the following is TRUE? (GATE-CS-2005)
(A) u = x + 10 and v = y
(B) u = x + 10 and v != y
(C) u + 10 = x and v = y
(D) u + 10 = x and v != y

 

Leave a comment