Fork System Call Linux
Published: 2023-03-01 12:39:18

Operating system; a conduit between a computer’s hardware and the user! 

Operating system is touted as the software piece that coordinates with the execution of certain applications, software or computer hardware. It also aids in a variety of other versatile tasks like memory management, file management, resource management, deadlock detection in OS and much more! 

As an operating system unfolds a variety of concepts, it also helps in the creation of new processes with the help of **[fork system call](https://www.codingninjas.com/codestudio/library/fork-system-call)**. 

This is the concept which helps in returning a process ID by not accepting any kind of parameters. A new process or a child process will be created with a fork system call to run in parallel with the parent processes. 

Though, there is much more to this concept which you can learn with the help of this tutorial.

So without any further ado, let’s get started!

What is the fork system call?

A system call is generally defined as the special function which helps in the management of the operating system routines in the presentable Kernel mode.

This mode is also known as the system mode with the presentable CPU mode. While processes may run in your kernel mode, they can have some kind of unrestricted access with the hardware. 

Fork system call is a part of the operating system which helps in the creation of a new process known as “child process”. This process runs concurrently with the already existing parent process. 

The fork call in an operating system returns the value of an integer with no requirement of any argument. After the creation of the new child process in your operating system both of the processes will execute the next command which follows your fork system call.

Hence, we need to separate the parent from its child while checking its return value as folk():

Negative: The child process will not be created successfully if the folk() return value would be negative
Zero: The new child process will be created successfully if zero would be the value of the folk() return
Positive: The positive value will be the process ID in a child process with the parent. This process ID will be the type of the pid_t id.

Though, both the children and the parent processes will get stored inside of the different memory locations. These memory spaces will contain the same type of information. So any operation that will be carried by the one process will not affect any other process.

A child process is basically touted to use the same CPU registers or the open files with the same PC used in the parent process. 

Properties of the child process:

In the fork system call, your child process will hold the following properties:

CPU counter and its resource utilisation will be initialised to its zero
When any parent process gets terminated, its child process will not receive any kind of signal
Thread which is used to create the fork () system call may be created in a child process. The address would be same like the parent process
A file descriptor in your parent process will automatically get inherited by the child process
Any open queue descriptor will be inherited with your parent process
The slack value of a cold process would be same as that of the child value of your parent process 

Properties that may not be inherited by your child process are:

Its memory locks 
The pending relatable signals in your child lock will be empty 
Process would generally be related to the record locks 
There will be asynchronous input and output operations with input and output content in the system
alarms() or other kind of timers will not be inherited by your child class 


Uses of Fork system call 

The basic use of a fork system call would be to create all of the new processes with the help of duplication of the calling processes. As the fork() system is generally made of varied parent’s processes, if the system becomes successful, the new child process will be created.

Though, the fork() system call may not accept any kind of additional parameter, it can simply create the child process in order to return the process ID. 

If the process becomes successful:


The operating system will first generate two kinds of identical copies of the parent and child processes in an address space. Though, the address spaces for both of the child or the parent process will be different 
The global variable will be created at the starting of the process. It can also be created outside of the process. Though, it gets lost when a program stops
The local variable will be created at the start of your process. It can be declared inside of the process and will be lost when your process ends 
The new ID of a process child will be returned in the parent process. Though, if anything will go wrong, the value of -1 will be returned as the parent process value
Zero value will be returned to its next child process. [ but in case of any failure, the child process will not be created] 



Fork() system call syntax 

In Linux, the syntax of your fork() system call will be;

Pid_t fork [ void];

In this syntax, the fork() may not take or return any kind of argument. The return type pid_t will be created when the child process will get created successfully. 

The process ID will get returned in case of the parent process. Its value 0 will be returned to its child process. Though the value -1 will be returned to its parent process. But in case of errors, there will not be any child process.

**Wrapping up **

Operating systems are a wide arena with a bundle of concepts like file management, resource management, **[deadlock detection in OS](https://www.codingninjas.com/codestudio/library/deadlock-detection-and-recovery)** among others!

In this guide we have explained in-depth about the fork system call which forms a crucial part of the operating system. 

Learn it, implement it, ace it!