fork is the Unix system call that creates a new process. The new process (the child) is an almost-exact copy of the caller (the parent): same memory contents, same file descriptors, same working directory. They differ only in their PIDs and in fork's return value—it returns 0 to the child and the child's PID to the parent. This lets each process branch on the result and do different work.
In C:
pid_t pid = fork();
if (pid == 0) {
// child
execvp("ls", argv);
} else if (pid > 0) {
// parent
waitpid(pid, NULL, 0);
} else {
perror("fork");
}
Shell pipelines use fork extensively. ls | grep foo | wc -l forks three new processes, connects them via pipes, and waits for them all. Modern Linux implements fork efficiently with copy-on-write memory: the child's pages share physical memory with the parent's until one of them writes, at which point the kernel duplicates the page.
Fork is usually followed immediately by exec, which replaces the child's program image with a new one. This "fork-then-exec" idiom is the canonical way to launch a new program, and is what happens every time you type a command at the shell.
Discussed in:
- Chapter 10: Processes and Job Control — Fork and Exec
Also defined in: Textbook of Linux