Process Creation: fork() System Call
You'll learn how new processes are born using the fork() system call and what its return value tells us.
The Core Idea
In Unix-based operating systems, a running process creates a new child process using the fork() system call. Think of it like cellular division: the parent process splits, and suddenly there are two nearly identical processes running at the same time—the original parent and the newly created child.
When fork() executes, the operating system:
- Creates a new Process Control Block (PCB) for the child
- Copies the parent's memory layout (code, data, heap, stack)
- Places the child in the Ready state
- Both processes continue executing from the point right after the
fork()call
The Magic Return Value
Here's what makes fork() special: it returns different values to each process, which is how they know who they are:
- In the parent process:
fork()returns the child's process ID (PID), a positive integer - In the child process:
fork()returns0 - On failure:
fork()returns-1(no child created)
This clever design lets you write code that both processes execute, but with different behavior based on the return value.
Why This Matters
The fork() return value is essential for coordination. The parent might use the child's PID to track it or wait for it to finish. The child knows it's the child because it receives 0, so it can perform different tasks than its parent—like loading a new program to execute.
Key Takeaway: The fork() system call clones a process into parent and child; it returns the child's PID to the parent, 0 to the child, and -1 on error—this return value is how each process determines its identity and role.