This visual model shows how Linux creates a new process and then replaces that process with a new running program.
Existing Process
(shell or systemd)
|
|
fork()
|
v
┌───────────────────────────┐
│ │
│ Child Process Created │
│ │
│ Same program image │
│ Same open files │
│ Own PID │
│ │
└───────────────────────────┘
|
|
exec()
|
v
┌───────────────────────────┐
│ │
│ Old image replaced │
│ │
│ New program loaded │
│ New memory layout │
│ Execution begins │
│ │
└───────────────────────────┘
|
v
Running Program
|
v
exit()
|
v
Parent observes
status with wait()
The fork() call creates a child process. At first, that child is
closely related to the parent. It has its own process ID, but it begins with a
copy of the parent process state.
The exec() call then replaces the child process image with a new
program. The process remains alive, but the code and memory layout become those
of the new executable.