C Fork() Function

quiz
Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Learn via video course

Free C++ Course: Learn the Essentials
Free C++ Course: Learn the Essentials
By Prateek Narang
Free
star5
Enrolled: 26844
Free C++ Course: Learn the Essentials
Free C++ Course: Learn the Essentials
Prateek Narang
Free
5
icon_usercirclecheck-01Enrolled: 26844
Start Learning

Overview

The c fork function is used for creating the duplicate of the calling process. When a process gets duplicated, it forms two types of processes. The first one is the parent process from which a duplicate process has formed and the second one is the child process that formed as a result of the duplication.

What is C fork() Function?

Below are the pre-requisites to understanding the C fork()-

  1. Process
  2. Different Stages of a process.

Forking is the process of creating of duplicate process from an existing process. The duplication of the process takes place when the C fork() is called. After the forking, there are two types of process there. The first one is the Parent process from which the duplicate processes are formed. And the second one is the child process. This is the duplicated process that forms from the parent process.

The C fork() function does not have any parameters and this function returns an integer value as the returned value. Given below are the various values that are returned by the C fork() function.

Negative Value

The C fork() function returns a negative value when the function fails to create a child process.

Zero Value

The C fork() function returns a zero value to the child process that is newly created. On successful duplication of a process, the PID of the child process is returned in the parent, and 0 is returned in the child process.

Positive Value

The C fork() function returns a positive value to the parent process or the caller. The positive value consists of the process ID of that particular child process that is being created.

Syntax of C fork() Function

The syntax of the C fork() function is as follows:

Parameters of C fork() Function

The C fork() function does not have any parameters.

Return Value of C fork Function

The C fork() function returns three integers as the return value. The three integers are like a positive value (it contains the process ID), a negative value (when the child process fails), or a zero (when the child process is created successfully).

How does the fork() Function in C work?

The C fork() function is a primary method of process creation of an operating system like Unix. The fork() is used for creating a new copy of the calling function. The newly created process is known as the Child process and the process from which the child process is created is known as the parent process. When there is closing or any unwanted avoidance in the parent function, then the child process also got crashed. Since the C fork() function is based on Threading so for getting the correct output, we should run the program on a local system.

Let us take an example to understand the working of the C fork() function. We will use the C fork() function to print the hello world many times.

Output:

In the above example, one output is the parent process and the other one is the child process.

output-fork-function

  • In the above example, the number of times the C fork() function is used is 1. So the process will be forked in the form of 2 power of n. The value of n represents the number of fork() system calls.

Now, we will use the fork() function three times for calling the process. Then the resultant output will be like 2 power of 3 is equal to 8.

Output:

Uses of fork() Function

The work of the C fork() function is to create a child process for the parent process. When we use the C fork() function in our program, we simply create a duplicate of that particular calling function. This function creates the child process in the form of 2 power of n, where n represents the number of calls that the function does.

C fork Examples

Now let us see some more examples of the C fork() function to understand this function in a better way. Each of the examples is provided with output and explanation.

Example 1

Output

Explanation

In the above example, we used the fork() function to create a child process from the main process. From the child and parent process, we printed the process ID and the parent ID named PID and PPID respectively. We used the wait (NULL) on the parent process for waiting for the child process to be get finished. Then we use the exit() on the child process to get finished the child process. In the above code, we can see that the PID of the parent process is the PPID of the child process. From this, we can conclude that process 24738 is the child process of process 24731

Example 2

Output

Explanation

In the above example, the process id of the parent function is 1252. This id is also the process id of the main function. Then we created the fork. The process id 1253 represents the creation of a fork. This process will help to print the output and after printing the output, the child process will be assigned a new id. After this, the process id 1252 will also be used for printing the content under the fork section. In this, there is the process id of the parent and child process which is the same. So, we can conclude that the new child process belongs to the earlier parent process.

Example 3

Output

Explanation

In the above example, we have we are using the loop to for doing the fork function many times. First, we printed the natural numbers from 1 to 10, and then we used the C fork() function to print the process two times. The numbers printed the first time are the parent process and the numbers that are printed again are the child process.

Conclusion

  • The c fork function is used for creating the duplicate of a calling process.
  • When a process is duplicated, there are two types of process forms that are the first is parent process and the second one is child process.
  • The process can be defined as the series of actions that are done to execute a program. When a program is run, it is not directly gets executed.
  • The syntax of the C fork() function is variable_name = fork();
  • The C fork() function does not have any parameter and this function returns three values that are a negative value, a positive value, or a zero, according to the action done by the process behavior.
  • The C fork() process will be forked in the form of 2 power of n and the value of n represents the number of fork() system calls.