What is fork in OS?

Fork in an operating system is a system call used to create a new process, called a child process, by duplicating the current process, known as the parent process. This mechanism is fundamental in multitasking systems, allowing multiple processes to run concurrently.

Real-World Example

Imagine a chef preparing a dish. To speed things up, they duplicate themselves (fork) so one chef continues cooking while the other prepares the next step. Similarly, the parent process "clones" itself to divide tasks between parent and child.

Key Features of Fork

  1. Process Duplication: The child process is a copy of the parent, inheriting its code, data, and open file descriptors.
  2. Unique Process IDs: The parent and child processes have distinct process IDs (PIDs).
  3. Execution Paths: After the fork, the parent and child processes can execute different instructions.

Fork System Call Syntax (in C)

pid_t pid = fork();

How Fork Works

  • Parent Process: The process that calls fork().
  • Child Process: A duplicate created by the fork() call.
  • Return Value:
    • 0: Returned to the child process.
    • Positive Value: The PID of the child returned to the parent process.
    • Negative Value: Indicates an error in creating the child process.

Example of Fork Usage

#include <stdio.h> #include <unistd.h> int main() { pid_t pid = fork(); if (pid == 0) { printf("Child process\n"); } else if (pid > 0) { printf("Parent process\n"); } else { printf("Fork failed\n"); } return 0; }

Output depends on whether the code is executed by the parent or child process.

Benefits of Fork

  1. Process Creation: Allows creation of multiple processes for multitasking.
  2. Code Inheritance: The child process inherits the parent's code and resources, reducing initialization overhead.
  3. Foundation for Exec: Combined with the exec() system call, fork() enables running a new program in the child process.

Limitations of Fork

  1. Resource Intensive: Creating a new process can consume significant system resources.
  2. Copy Overhead: Copying process memory (even with optimizations like copy-on-write) can impact performance.
  3. Error Handling: A failed fork() can disrupt process flow, requiring robust error handling.

Applications of Fork

  • Multitasking: Running multiple tasks or processes in parallel.
  • Process Isolation: Creating isolated environments for tasks.
  • Server Programming: Handling multiple client connections (e.g., in web servers).

Understanding fork() is essential for systems programming and working with process management in operating systems. For a deeper dive into process creation and management, explore System Design Primer The Ultimate Guide. Fork forms the backbone of multitasking in Unix-like systems.

TAGS
Coding Interview
System Design Interview
CONTRIBUTOR
Arslan Ahmad
Arslan Ahmad
ex-FAANG engineering manager and author or Grokking series.
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
How do I introduce myself in Google interview?
Why are coding interviews so hard?
What type of engineers work at Intel?
What questions does Uber ask?
What is the interview process at LinkedIn?
What is the salary of a PayPal full stack developer?
Related Courses
New
Grokking the AI System Design Interview course cover
Grokking the AI System Design Interview
Learn to design AI systems the way interviewers expect: classic ML products, LLM and RAG architectures, and agentic systems, all through the lens of the system design interview.
4.8
(1,192 learners)
Discounted price for Your Region

$123

Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Grokking Modern AI Fundamentals course cover
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
4.1
Discounted price for Your Region

$72

Design Gurus logo
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.