0
0
Operating Systemsknowledge~10 mins

Process creation (fork and exec) in Operating Systems - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Process creation (fork and exec)
Start: Running Process
Call fork()
Create Child Process
Child: Returns 0 from fork()
Call exec() to run new program
Replace Child's code with new program
Parent: Returns child's PID from fork()
Parent continues original program
The original process calls fork() to create a child. The child gets 0 return and can call exec() to run a new program. The parent gets child's ID and continues.
Execution Sample
Operating Systems
pid = fork()
if pid == 0:
    exec("/bin/ls")
else:
    wait()
This code creates a child process with fork. The child runs 'ls' using exec, the parent waits for child to finish.
Analysis Table
StepProcessActionfork() Returnexec() CalledResulting State
1ParentCalls fork()N/ANoParent running, fork called
2Parentfork() returns child's PIDChild PID (e.g. 1234)NoParent continues original code
2Childfork() returns 00NoChild starts execution
3ChildCalls exec("/bin/ls")0YesChild replaced by 'ls' program
4ParentCalls wait()Child PIDNoParent waits for child to finish
5ChildRuns 'ls' program0YesChild runs new program until exit
6Parentwait() returns after child exitsChild PIDNoParent resumes after child ends
💡 Execution stops when child finishes exec program and parent finishes wait.
State Tracker
VariableStartAfter fork()After exec()After wait()Final
pidundefined0 in child, child's PID in parent0 in child (exec replaces code, pid unchanged)child's PID in parentchild's PID in parent
Key Insights - 3 Insights
Why does fork() return 0 in the child but a positive number in the parent?
fork() returns 0 in the child process to indicate it is the new child. The parent gets the child's process ID (a positive number). See execution_table step 2.
What happens to the child's code after exec() is called?
exec() replaces the child's current program code with a new program. The child's process ID stays the same, but the code and data change. See execution_table step 3 and 5.
Why does the parent call wait() after fork()?
The parent calls wait() to pause and let the child finish its new program before continuing. This prevents the child from becoming a 'zombie' process. See execution_table step 4 and 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2. What value does fork() return in the child process?
A-1
BChild's PID
C0
DUndefined
💡 Hint
Check the 'fork() Return' column for the child process at step 2.
At which step does the child's program get replaced by the new program?
AStep 3
BStep 1
CStep 5
DStep 6
💡 Hint
Look for when exec() is called in the 'exec() Called' column.
If the parent did not call wait(), what would happen according to the execution flow?
AParent would immediately exit before child finishes
BChild would become a zombie process
CParent would wait indefinitely
DChild would never run exec()
💡 Hint
Refer to key_moments about why wait() is important to prevent zombie processes.
Concept Snapshot
fork() creates a new child process by duplicating the parent.
In the child, fork() returns 0; in the parent, it returns child's PID.
exec() replaces the child's code with a new program.
Parent can call wait() to pause until child finishes.
This sequence creates a new process running a different program.
Full Transcript
Process creation in operating systems often uses fork and exec. First, a running process calls fork(), which creates a new child process. The fork call returns 0 in the child and the child's process ID in the parent. The child can then call exec() to replace its program with a new one, such as running a different command. Meanwhile, the parent can call wait() to pause until the child finishes. This method allows one process to create another and run a different program, managing execution order and resources.