Contrary to all expectations, it’s basically a fork , which makes the creation process so incredibly fast in Unices.
AFAIK, on Linux, the actual process memory is not copied to the fork, the child starts with the same virtual memory mapping as the parent, and the pages are copied only where and when the child makes the change. Most pages are read-only code, so they are never copied. This is called copy-on-write .
Use cases where it is useful to use parent process copying:
When you say cat foo >bar , the forks of the shell, and in the child process (still the shell) prepare the redirection, and then execs cat foo . The executed program runs under the same PID as the child shell and inherits all open file descriptors. You won’t believe how easy it is to write a basic Unix shell.
- Demons (services)
Demons run in the background. Many of them develop after initial preparation, exits from the parent and the child are separated from the terminal and remain in the background.
- Network servers
Many network daemons must handle multiple connections simultaneously. Sshd example. The main daemon starts as root and listens for new connections on port 22. When a new connection appears, it expands the child. The child simply saves a new socket representing this connection, authenticates the user, reduces privileges, etc.
- Etc
source share