5. Process Termination

The Termination Process

Once a process is created, how does it end? The termination of a process is a critical part of the process lifecycle, ensuring system resources are managed effectively.

A process typically terminates by calling the _exit system call. This action signals the kernel that the process is finished and its resources, like memory and file descriptors, can be reclaimed. Upon exiting, the process provides a termination status to the kernel, which is an integer value. By convention, a status of 0 indicates successful execution, while a non-zero value signals an error.

However, calling _exit doesn't immediately erase the process. The parent process must acknowledge its child's termination by using the wait system call. This call allows the parent to retrieve the child's termination status. This two-step mechanism is essential for proper process cleanup. Another way to linux kill child process is by using signals, a topic we will explore in a later lesson.

Orphan Processes

What happens if a parent process terminates before its child? The child process becomes an "orphan." Since its original parent can no longer call wait, the kernel intervenes. The orphan process is immediately adopted by a special system process, typically init (process ID 1), which is considered the ancestor of all processes. The init process then assumes the role of the parent, periodically calling wait to collect the termination status of any of its adopted children, allowing them to terminate cleanly.

Zombie Processes

A different scenario occurs when a child process terminates, but its parent has not yet called wait. In this state, the child becomes a "zombie" process. The kernel releases most of the zombie's resources, but it keeps an entry in the process table. This entry contains the process ID and the termination status, waiting for the parent to collect it.

Zombie processes are already dead, so they don't consume CPU time. You cannot kill them with signals because they are not running. The process of the parent calling wait to clean up a zombie is called "reaping." If the parent process never calls wait, these zombies can accumulate. While a few are harmless, a large number can fill the process table, preventing new processes from being created. In cases where the parent process also terminates, init will adopt and reap the zombie.

Zombie vs Orphan Process

Understanding the difference between a zombie vs orphan process is key to diagnosing process-related issues.

  • An orphan process is an active, running process whose parent has died. It is adopted by init and continues to execute until it finishes.
  • A zombie process is a dead process that has completed its execution but still has an entry in the process table. It is waiting for its parent process to read its exit status.

In short, an orphan is alive but parentless, while a zombie is dead but not yet fully reaped by its parent.

Sign in to save your learning progress

Sign in

Exercises

To apply these concepts, try the following hands-on lab:

  1. Manage and Monitor Linux Processes - Practice interacting with foreground and background processes, inspecting them with ps, monitoring resources with top, adjusting priority with renice, and terminating them with kill. This lab provides practical experience with the process lifecycle, including how to terminate them and observe their states.

Quiz

What is the most common termination status for a process succeeding?