0
0
Operating Systemsknowledge~6 mins

Process creation (fork and exec) in Operating Systems - Full Explanation

Choose your learning style9 modes available
Introduction
Imagine you want your computer to start a new task, like opening a program. The system needs a way to create a new process that can run this task separately. This is where process creation comes in, using two main steps: making a copy of an existing process and then replacing it with a new program.
Explanation
Fork: Creating a copy
The fork operation makes a new process by copying the current one. This new process is almost identical to the original, including its code and data. Both processes continue running independently after the fork, but they have different process IDs.
Fork creates a new process by duplicating the current one.
Exec: Running a new program
After fork, the new process often needs to run a different program. The exec operation replaces the current process's code and data with a new program loaded from disk. This means the process keeps its ID but changes what it is doing.
Exec replaces a process's program with a new one.
Why use fork and exec together?
Using fork and exec together allows the system to create a new process and then run a different program in it. This two-step method gives control over the new process before it starts the new program, such as setting up resources or environment.
Fork and exec combined let a process create and run a new program.
Real World Analogy

Imagine you have a recipe book and want to make a new dish. First, you make a photocopy of the recipe (fork), so you have your own copy to work on. Then, you erase the copied recipe and write a new one on it (exec), so your copy now has a different recipe to follow.

Fork: Creating a copy → Making a photocopy of the recipe book page
Exec: Running a new program → Erasing the copied recipe and writing a new one
Why use fork and exec together? → Copying the recipe first, then changing it to a new recipe
Diagram
Diagram
┌─────────────┐        fork        ┌─────────────┐
│ Parent Proc │ ───────────────▶ │ Child Proc  │
│ (Program A) │                  │ (Program A) │
└─────────────┘                  └─────────────┘
                                   │
                                   │ exec
                                   ▼
                             ┌─────────────┐
                             │ Child Proc  │
                             │ (Program B) │
                             └─────────────┘
This diagram shows a parent process creating a child process with fork, then the child process replacing its program with exec.
Key Facts
ProcessA running instance of a program with its own memory and resources.
ForkAn operation that creates a new process by copying an existing one.
ExecAn operation that replaces a process's program with a new program.
Process ID (PID)A unique number identifying a process in the system.
Parent processThe original process that creates a new child process.
Child processThe new process created by fork, initially a copy of the parent.
Code Example
Operating Systems
import os

pid = os.fork()
if pid == 0:
    # Child process
    os.execvp('ls', ['ls', '-l'])
else:
    # Parent process
    print(f"Parent process, child PID: {pid}")
OutputSuccess
Common Confusions
Fork creates a completely new program from scratch.
Fork creates a completely new program from scratch. Fork copies the existing process; it does not load a new program until exec is called.
Exec creates a new process.
Exec creates a new process. Exec replaces the program in the current process; it does not create a new process.
Parent and child processes share the same memory after fork.
Parent and child processes share the same memory after fork. They start as copies but have separate memory spaces; changes in one do not affect the other.
Summary
Fork creates a new process by copying the current one, giving it a unique ID.
Exec replaces the program in a process with a new program, changing what it runs.
Together, fork and exec let the system start new programs in separate processes.