Code for Fork System Call in OS Last Updated : 13 Oct, 2023 Comments Improve Suggest changes Like Article Like Report 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 Process ID different from the parent's ID. Primary Terminologies related to Fork System Call in OSProcess: The task or job performed by the computer. Example: running a program. System call: A system call is a way to shift between user mode and kernel node to access the Functionalities of Operating Systems.Child process: The child process is the new process that is created by the parent process. It is essentially a copy of the parent process, and A system inherits many of its attributes, such as code, data, and open files.Parent process: The parent process is the original process that initiates the creation of a new process. It serves as the starting point for the new processProcess ID (PID): Process ID is a special identification that OS assigns to every process. It returns an integer value.We use fork() one time in a parent (P) then a child process is created (C1) and there is also a parent process (P) present. Similarly, if we write fork again, here C1 becomes the parent and creates a new child process C2 and there is a parent process (C1) available. P also creates a child process (C3) and P is also present (P). Example 1: Fork system call in OS Example 2: Fork system call in OS By observing, we can conclude:Total no. of child process generated will be: 2^n - 1.Total no of processes executed: 2^n. Here n is the number of times forks called. Remember that child processes return 0, while parent processes return a positive value (probably +1) at the fork call. Examples of Fork System call in OS1. Simple Fork: C #include <stdio.h> #include <unistd.h> int main() { pid_t child_pid; // Create a child process child_pid = fork(); if (child_pid == 0) { printf("Child process\n"); } else if (child_pid > 0) { printf("Parent process\n"); } else { perror("Fork failed"); } return 0; } Output: Fork example in linux OS2. Parent and Child Processes Printing with PIDs: C #include <stdio.h> #include <unistd.h> int main() { pid_t child_pid; // Create a child process child_pid = fork(); if (child_pid == 0) { printf("Child process: PID=%d\n", getpid()); } else if (child_pid > 0) { printf("Parent process: PID=%d, Child PID=%d\n", getpid(), child_pid); } else { perror("Fork failed"); } return 0; } Output: Fork example in linux OS3. Forking Multiple Child Processes: C #include <stdio.h> #include <unistd.h> int main() { int i; for (i = 0; i < 3; i++) { pid_t child_pid = fork(); if (child_pid == 0) { printf("Child process %d: PID=%d\n", i + 1, getpid()); break; // Each child process should exit the loop } else if (child_pid < 0) { perror("Fork failed"); } } if (getpid() != 1) { // This code is executed only by the parent process printf("Parent process: PID=%d\n", getpid()); } return 0; } Output: Fork example in linux OSConclusionfork() system call is the fundamental tool that allows processes to duplicate themselves. It makes a copy of process in a separate memory space, it provides both child and parents a different process ID (PIDs) for identification. The uses of fork are multitasking, parallel processing and complex process structure. Comment More infoAdvertise with us Next Article Code for Fork System Call in OS shnwaz_solves Follow Improve Article Tags : Operating Systems Geeks Premier League Geeks Premier League 2023 Similar Reads Fork System Call in Operating System 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. 5 min read Different Types of System Calls in OS System calls are interfaces provisioned by the operating system to allow user-level applications to interact with low-level hardware components & make use of all the services provided by the kernel, which is a core component and the heart of an operating system that manages all the hardware and 10 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 involv 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 on which it is executed. 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' 11 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 4 min read OS File System Architecture An operating system (OS) is software that manages computer hardware and software resources and provides related services to computers. It acts as an intermediary between applications and computer hardware. What is a Document System?A file system is a method that helps in organizing and managing data 5 min read Create Processes with Fork in C++ fork() is a system call that creates a child process from the parent process. Whenever we call fork() from the parent program, a child process is created that has the exact copy of the address space. The important thing to remember is it shares the copy of the address space, not the copy itself. Syn 3 min read Critical Regions in Operating System In an operating system, a critical region refers to a section of code or a data structure that must be accessed exclusively by one method or thread at a time. Critical regions are utilized to prevent concurrent entry to shared sources, along with variables, information structures, or devices, that a 3 min read Components of Operating System An Operating system is an interface between users and the hardware of a computer system. It is a system software that is viewed as an organized collection of software consisting of procedures and functions, providing an environment for the execution of programs. The operating system manages system s 7 min read Operating Systems | Set 5 Following questions have been asked in GATE 2012 exam. 1. A process executes the code fork (); fork (); fork (); The total number of child processes created is (A) 3 (B) 4 (C) 7 (D) 8 Answer (C) Let us put some label names for the three lines fork (); // Line 1 fork (); // Line 2 fork (); // Line 3 3 min read Like