What if your computer could start new programs all by itself, instantly and without mistakes?
Why Process creation (fork and exec) in Operating Systems? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you want your computer to run two programs at the same time, but you have to start each one by typing commands manually every time.
You open a terminal, run one program, then close it, then open another terminal and run the next program.
This manual way is slow and tiring because you must do everything step-by-step.
It's easy to make mistakes like typing wrong commands or forgetting to start a program.
You also can't easily make programs start other programs automatically.
Process creation using fork and exec lets the computer copy a running program and then replace the copy with a new program.
This means one program can create another program to run without you typing commands again.
It makes multitasking smooth and automatic.
Run program A Close program A Run program B
pid = fork() if pid == 0: exec('programB')
It enables programs to start other programs automatically, making multitasking and complex workflows possible.
Your web browser opens a new tab by creating a new process to load a webpage without stopping the browser itself.
Manual program starting is slow and error-prone.
Fork creates a copy of a running program.
Exec replaces the copy with a new program to run.
Practice
fork() system call do in an operating system?Solution
Step 1: Understand the role of
Thefork()fork()call creates a new process by duplicating the current one, including its code and data.Step 2: Differentiate from
Unlikeexec()exec(), which replaces the process,fork()makes a copy, so both processes continue running.Final Answer:
Creates a new process by copying the current process -> Option BQuick Check:
fork()= process copy [OK]
- Confusing fork() with exec()
- Thinking fork() replaces the process
- Believing fork() pauses the process
exec() in a program?Solution
Step 1: Recall exec() syntax
Theexec()family requires the program path and an argument list, where the first argument is the program name.Step 2: Identify correct argument format
exec("/bin/ls", ["ls", "-l"]) correctly passes the path and an array with the program name and its argument.Final Answer:
exec("/bin/ls", ["ls", "-l"]) -> Option CQuick Check:
exec() needs path + argument list [OK]
- Passing arguments as separate strings instead of array
- Using command line string instead of path
- Omitting the program name in argument list
pid = fork()
if pid == 0:
exec("/bin/echo", ["echo", "Hello"])
else:
print("Parent process")What will be printed when this code runs?
Solution
Step 1: Understand fork() behavior
Thefork()creates a child process. The child runs theexec()replacing itself with the echo program.Step 2: Analyze output from parent and child
The parent (pid > 0) prints "Parent process". The child (pid == 0) calls exec(), loading /bin/echo which prints "Hello". Both outputs appear on stdout, typically with parent first.Final Answer:
Parent process Hello -> Option AQuick Check:
fork()+ childexec()= both print [OK]
- Thinking only parent prints
- Reversing the output order
- Believing exec() prevents child output
pid = fork()
if pid == 0:
exec("ls", ["ls", "-l"])
else:
print("Parent")Solution
Step 1: Check exec() usage
Theexec()call requires the full path to the executable, not just the command name.Step 2: Identify the missing full path
Using "ls" without "/bin/ls" will cause exec to fail because it won't find the program.Final Answer:
The exec call is missing the full path to the program -> Option AQuick Check:
exec() needs full path [OK]
- Using command name without path in exec()
- Confusing fork() and exec() roles
- Passing wrong argument types to exec()
Solution
Step 1: Understand process creation and replacement
Callingexec()replaces the current process, so calling it directly stops the original program.Step 2: Use fork() then exec() in child
By callingfork(), the program creates a child process. The child can then callexec()to run the new program, leaving the parent running.Final Answer:
Call fork() to create a child, then the child calls exec() -> Option DQuick Check:
fork() then exec() = safe new program run [OK]
- Calling exec() directly and losing original process
- Calling fork() twice unnecessarily
- Calling exec() before fork()
