The Wayback Machine - https://web.archive.org/web/20240926192756/https://www.geeksforgeeks.org/fork-system-call-in-operating-system/
Open In App

Fork System Call in Operating System

Last Updated : 07 Sep, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In many operating systems, the fork system call is an essential operation. The fork system call allows the creation of a new process. When a process calls the fork(), it duplicates itself, resulting in two processes running at the same time. The new process that is created is called a child process. It is a copy of the parent process. The fork system call is required for process creation and enables many important features such as parallel processing, multitasking, and the creation of complex process hierarchies.

It develops an entirely new process with a distinct execution setting. The new process has its own address space, and memory, and is a perfect duplicate of the caller process.

Basic Terminologies Used in Fork System Call in Operating System

  • Process: In an operating system, a process is an instance of a program that is currently running. It is a separate entity with its own memory, resources, CPU, I/O hardware, and files.
  • Parent Process: The process that uses the fork system call to start a new child process is referred to as the parent process. It acts as the parent process’s beginning point and can go on running after the fork.
  • Child Process: The newly generated process as a consequence of the fork system call is referred to as the child process. It has its own distinct process ID (PID), and memory, and is a duplicate of the parent process.
  • Process ID: A process ID (PID) is a special identification that the operating system assigns to each process.
  • Copy-on-Write: The fork system call makes use of the memory management strategy known as copy-on-write. Until one of them makes changes to the shared memory, it enables the parent and child processes to share the same physical memory. To preserve data integrity, a second copy is then made.
  • Return Value: The fork system call’s return value gives both the parent and child process information. It assists in handling mistakes during process formation and determining the execution route.

Process Creation

When the fork system call is used, the operating system completely copies the parent process to produce a new child process. The memory, open file descriptors, and other pertinent properties of the parent process are passed down to the child process. The child process, however, has a unique execution route and PID.

The copy-on-write method is used by the fork system call to maximize memory use. At first, the physical memory pages used by the parent and child processes are the same. To avoid unintentional changes, a separate copy is made whenever either process alters a shared memory page.

The return value of the fork call can be used by the parent to determine the execution path of the child process. If it returns 0 then it is executing the child process, if it returns -1 then there is some error; and if it returns some positive value, then it is the PID of the child process.

Fork() System call

Fork() System call

Advantages of Fork System Call

  • Creating new processes with the fork system call facilitates the running of several tasks concurrently within an operating system. The system’s efficiency and multitasking skills are improved by this concurrency.
  • Code reuse: The child process inherits an exact duplicate of the parent process, including every code segment, when the fork system call is used. By using existing code, this feature encourages code reuse and streamlines the creation of complicated programmes.
  • Memory Optimisation: When using the fork system call, the copy-on-write method optimises the use of memory. Initial memory overhead is minimised since parent and child processes share the same physical memory pages. Only when a process changes a shared memory page, improving memory efficiency, does copying take place.
  • Process isolation is achieved by giving each process started by the fork system call its own memory area and set of resources. System stability and security are improved because of this isolation, which prevents processes from interfering with one another.

Disadvantages of Fork System Call

  • Memory Overhead: The fork system call has memory overhead even with the copy-on-write optimisation. The parent process is first copied in its entirety, including all of its memory, which increases memory use.
  • Duplication of Resources: When a process forks, the child process duplicates all open file descriptors, network connections, and other resources. This duplication may waste resources and perhaps result in inefficiencies.
  • Communication complexity: The fork system call generates independent processes that can need to coordinate and communicate with one another. To enable data transmission across processes, interprocess communication methods, such as pipes or shared memory, must be built, which might add complexity.
  • Impact on System Performance: Forking a process duplicates memory allocation, resource management, and other system tasks. Performance of the system may be affected by this, particularly in situations where processes are often started and stopped.

Conclusion

In conclusion, the fork system call in operating systems offers a potent mechanism for process formation. It enables the execution of numerous tasks concurrently and the effective use of system resources by replicating the parent process. It’s crucial for the creation of reliable and effective operating systems to comprehend the complexities of the fork system call.

FAQs on Fork System Call

Q1: What is the fork system call?

Answer:

The fork system call creates a new process(i.e Child Process) by duplicating the existing process(i.e Parent Process)

Q2: How is the fork system call used?

Answer:

The fork system call is invoked in a program to create a child process.

Q3: What is the return value of the fork system call?

Answer:

It returns the process ID of the child to the parent and 0 to the child.

Q4: What is the purpose of using the fork system call?

Answer:

The fork call allows for the creation of a new process that can perform tasks independent of the parent process.



Similar Reads

Xv6 Operating System -adding a new system call
Prerequisite - Xv6 Operating System -add a user program In last post we got to know how to add user program in Xv6 Operating System. Now here you will see how to add new system call in Xv6 Operating System. A dding new system call to xv6: A system call is way for programs to interact with operating system. A computer program makes system call when
6 min read
Difference Between System Call and library call
For anyone dealing with computer programming especially when it comes to performance issues or dealing with operations systems, it is very important to know the difference between system calls and library calls. System calls can be said as a way through which programs can ask the Kernel of an operating system for a certain service on the other hand
7 min read
Code for Fork System Call in OS
Fork is a system call in the Operating System used to create a copy of the process, the copied process created is known as the child process. The main process or the process from which the child process is created is known as the parent process. The parent and child processes are in separate address spaces in fork(). A Child process will have its P
4 min read
Remote Procedure Call (RPC) in Operating System
Remote Procedure Call (RPC) is a powerful technique for constructing distributed, client-server based applications. It is based on extending the conventional local procedure calling so that the called procedure need not exist in the same address space as the calling procedure. The two processes may be on the same system, or they may be on different
8 min read
Fork Function Call
In this article, we are going to see the fork function call in detail with the help of an example. A function call is an operating system call that is used to create a copy of a process which is also called a child process. This function call is used in a system that supports multitasking. Purpose of Fork()At its core, a function call is a fundamen
3 min read
System Protection in Operating System
Introduction: System protection in an operating system refers to the mechanisms implemented by the operating system to ensure the security and integrity of the system. System protection involves various techniques to prevent unauthorized access, misuse, or modification of the operating system and its resources. There are several ways in which an op
3 min read
User View Vs Hardware View Vs System View of Operating System
User View of Operating System: The Operating System is an interface, hides the details which must be performed and present a virtual machine to the user that makes it easier to use. Operating System provides the following services to the user. Execution of a programAccess to I/O devicesControlled access to filesError detection (Hardware failures, a
1 min read
Traps and System Calls in Operating System (OS)
Traps and system calls are two mechanisms used by an operating system (OS) to perform privileged operations and interact with user-level programs. Here is an overview of each mechanism: Traps: A trap is an interrupt generated by the CPU when a user-level program attempts to execute a privileged instruction or encounters an error. When a trap occurs
3 min read
System Programs in Operating System
System Programming can be defined as the act of building Systems Software using System Programming Languages. According to Computer Hierarchy, Hardware comes first then is Operating System, System Programs, and finally Application Programs. Program Development and Execution can be done conveniently in System Programs. Some of the System Programs ar
3 min read
File System Implementation in Operating System
A file is a collection of related information. The file system resides on secondary storage and provides efficient and convenient access to the disk by allowing data to be stored, located, and retrieved. File system implementation in an operating system refers to how the file system manages the storage and retrieval of data on a physical storage de
11 min read
Difference between System Software and Operating System
Software is like a set of instructions that tell a computer what to do. There are two main types application software and system software. Application software is extra programs you can add to your computer to do specific things, like write documents or play games. You don't need these to use your computer, but they help you do particular tasks. Sy
5 min read
Implementation of sleep (system call) in OS
In this article, we are going to learn about sleep (system call) in operating systems. In the computer science field, a system call is a mechanism that provides the interface between a process and the operating system. In simple terms, it is basically a method in which a computer program requests a service from the kernel of the operating system. W
4 min read
Linux system call in Detail
A system call is a procedure that provides the interface between a process and the operating system. It is the way by which a computer program requests a service from the kernel of the operating system. Different operating systems execute different system calls. In Linux, making a system call involves transferring control from unprivileged user mod
4 min read
Difference between system() and execl() call
A system() call is a way for the programs to interact with the operating system. It is used to execute a command within a process. The system call provides the services of the operating system to the user programs via Application Program Interface (API). It provides an interface between a process and an operating system and it does not replace the
4 min read
What is System Call Interposition?
SystÐĩm calls IntÐĩrposition is a valuablÐĩ approach that allows intÐĩrcÐĩption and modification of systÐĩm calls madÐĩ in thÐĩ opÐĩrating systÐĩm. This tÐĩchniquÐĩ makÐĩs it ÐĩasiÐĩr to ÐĩnforcÐĩ finÐĩ-grainÐĩd sÐĩcurity policiÐĩs and intÐĩgratÐĩ nÐĩw capabilitiÐĩs, rÐĩsulting in a morÐĩ flÐĩxiblÐĩ and adaptablÐĩ sÐĩcurity modÐĩl. While it may not be thÐĩ ultimatÐĩ solution for sy
4 min read
Introduction of System Call
A system call is a programmatic way in which a computer program requests a service from the kernel of the operating system it is executed on. A system call is a way for programs to interact with the operating system. A computer program makes a system call when it requests the operating system's kernel. System call provides the services of the opera
11 min read
Banker's Algorithm in Operating System
Prerequisite - Resource Allocation Graph (RAG), Banker’s Algorithm, Program for Banker’s Algorithm Banker's Algorithm is a resource allocation and deadlock avoidance algorithm. This algorithm test for safety simulating the allocation for predetermined maximum possible amounts of all resources, then makes an “s-state” check to test for possible acti
15 min read
Swap Space in Operating System
A computer has a sufficient amount of physical memory but most of the time we need more so we swap some memory on disk. Swap space is a space on a hard disk that is a substitute for physical memory. It is used as virtual memory which contains process memory images. Whenever our computer runs short of physical memory it uses its virtual memory and s
6 min read
Thread Models in Operating System
A thread is a light weight process which is similar to a process where every process can have one or more threads. Each thread contains a Stack and a Thread Control Block. There are four basic thread models : 1. User Level Single Thread Model : Each process contains a single thread.Single process is itself a single thread.process table contains an
2 min read
The Tempo Operating System
Operating system : An Operating system is an interface between the computer user and computer hardware. An Operating system is a software which performs all the basic task like file handling, file management, memory management, handling input and output, controlling peripherals devices such as disk drive and printer, mouse...etc Tempo operating sys
5 min read
Boot Block in Operating System
Basically for a computer to start running to get an instance when it is powered up or rebooted it need to have an initial program to run. And this initial program which is known as bootstrap needs to be simple. It must initialize all aspects of the system, from CPU registers to device controllers and the contents of the main memory, and then starts
5 min read
Comparison on using Java for an Operating System instead of C
Java is a managed language which offers memory safety. In Java, pointers do not exist, so we cannot do pointer arithmetic on a function pointer. An application can invoke a method defined in a class through the class object. In Java, we cannot do unsafe typecast of an object to overwrite the method pointer with something else. An out of bound array
2 min read
Difference between Loading and Linking in Operating System
An operating system acts as an intermediary between the user of a computer and computer hardware. The purpose of an operating system is to provide an environment in which a user can execute programs conveniently and efficiently. Linking and Loading are utility programs that play an important role in the execution of a program. Linking intakes the o
4 min read
Stack Implementation in Operating System uses by Processor
A stack is an associate ordered a set of components, only one of that (last added) are often accessed at a time. The point of access is named the highest of the stack. The number of components within the stack, or length of the stack, is variable. Items could solely be side to or deleted from the highest of the stack. For this reason, a stack is ad
4 min read
Best-Fit Allocation in Operating System
INTRODUCTION: Best-Fit Allocation is a memory allocation technique used in operating systems to allocate memory to a process. In Best-Fit, the operating system searches through the list of free blocks of memory to find the block that is closest in size to the memory request from the process. Once a suitable block is found, the operating system spli
4 min read
Thread Control Block in Operating System
Very similar to Process Control Blocks (PCBs) which represents processes, Thread Control Blocks (TCBs) represents threads generated in the system. It contains information about the threads, such as it's ID and states. The components have been defined below: Thread ID: It is a unique identifier assigned by the Operating System to the thread when it
1 min read
Two State Process Model in Operating System
Pre-requisite: States of a Process in Operating Systems The process in an operating system passes from different states starting from its formation to its completion. A process consists of program data and its associated data and a process control block (PCB). A process may change its state because of the following events like I/O requests, interru
3 min read
I/O Hardware in Operating System
I/O Hardware is a set of specialized hardware devices that help the operating system access disk drives, printers, and other peripherals. These devices are located inside the motherboard and connected to the processor using a bus. They often have specialized controllers that allow them to quickly respond to requests from software running on top of
6 min read
Completely Fair Queuing (CFQ) in Operating System
Several Linux I/O schedulers have been proposed. Some of these are: Completely Fair Queuing (CFQ) Deadline NOOP Anticipatory CFQ is one of the input/output scheduler for the Linux kernel and is the current default scheduler in the Linux kernel. What is kernel ? Kernel is the central part of an operating system. It manages the operation between the
2 min read
Concurrent Processes in Operating System
Concurrent processing is a computing model in which multiple processors execute instructions simultaneously for better performance. Concurrent means, which occurs when something else happens. The tasks are broken into subtypes, which are then assigned to different processors to perform simultaneously, sequentially instead, as they would have to be
2 min read