Keywords: fork | exec | UNIX | process management
Abstract: This article explains the core differences between the fork and exec system calls in UNIX, covering their definitions, usage patterns, optimizations like copy-on-write, and practical applications. Based on high-quality Q&A data, it provides a comprehensive overview for developers.
In UNIX-based systems, the fork and exec system calls are fundamental for process creation and execution. This article explores their differences and practical applications.
Understanding fork
The fork call creates a new process by duplicating the existing one. The child process is almost identical to the parent, with a unique process ID (PID) and the parent's PID as its parent PID (PPID). The return value distinguishes parent and child: 0 for the child, and the child's PID for the parent. If fork fails, no child is created, and the parent receives an error code.
Understanding exec
exec replaces the current process with a new program, loading it into memory and starting execution from its entry point. This does not create a new process but transforms the existing one. There is a whole family of exec calls, such as execl, execle, and execve.
Combining fork and exec
Often, fork and exec are used together to run a new program as a child process. For example, shells use this to execute commands: fork a child, then exec the command. The following diagram illustrates this with the bash shell running the ls command:
+--------+
| pid=7 |
| ppid=4 |
| bash |
+--------+
|
| calls fork
V
+--------+ +--------+
| pid=7 | forks | pid=22 |
| ppid=4 | ----------> | ppid=7 |
| bash | | bash |
+--------+ +--------+
| |
| waits for pid 22 | calls exec to run ls
| V
| +--------+
| | pid=22 |
| | ppid=7 |
| | ls |
V +--------+
+--------+ |
| pid=7 | | exits
| ppid=4 | <---------------+
| bash |
+--------+
|
| continues
V
Separate Usage Scenarios and Optimizations
fork can be used alone, e.g., for daemons that listen on TCP ports and fork copies to handle requests. Similarly, exec can be used directly when a program wants to replace itself without forking. Some UNIX implementations use copy-on-write optimization for fork, delaying memory copying until modification occurs.
Additional Considerations
Understanding fork and exec is key to effective process management. Historical context shows they embody the simplicity of UNIX design. Developers should be aware of resource limits and implementation variations.