Technical skill

Linux process: PID and PPID

A process tree gives names to running programs

A computer can run many programs at once. Each running instance is a process, and Linux gives it a process identifier (PID), a number used to refer to it while it lives. The parent process identifier (PPID) points to the PID of the process that started it. You can picture the relationships as a family tree: the process is one entry, and the parent link shows where it came from. A tree view makes those links visible.

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

Each process has its own ID; a child also records which process started it.

Use ps to see the current tree of visible processes:

ps -eo pid,ppid,stat,comm --forest

The numbers and names in this display are a snapshot. The kernel can reuse a PID after its process exits, and a process's parent can change during its life.

How a child process starts and finishes

In Unix-like systems, fork() creates a child process with a new PID and the creating process as its initial parent. The parent can call wait() or waitpid() to collect the child's exit status. After a child finishes, it may remain briefly as a zombie: the program has stopped running, but its parent has not yet collected that status. A zombie does not use CPU like a live process.

execve() replaces a process's program image with another program. It does not create a new process, so the PID and PPID stay the same. A shell often starts a child and then calls execve() inside it; that is why the program name can change while its PID remains.

If a parent exits first, Linux assigns its orphaned children to a suitable process. This is usually the init process in the same PID namespace, unless a nearer ancestor registered as a child subreaper. A process's displayed parent can therefore change.

Inspect one process

To see one current PID's identifier, parent, state, and command name, use:

ps -o pid,ppid,stat,comm -p 1234
cat /proc/1234/status

In /proc/PID/status, PPid reports the parent PID visible in that procfs view. For example, inspect /proc/1234/status for process 1234. In Bash, $$ expands to the shell's PID and $PPID to its parent PID; $$ keeps the same value inside a Bash subshell. $! contains the PID of the most recently started background job; a script can pass that value to wait.

A PID namespace gives processes a separate numbering view. A process can have one PID inside a container and another in an ancestor namespace, and different namespaces can each contain a process numbered 1. A /proc mount shows processes visible from the namespace where it was mounted, so ps results depend on that view. When a container PID does not match a host listing, check the PID namespace and procfs mount before assuming the process disappeared. Before acting on a saved PID, verify its current command and start time because that number may now belong to another process.

Interactive exercises

Materials

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