Fork ( ) System Call in C Program with Examples

fork() system call

  • Fork system call use for creates a new process, which is called child process, which runs concurrently with the 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 uses the same pc(program counter), same CPU registers, same open files use in the parent process.
  • It takes no parameters and returns an integer value. Below are different values returned by fork().

Negative Value: the 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 the process ID of a newly created child process.

1. Predict the Output of the following program:

#include <stdio.h> 
#include <sys/types.h> 
#include <unistd.h> 
int main() 
{ 
  
    // make two process which run same 
    // program after this instruction 
    fork(); 
  
    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

The number of times hello printed is equal to the number of processes created. Total Number of Processes = 2n where n is a number of fork system calls. So here n = 3, 23 = 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
Code language: JavaScript (javascript)

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 the 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.

Important: Parent process and child process are running the same program, but it does not mean they are identical. OS allocate different data and states for these two processes and also control the flow of these processes can be different. See next example

4.Predict the Output of the following program.

#include <stdio.h> 
#include <sys/types.h> 
#include <unistd.h> 
  
void forkexample() 
{ 
    int x = 1; 
  
    if (fork() == 0) 
        printf("Child has x = %d\n", ++x); 
    else
        printf("Parent has x = %d\n", --x); 
} 
int main() 
{ 
    forkexample(); 
    return 0; 
} 

Output:

Parent has x = 0
Child has x = 2
     (or)
Child has x = 2
Parent has x = 0
  • Here, global variable change in one process does not affected two other processes because data/state of two processes are different.
  • And also parent and child run simultaneously so two outputs are possible.

Leave a Comment