I think some concepts from "Advanced Unix Programming" by Marc Rochkind were helpful in understanding the different roles of fork()
/exec()
, especially for someone used to the Windows CreateProcess()
model:
A program is a collection of instructions and data that is kept in a regular file on disk. (from 1.1.2 Programs, Processes, and Threads)
.
In order to run a program, the kernel is first asked to create a new process, which is an environment in which a program executes. (also from 1.1.2 Programs, Processes, and Threads)
.
It’s impossible to understand the exec or fork system calls without fully understanding the distinction between a process and a program. If these terms are new to you, you may want to go back and review Section 1.1.2. If you’re ready to proceed now, we’ll summarize the distinction in one sentence: A process is an execution environment that consists of instruction, user-data, and system-data segments, as well as lots of other resources acquired at runtime, whereas a program is a file containing instructions and data that are used to initialize the instruction and user-data segments of a process. (from 5.3
exec
System Calls)
Once you understand the distinction between a program and a process, the behavior of fork()
and exec()
function can be summarized as:
fork()
creates a duplicate of the current processexec()
replaces the program in the current process with another program(this is essentially a simplified 'for dummies' version of paxdiablo's much more detailed answer)