Process creation (fork and exec) in Operating Systems - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When a new process is created using fork and exec, it is important to understand how the time taken grows as the system handles more processes.
We want to know how the cost of creating processes changes as the number of processes increases.
Analyze the time complexity of the following code snippet.
for (int i = 0; i < n; i++) {
pid_t pid = fork();
if (pid == 0) {
execve("/bin/program", args, envp);
exit(0);
}
}
This code creates n child processes by calling fork in a loop, then each child replaces its program image using exec.
- Primary operation: The loop runs fork() n times to create n processes.
- How many times: The fork system call is executed once per iteration, so n times.
Each new process requires a fork call, which duplicates the current process, and then an exec call to load a new program.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 forks and 10 execs |
| 100 | About 100 forks and 100 execs |
| 1000 | About 1000 forks and 1000 execs |
Pattern observation: The total work grows directly with the number of processes created.
Time Complexity: O(n)
This means the time to create processes grows linearly with the number of processes requested.
[X] Wrong: "Forking multiple processes happens all at once and takes constant time regardless of n."
[OK] Correct: Each fork call duplicates the process separately, so the total time adds up with each new process created.
Understanding how process creation scales helps you reason about system performance and resource management in real applications.
"What if we replaced fork with a thread creation call? How would the time complexity change?"
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()
