keepcalculator

06 Numbers and Powers

Does a Child Inherit the Parent PID After exec?

Card No. 06·12825 · Updated · Standard formula, shown below

No, a child process does not inherit the parent’s PID after exec. The PID (process identifier) is assigned by the kernel at process creation (fork on Unix-like systems) and remains unchanged across an exec call. The child gets its own unique PID, and exec only replaces the process’s memory image, not its identity.

Background: What Are PIDs and exec?

Every process on a Unix-like system (Linux, macOS, BSD) has a numeric PID assigned by the kernel when the process is created. The PID is used by the kernel and by other processes to send signals, check status, and manage the process. The exec family of functions (execl, execv, execle, execve, etc.) loads a new program into the current process, replacing the existing code, data, and stack. The process’s PID, parent PID (PPID), and most other kernel-managed attributes are not changed by exec.

How Process Creation Works

In Unix-like systems, a new process is created with fork(). This system call creates a nearly identical copy of the calling process, including the same code, memory contents, and open file descriptors. The new child process receives a new PID, and its PPID is set to the PID of the parent. After fork, the child often calls one of the exec functions to load a different program. The sequence is so common that it’s usually written as fork + exec.

Key points:

  • fork creates a new process with a unique PID.
  • exec replaces the process image but keeps the same PID.
  • The child’s PPID is set at fork and does not change after exec.

What Actually Changes with exec?

When a process calls exec, the kernel discards the old program’s code and data, loads the new executable, and reinitializes the stack, heap, and static data. However, the following are preserved across exec:

  • PID and PPID
  • Process group ID and session ID
  • Open file descriptors (unless marked close-on-exec)
  • Environment variables (passed explicitly to exec)
  • Signal dispositions (signals caught by the old program are reset to default, but ignored signals remain ignored)
  • Current working directory, root directory, umask, and resource limits

In short, exec changes what the process does, not its identity.

Common Misconception: PID vs. PPID

A frequent point of confusion is between PID and PPID. The child process has its own PID, but its PPID is the parent’s PID. After exec, the child’s PPID remains the same—it still points to the parent that called fork. If the parent exits, the child may be reparented to init (PID 1 on Linux), which changes its PPID, but that is unrelated to exec.

Another misconception: some think that because exec replaces the process, the PID might be recycled or changed. In reality, the kernel keeps the PID stable for the life of the process, even across exec calls. A process can call exec multiple times, and its PID never changes.

Why This Design Matters

Stable PIDs across exec are crucial for system behavior. For example:

  • Shells use fork + exec to run commands. If the child’s PID changed after exec, the shell couldn’t track the job or send signals to it.
  • Daemons often fork and then exec to drop privileges or load a new configuration. Their PID must remain the same so that process managers (like systemd) can monitor them.
  • Security tools and auditing rely on stable PIDs to attribute actions to a specific process.

Practical Example: fork + exec in Code

Consider a simple C program:

pid_t pid = fork();
if (pid == 0) {
    // child process
    printf("Child PID: %d\n", getpid());
    execl("/bin/ls", "ls", NULL);
    // If exec fails, we reach here
    perror("exec");
    exit(1);
} else {
    // parent process
    printf("Parent PID: %d, Child PID: %d\n", getpid(), pid);
}

Here, getpid() in the child returns the same value before and after execl. The parent’s pid variable holds the child’s PID, which remains valid.

Frequently Asked Questions

Does the child process get a new PID after exec?

No. The PID is assigned at fork and stays the same throughout the process’s lifetime, even after exec. The kernel does not change the PID when the process image is replaced.

What is the difference between fork and exec?

fork creates a new process (a copy of the parent) with a new PID. exec loads a new program into the current process, replacing the old program but keeping the same PID. They are often used together: fork to create a child, then exec to run a different program.

Does the child inherit the parent’s environment variables after exec?

Yes, the child inherits the parent’s environment at fork. When exec is called, the environment can be passed explicitly (e.g., execle or execve). If not, the current environment is preserved.

Can a process change its own PID?

No. A process cannot change its PID. PIDs are assigned by the kernel at creation and are immutable until the process exits. The only way to get a different PID is to create a new process.

What happens to the child’s PPID if the parent dies?

If the parent process exits, the child is reparented to the init process (PID 1 on Linux) or a subreaper. The child’s PPID changes, but its own PID remains the same. This is unrelated to exec.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →