Technical skill

Linux process: fork

Subject: .../LINUX

Start a second task from a running program

A running program may need a helper to do another task while the original continues or waits. Linux can start a second process from the first. The original is called the parent and the new process its child; both continue from the same point, then can do different work. Their relationship forms a small branch in the process tree. A process is one running instance of a program.

One running program starts two separate helper processes on the same computer.

A parent process can create a child that runs as a separate process.

Linux calls this operation fork(). It creates a child process. The child gets its own memory, initialized from the parent's data. Linux uses copy-on-write so it can wait to copy physical memory pages until one process changes them. The memory is not shared for ordinary writes. The child also inherits open file descriptors. Corresponding descriptors refer to the same open file description, so the two processes can share state such as a file offset.

The example below starts the date program in the child and waits for its result in the parent. The code uses C, a programming language, and POSIX process functions:

pid_t child = fork();

if (child == -1) {
    perror("fork");
    exit(EXIT_FAILURE);
}
if (child == 0) {
    execlp("date", "date", "+%F", (char *)NULL);
    perror("execlp");
    _exit(127);
}

int status;
if (waitpid(child, &status, 0) == -1) {
    perror("waitpid");
    exit(EXIT_FAILURE);
}
if (WIFEXITED(status)) {
    printf("child exit status: %d\n", WEXITSTATUS(status));
}

If fork() succeeds, the child receives return value 0 and the parent receives the child's process ID. If creating the child fails, the parent receives -1. The C type pid_t holds a process ID. execlp replaces the child program with date; if that succeeds, it never returns. If it fails, the child prints an error and calls _exit. The parent calls waitpid for that child and checks whether it exited normally before reading the exit code. The program assumes standard headers for pid_t, fork, execlp, waitpid, the status macros, and standard I/O.

Keep creation, execution, and collection separate

fork() creates another process. execve() replaces a process's program image while keeping its PID. A common pattern is to fork, call execve() or another exec-family function in the child, then call waitpid in the parent. The wait call collects the child's termination status. If a child exits before the parent collects that status, the kernel keeps a minimal zombie entry until a wait reaps it.

fork() can fail when process or resource limits prevent creating another process, so check its return value. In a multithreaded program, only the thread that called fork() exists in the child. Before an exec, that child must use only async-signal-safe functions. Higher-level process-launch APIs often handle these constraints for ordinary application code.

The parent and child run independently, so their output can appear in either order. If later parent work depends on a child, wait for it explicitly. When several children run, track and reap each one. The wait status is encoded: use WEXITSTATUS(status) only after WIFEXITED(status) is true, and use WIFSIGNALED(status) to detect signal termination.

Interactive exercises

Materials

Skillsets
Skillset Devops preview
image descriptionDevops
by Dio
3 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Skillset Linux for backend developer preview
image descriptionLinux for backend developer
by Artem Naumenko (fb)
16 users
Skillset Skyeng SRE preview
image descriptionSkyeng SRE
by Yuliya Marochkina
12 users
Skillset Skyeng linux system administrator preview
image descriptionSkyeng linux system administrator
by Vitaliy Okulov
3 users
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful