Dealing with Orphans and Zombies

Most Linux processes don't run forever. If they did, they'd consume memory, CPU time, and other resources, and Linux would soon become sluggish as a result. Processes can terminate for many reasons—because they've finished whatever finite job they're designed to do (as, for instance, cp terminates when it finishes copying a file), because a user has told a program to terminate via an exit command, because a user or administrator has told the process to terminate via a kill command (as described in the upcoming section, "Basic Use of kill"), or because a program has crashed. One complication of a process terminating is that it may leave behind children. For instance, in Figure 14.1, if the user exits from the bash shell (and hence the xterm in which it's running), the programs run from bash will be left without a parent—they'll be orphaned.

Because of the way Linux handles processes internally, the process tree must remain intact; orphaned processes can't simply be left running without parents. Linux solves this problem by having init "adopt" all orphaned processes. In order to help trace processes to their parents, each process has a parent PID (PPID). For instance, the PPID of init is 0, indicating that init's parent is the kernel. When a process is orphaned, its PPID is set to 1. Because many programs are launched from scripts or other processes that terminate before the programs they launch, many Linux programs, including most servers, have PPIDs of 1.

Occasionally, something will go wrong when a process ends. Ordinarily, a parent is responsible for doing some of the work involved in cleaning up after its terminated children. If the parent doesn't do this work, though, the result is a zombie\ a process that's been incompletely killed. Zombies consume little in the way of system resources, but they appear in process listings with a notation to the effect that they're zombies. Thus, zombies may be distracting, but they aren't really harmful—at least, not normally. If a buggy process spawns a lot of processes and then doesn't manage their termination properly, your process table might fill with zombies and system performance might even be impacted. Such occurrences are rare, though.

Because init adopts the children of a process that's terminated, you can rid yourself of zombies by locating the zombies' parents and killing them. When you do this, init adopts the zombies and does the housekeeping necessary to remove them from the process list.

Team LIB

Team LIB

^ previous

0 0

Post a comment