Fork System Call

Learn via video courses
Topics Covered

Overview

In an operating system, New processes are created using the fork() system call. It returns a process ID and does not accept any parameters. A new process (child process) is created by the fork system call that runs concurrently with the parent process.

What is a Fork System Call in OS?

A new process known as a "child process" is created with the fork system call which runs concurrently with the process called the parent process. fork system call in OS returns an integer value and requires no arguments. After the creation of a new child process, both processes then execute the next command following the fork system call. Therefore, we must separate the parent from the child by checking the returned value of the fork():

  • Negative: A child process could not be successfully created if the fork() returns a negative value.
  • Zero: A new child process is successfully created if the fork() returns a zero.
  • Positive: The positive value is the process ID of a child's process to the parent. The process ID is the type of pid_t that is defined in sys/types.h.

However, the parent and child processes are stored in different memory locations. These memory spaces contain the same information, therefore any operations carried out by one process do not affect the other.

A child process uses the same open files, CPU registers, and the same pc(program counter) used in the parent process.

What is the Use of Fork System Call?

The use of the fork() system call is to create a new process by duplicating the calling process. The fork() system call is made by the parent process, and if it is successful, a child process is created.

Use of Fork System Call

The fork() system call does not accept any parameters. It simply creates a child process and returns the process ID. If a fork() call is successful :

  • The OS will create two identical copies of the address space for parent and child processes. Therefore, the address spaces of the parent and child processes are different.
  • A global variable is:
    • Created when the process starts
    • Declared outside the process
    • Lost when the program stops
  • A local variable is:
    • Created as the process starts
    • Declared inside the process
    • Lost when the process ends
  • The new child process's process ID, or PID, is returned to the parent process. If something goes wrong, the parent process returns -1.
  • Zero is returned to the new child process. (In the case of failure, the child process is not created).

Syntax for fork() System Call

In Linux and Ubuntu, the fork() system call has the following syntax:

In the syntax, fork() does not take any arguments, and its returned type is pid_t. Whenever a child process is successfully created, its PID (Process ID) is returned to the parent process, and the 0 value is returned to the child process itself.

-1 is returned to the parent process in the case of any errors, and no child process is created.

Examples For fork() System Call

Let’s take a few examples for a better understanding of the fork() system call:

Example 1: Calling fork()

Take a look at the example below, where the fork() system call is used to create a new child process:

Output:

Explanation :

  • In this program, We've used the fork() function, which will create a new child process.
  • Once the child process has been created, The child process and the parent process will both point to the next command.
  • In this manner, the remaining commands will be executed 2n2^n times, where n represents the number of fork() system calls.

Consider the following scenario, where fork() is called multiple times:

Output :

Example 2: Testing if fork() was Successful

In the example below, we have checked the integer value returned by fork() using the decision-making construct.

Output :

Explanation :

  • p is the variable of type pid_t which stores the integer value returned by fork().
  • When fork() is called and a child is successfully created, the variable p becomes 0.
  • while p holds any positive value which is for the parent process as the parent process will get the child process's id returned.

Example 3: Multiple Child Processes Using fork() and Loop :

In the example below, We used a for loop to create 4 child processes. The PID and PPID from the child processes were also printed.

Output :

Explanation :

  • Since they all belong to the same parent as we can see, all of the child processes have the same Parent process ID in the output.

Example 4: Real-Life Example

We've created a 4-digit PIN code in the following example and sent the generated code to the parent process from a child process.

Output :

Explanation :

  • Instead of doing mathematical computations like md5, sha256, etc. within the main program's process, the hash can simply be calculated on a child process and returned to the main process.
  • In a child process, a 4-digit PIN code was created and sent to the parent process, the main application.
  • Then, we can print the pin from the main process, as a result, we can see that every time we run the application, we receive a unique 4-digit PIN code.

FAQs

Q. What do you mean by system call?

A. System calls are unique functions that manage OS operations in kernel mode.

Q. What is kernel mode?

A. Kernel mode is an operating mode for central processing units (CPU).

Q. Which header file defines the "fork()" function?

A. In the header file "unistd.h", fork() is defined.

Q. Which process will execute the statement

A. following the fork() call—the parent or the child? Both the parent and the child processes are run at the same time, and either of them could get control first from the OS.

Q. What does the fork() function's negative value denote?

A. It denotes that the creation of the child process was unsuccessful.

Q. What are the differences between the exec() and fork() functions?

A.

fork()exec()
The fork() creates a new process that is an identical copy of the original process.The exec() creates a new process in place of the original process.
Both the parent and the child processes are run at the same time.Unless there is an error, control never goes back to the original process.
Parent and child processes are in different address spaces.parent address space is replaced by the child address space.

Conclusion

  • A new process known as a “child process” is created with the fork system call which runs concurrently with the process called the parent process.
  • The parent process and the child process must be separated by checking the returned value of the fork().
  • A child process uses the same CPU registers, the same open files, and the same pc(program counter) used in the parent process.
  • In the syntax, fork() does not take any arguments, and its returned type is pid_t.
  • The commands will be executed 2n times, which is the total number of processes, where n represents the number of fork() system calls.
  • In real-life, we can create some unique codes with the fork() system call as every child process will give a unique value.