fork() returns > 0
The positive value identifies the child PID.
Operating Systems · Processes · GATE CSE
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.
fork();
fork();
fork();
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().
The positive value identifies the child PID.
The new child can identify its own path from this return value.
For unrestricted sequential forks, every process executes every later fork. That is the clean doubling case.
fork(); // #1
fork(); // #2
fork(); // #3
Only P0 exists.
New children can reach later iterations too. First determine when the fork actually runs.
for (i = 0; i < 3; i++)
fork();
for (i = 0; i < 10; i++)
if (i % 2 == 0)
fork();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
for (i = 0; i < 3; i++) {
if (fork() == 0)
continue;
break;
}
printf("Hello!");Six tempting shortcuts behind the most common fork() mistakes.
Step through exam-relevant patterns and watch the process-creation tree grow.
fork();
fork();
fork();Practice the full fork() set by concept, predict the answer first, then reveal the key idea.
General process-count reasoning when fork() executes repeatedly inside a loop.
Tests how parent and child observe variable values and addresses after fork().
Tests exponential growth when all processes repeatedly execute fork().
Distinguishes total processes from children created.
fork() executes only on selected loop values.
Reinforces the system-call concept around fork().
Combines loop execution, fork(), printf() and wait().
The child continues while the parent breaks.
Tests variable behaviour across parent and child paths.
Two unrestricted forks followed by one print statement.
Tests explicit shared-memory semantics.
Direct process-creation system-call question.
Compares output counts with forks in different positions.
Post-exam recollections reported fork-related print-count questions.
Screenshot it, then solve by tracing.
Trace first. Check all three counts second.
fork();
if (fork() && fork()) {
fork();
}
printf("X");If you can identify who reaches the next line, what fork() returned, and where each process goes next, unfamiliar questions become manageable.