Fundamentals 7 min read

How Linux Process Creation Powers Containers: From fork to Namespaces

This article explains how Linux creates processes using fork, vfork, clone and pthread_create, reveals the role of the init process, explores clone flags and namespace checks in the kernel, and shows why understanding these fundamentals demystifies container startup.

Liangxu Linux
Liangxu Linux
Liangxu Linux
How Linux Process Creation Powers Containers: From fork to Namespaces

Process Hierarchy on Linux and macOS

Running ps -ef shows that PID 1 is the init process: systemd on Linux and launchd on macOS. All other processes are descendants of PID 1, whose parent is the invisible PID 0.

Creating Processes in Linux

Linux provides four primary ways to create a new execution context: fork – creates a copy of the calling process. vfork – similar to fork but suspends the parent until the child calls exec or _exit. clone – a flexible system call that can share selected resources with the parent.

glibc wrapper pthread_create – creates a thread by invoking clone with thread‑specific flags.

All of these eventually invoke the kernel function do_fork, which implements the low‑level creation logic.

Key Parameter: clone_flags

The clone_flags argument determines which resources are shared between the parent and the new task. For example, pthread_create passes flags that cause the new task to share the same PID, memory space, and file descriptors, turning it into a thread rather than an independent process.

Minimal fork Example

#include <unistd.h>
#include <sys/wait.h>
int main() {
    pid_t pid = fork();
    if (pid == 0) {
        // Child process – replace image with a shell
        execlp("/bin/sh", "sh", (char *)NULL);
    } else if (pid > 0) {
        // Parent process – wait for child termination
        wait(NULL);
    } else {
        // fork failed
        return 1;
    }
    return 0;
}

After fork, the child receives pid == 0. Combining fork with exec replaces the child’s program image, effectively launching a new shell.

Namespace Validation in do_fork

Inspecting the kernel source of do_fork reveals a validation step that rejects incompatible flag combinations: CLONE_NEWUSER or CLONE_NEWPID cannot be used together with CLONE_THREAD or CLONE_PARENT. CLONE_NEWPID creates a new PID namespace, isolating the child’s process IDs from the host. CLONE_THREAD forces the new task to share the same PID as its creator, which contradicts the isolation intent of a new PID namespace. The kernel therefore returns an error when both flags are set.

Implications for Container Technology

Containers rely on Linux namespaces to achieve isolation. When a container is started, the runtime typically invokes clone with CLONE_NEWPID (and other namespace flags such as CLONE_NEWUTS, CLONE_NEWNET, etc.) to place the container’s processes in separate namespaces. Threads inside a container are created with pthread_create, which uses CLONE_THREAD to share the PID within the container’s PID namespace, but never mixes CLONE_THREAD with CLONE_NEWPID at the same time. Understanding the relationship between fork / clone , clone_flags , and namespace checks provides a solid foundation for grasping how containers are instantiated and why certain flag combinations are prohibited.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxContainerOperating SystemprocessNamespacesystem callsfork
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.