Operating Systems · Processes · GATE CSE

From One Processto Many

Master fork() by tracing execution paths, not memorising shortcuts. Build process trees, follow parent and child branches, handle loops and conditions, and solve the patterns that actually appear in GATE.

Interactive tracingGATE fork() PYQsBeyond-GATE practice
fork();
fork();
fork();
1process before any fork
P0
01

fork() in 60 seconds.

Before process-count formulas, understand what one successful call actually does.

fork() creates a new process by duplicating the calling process. The original is the parent; the new process is the child. Both continue from the instruction immediately after fork().

Parent

fork() returns > 0

The positive value identifies the child PID.

Child

fork() returns 0

The new child can identify its own path from this return value.

The universal mental modelDo not count fork() statements. Count the processes that reach each fork().
02

Where 2ⁿ works.

For unrestricted sequential forks, every process executes every later fork. That is the clean doubling case.

Sequential fork stepper1 → 2 → 4 → 8
fork();   // #1
fork();   // #2
fork();   // #3
1process at start
P0

Only P0 exists.

2ⁿTotal processes, original included
2ⁿ − 1Children created
03

Loops and conditions.

New children can reach later iterations too. First determine when the fork actually runs.

Loop growth simulatorMove iteration by iteration
for (i = 0; i < 3; i++)
    fork();
1Total processes
0Children created
1Reach next iteration
2⁰Growth
04

Control flow changes everything.

Return values, short-circuit operators, break and continue turn fork() into an execution-tracing problem.

pid = fork();
if (pid > 0) {
    // parent executes this
}
if (pid == 0) {
    // child executes this
}
// both can continue here
Logical AND
fork() && fork()
First child gets 0 → false → second operand skipped.
Parent gets > 0 → true → second fork executes.
Total processes: 3
Logical OR
fork() || fork()
Parent gets > 0 → true → second operand skipped.
Child gets 0 → false → second fork executes.
Total processes: 3
GATE CSE 20262 marks · control flow
for (i = 0; i < 3; i++) {
    if (fork() == 0)
        continue;
    break;
}
printf("Hello!");

How many times does the final printf execute?

View original GATE discussion ↗
05

Mistake Lab.

Six tempting shortcuts behind the most common fork() mistakes.

“Three fork calls create three children.”
Later forks may be executed by children created earlier. Three unrestricted sequential forks create seven children.
“Three forks means eight children.”
Eight is the total process count. One is the original process, so seven are children.
“Parent always runs first.”
Do not assume parent-child scheduling order unless synchronization constrains it.
“wait() reduces the process count.”
wait() constrains progress; it does not undo a successful fork.
“Every written fork doubles everything.”
Only processes that reach and execute that fork can create another process.
“fork() && fork() gives four processes.”
The first child gets 0 and short-circuits. The total is three.
06

Process Tree Lab.

Step through exam-relevant patterns and watch the process-creation tree grow.

Choose a pattern

Three sequential forks

fork();
fork();
fork();
Start with one process, P0.
1Processes visible
0Children created
0Outputs so far

Creation tree

P0
07

The fork() Question Vault.

Practice the full fork() set by concept, predict the answer first, then reveal the key idea.

14 questions
GATE IT 2004Loop

Loop-based fork count

General process-count reasoning when fork() executes repeatedly inside a loop.

Key idea: For n unrestricted stages reached by all current processes, total processes are 2ⁿ.
GATE CSE 2005Memory

Parent-child variable behaviour

Tests how parent and child observe variable values and addresses after fork().

Key idea: Ordinary variable updates happen in separate process address spaces after fork().
GATE CSE 2008Loop

fork() inside n loop iterations

Tests exponential growth when all processes repeatedly execute fork().

Key idea: Total = 2ⁿ, children = 2ⁿ − 1.
GATE CSE 2012Process count

Three sequential forks

Distinguishes total processes from children created.

Key idea: 8 total processes, 7 children.
GATE CSE 2019Loop + condition

Conditional fork inside loop

fork() executes only on selected loop values.

Key idea: Five qualifying stages create 32 total processes and 31 children.
GATE CSE 2023System call

System call and kernel-mode transition

Reinforces the system-call concept around fork().

Key idea: fork() is a system call and its service requires kernel execution.
GATE CSE 2024wait()

fork() + wait() + output count

Combines loop execution, fork(), printf() and wait().

Key idea: Accepted result: 14 print executions.
GATE CSE 2026Control flow

continue, break and fork return

The child continues while the parent breaks.

Key idea: Final printf executes 4 times.
ISRO CSE 2017Memory

Parent-child variable output

Tests variable behaviour across parent and child paths.

Key idea: Parent and child observe their own private copies.
ISRO CSE 2018Output

Two forks and printf

Two unrestricted forks followed by one print statement.

Key idea: Four processes reach the print.
UGC NET 2016Memory

What remains shared after fork?

Tests explicit shared-memory semantics.

Key idea: Shared-memory segments remain shared; ordinary heap and stack are not one mutable copy.
UGC NET 2018Concept

System call that creates a Unix process

Direct process-creation system-call question.

Key idea: fork().
UGC NET 2020Output

Multiple fork and print programs

Compares output counts with forks in different positions.

Key idea: Count only processes that reach the particular print statement.
BARC 2025Memory-based

Reported fork questions

Post-exam recollections reported fork-related print-count questions.

Key idea: Use as extra practice only; exact wording is memory-based.
08

The 30-second revision sheet.

Screenshot it, then solve by tracing.

fork() == 0Child path
fork() > 0Parent path
n unrestricted forksTotal = 2ⁿ
Children2ⁿ − 1 in the unrestricted case
Condition?Find who reaches the fork
Loop?Track who reaches the next iteration
&& / ||Short-circuit first
break / continueTrace the next statement
printf()Count who reaches it
wait()Ordering constraint, not deletion
09

Final Boss.

Trace first. Check all three counts second.

fork();

if (fork() && fork()) {
    fork();
}

printf("X");
Trace the process, not the syntax.

If you can identify who reaches the next line, what fork() returned, and where each process goes next, unfamiliar questions become manageable.

From One Process to ManyOperating Systems · GATE CSE
Aditya Rathod · Team GO Classes