Fork System Call in Operating System

Fork System Call in Operating System

Introduction:

fork system call is an essential operation. It creates a new process. When a process calls the fork(), it is running in two processes at the same time. The new process that is created is called a child process which is a copy of the parent process. The fork system call is to create a process. It enables parallel processing, multitasking, and the creation of complex process hierarchies.

It creates a new process with a distinct execution setting. The new process has its own address space and memory and it is a perfect duplicate of the caller process.

Simply, a fork system call is a method used for creating a new process, which is a copy of the calling (parent) process. The new process is referred to as the child process, and it inherits various attributes and resources from the parent process.

The fork system call is used to create a new program. Creates a copy of the calling process, including program code, memory, open files, and other services. Once the fork is called, the two processes (parent and child) run independently.

fork system call
fork system call

Process

A process is an instance of a program running in the system. It includes the execution process, its associated data, and resources such as memory and data description.

Parent Process

The process that calls the fork system call is called the parent process. It creates a copy of itself, called a child process. Process of fork() system call to start a new child process.

Child Process

The newly created process is known as the child process. It is a copy of the parent process and inherits various attributes and resources from the parent. It has its own PID(Process ID), and memory, and is a copy of the parent process.

Process ID:

A unique identification that the operating system assigns to each process. The PID helps identify and manage processes in the system.

Return Value

The fork system call returns different values in the parent and child processes. In the parent process, it returns the PID of the child process, while in the child process, it returns 0. In case of an error, it returns-1.

Process Tree

The set of processes in a system organized in a hierarchical structure, with a single root process (init or systemd in Unix-like systems) at the top, and parent-child relationships between processes.

Parent-Child Relationship

After calling, the parent process maintains its relationship with its child program. A parent process can usually monitor the execution of a child process, receive notification of its termination, and manage its resources.

Copy-on-Write (COW)

Fork system calls use a memory management strategy called copy-on-write. It causes parent and child processes to share the same physical memory until one of them switches to shared memory. To ensure the integrity of the data, we then make a second copy..

Execution Continues

After the fork system call, both the parent and child processes continue execution independently. They can execute different sections of code or perform different tasks.

fork() normally return the three values:

Negative value – creation of the child process was unsuccessful.
Zero – A new child process is created.
Positive value – the process ID of the child process, to the parent. The function returns a process ID of type pid_t, which is defined in sys/types.h. Normally, the process ID is an integer. Moreover, a process can use the function getpid() to retrieve the process ID assigned to this process.
Since the child and parent processes run concurrently, these processes reside in different memory spaces.

#include <stdio.h>
#include <unistd.h>
int main() {
pid_t pid;
pid = fork();
if (pid < 0) {                            //if return negetive values
fprintf(stderr, "Fork failed\n");         
return 1;
} else if (pid == 0) {                    // if return zero     
// Child process
printf("Child process is running\n");   
} else {                                 // if return positive value   
// Parent process
printf("Parent process is running\n");
}
return 0;
}

Examples

Code

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main() // main function
{ 
fork();
fork();
fork();
printf("The fork() system call creates a process\n");
return 0;
}

Result

The fork() system call creates a process

The fork() system call creates a process

The fork() system call creates a process

The fork() system call creates a process

The fork() system call creates a process

The fork() system call creates a process

The fork() system call creates a process

The fork() system call creates a process

Total Process Created here  2^3=8 it will execute 8 times

Advantages of fork() System Call

Multitask Process Creation:
Fork creates new processes within an operating system. This is a multitasking process, where multiple processes can run concurrently, performing different tasks. This improves system efficiency and concurrency.
Parallel Execution
In a fork system called a parent process, multiple child processes, each of can be executed independently and concurrently system process.
Code reusability
When using a fork call, the program copies the parent process, including all child processes.. This feature encourages code reuse and makes it easier to create complex programs using existing code.

Memory Optimization: 
The copy-on-write method optimizes memory usage when using fork () system calls. Startup memory will be minimized since parent and child processes share the same physical memory page. The process only prints when it swaps memory pages to improve memory.

Process Isolation

When a fork creates a new process, it also detaches the process. Each process runs independently of other processes and has its own memory space and execution details. This isolation increases the stability and security of the system because an error or failure in one process usually does not affect other processes.

Fault Tolerance

fork() creates a child process that can handle critical tasks and the parent process can continue unaffected by the child’s error. If an error occurs in a child process, the system can terminate or restart it without impacting the parent process or other processes.

Dynamic Process Creation

Fork() allows the creation of dynamic processes that can create new processes based on runtime or user input.

Process Hierarchies: A parent process can create multiple levels of child processes.

Disadvantages of fork() System Call

Resource intensive: 

Forking a process will copy the entire address of the parent process, including memory pages, file descriptors, and other resources. This can consume a lot of resources, especially for processes with large memory.

Copy-on-Write Overhead(Memory Overhead)

Even with Copy-on-Write optimization, the fork system presents memory overhead. It copies the entire parent process, including all of its memory, thus increasing memory usage

Resources Duplication:

When a process forks, the child process copies all open files, network connections,  and other resources. This duplication can waste resources and be ineffective.

Relationship Overhead:

Maintaining parent-child relationships between processes can incur overhead in terms of process management and synchronization.

Online Playlists

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply

    Your email address will not be published. Required fields are marked *