Fork() System Call

  • fork() is a system call API that is responsible for creating a new, duplicate process as that of the parent process called the child process.
  • The parent and child process is said to executing two instances of the same application or a program.
  • The child process will have a new Process Descriptor where most of the fields are common as that of the parent process.
  • A new set of the virtual addresses(VADs) are created and their contents are duplicated from the parent process that is virtual address space of the child is the same as that of the parent process.

int ret = fork(void);the

Return Type:
On success, it can return either 0 or non-zero

  • It returns 0 to the newly created child Process and
  • It returns a new PID of the child process to the parent process.
  • On success, fork returns twice: once in the parent and once in the child.

In case of failure, it returns -1.

Reasons for failure:

  • If the parent process has exceeded the limit for the number of child processes it can create.
  • If there is not enough space in the process table or not enough space in virtual memory.

Sample code for fork() API

int ret;
ret = fork();

    case 0:
         printf("\n It is the Child Process");
    case -1:
         printf("\n There is failure in creating child process");
         printf("\n This is the Parent Process");

Diag-1: fork() API

untitled image

Relation between parent and Child Process:

  • In the case of fork() API, both child, and parent will execute two instances of the same application or a program.
  • Since the Virtual address space of a child process will be the same as that of the parent process, most of the fields of a child process in its control block will be duplicate as that of the parent’s control block(PCB).

Some of the fields which are duplicate as that of parents are:

  • CPU Registers
  • Current working directory
  • Signal Mask
  • Scheduling Parameters
  • No of open files
  • Resource Limit
  • Identical Copy of Memory

The child process will have its Process ID and even data section and the state of the child process will be different from that of the parent.

Relation between number of child process created and fork system calls

  • If there are n fork calls, the number of child process created is 2^n -1 and
  • Total number of process is 2^n, including the main parent process


int main()
   printf("\n Hello\n");
  • For the above call total child process created is 2^3 -1 =7.
  • The first fork will result in 1 child process and both child and parent will execute the next fork, thus 2 more child process, and then all 4(two child and 2 parents) will call the last fork thus 4 child created. Hence total of 7 child process is created and
  • Eight times hello will be printed.

Internal working of fork():

  • Firstly, it checks for the available kernel resources like memory or disk to complete the fork() successfully.
  • Kernel finds the slots in the process table and confirms the availability of slot to start the construction of the child process current context and confirms that there is not too many processes running and assigns the PID to the child process.

Related Posts:

Categories: Operating system (OS)

4 replies


  1. wait() and waitpid() API - Tech Access Info
  2. Index of Operating System - Tech Access
  3. Exec Family System Call - Tech Access
  4. Pipes - Tech Access

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: