Zombie process

A zombie is mainly used in Unix -like operating systems, a process that is completed, but still appear in the process table and slightly system resources. A zombie directed itself to no harm, but may indicate an error.

Creation of zombie processes

When a process starts a new process (by forking ), the old parent process and the new child process is called. If the child process exits, the parent process can ask the operating system has finished on the nature of the child process: successfully, with an error, crashed, broken, etc.

To enable this query remains a process, even after it has been completed, are in the process table until the parent process this query by - no matter if this information is used or not. Until then, the child process has the state zombie. In this state, the process is itself no more memory (up to the space moderately negligible entry in the process table of the kernel ) and consumes no CPU time, but it retains its PID, the (still) can not be reused for other processes.

The kernel sends to the parent a special signal, namely SIGCHLD, when one of his children is terminated. In the handler for this signal status inquiry of the child process is carried out so that it disappears for good. This does not happen, or no handler for SIGCHLD is specified (in this case, the signal is simply ignored ), the child remains in the zombie state in the process table.

Orphaned processes

Another special case, which is in and of itself regardless of zombies, but can occur in combination with it is if the parent process is terminated. In this case, all its child processes called " orphaned ". The child processes are then "adopted" by definition of the process with PID 1 and get these assigned as the parent process. ( In the classical System V boot sequence of the so-called init PID is 1 In OS X launchd occurs at this point. ) This happened no less than for current as well as for zombie processes whose parents no longer exist.

Problems of zombies

Zombies are as a rule is not a problem for an operating system, since these processes have already been completed and take very few system resources. However, the causative fault in the parent process under certain circumstances, such as under high load, have far greater consequences. A large number of zombies can also cause the kernel to free PIDs that he needs for new processes go out. The PID is occupied by zombies is more problematic under certain circumstances as their minimal memory consumption. If the number of processes is limited to a system (including via maxproc in limits.conf to restrict, for example, a Forkbomb in their effect ), thus leading to many zombies to the fact that the kernel does not generate new processes more.

Treatment of zombie processes

Although zombie processes pose no threat to the system in general, they can nevertheless be deleted manually - or automatically by the system.

Automated deletion

The init process has among its tasks to retrieve the completion status of all his children as soon as they finished - thus transferred into the zombie state - be. He ensures that no unnecessary zombies exist in the system. Ongoing processes that are adopted by the init process will not be affected - waiting for the init process just until they are finished, and then asks for their status.

Manually Clearing

A zombie process can be detected in the Unix process table usually by the status of "Z" ( for zombie ) ( for example, ps axo stat, pid, comm | grep ' ^ Z'). Through the kill command the SIGKILL signal to be sent to the PID of the zombie process to compulsively delete it from the process table. However, it may lead to unforeseen side effects, if the parent process is still running and wants to access the status of its child process at a later time with this method.

Alternatively, the parent process of the Zombies (according PPID in the process table, for example, ps axo stat, pid, comm, ppid | grep ' ^ Z') manually send the signal SIGCHLD, on the command line about using kill- CHLD < PID of the parent process >. Not responding father it, so you can exit the parent process, so that the zombie child adopted by the init process and will immediately thereafter be permanently deleted by retrieving its state.

Feature in the Linux kernel

The Linux kernel provides for processes that are not interested in their children's status, a simple - mind not standardized - get rid of method Zombies: Is a process to explicitly that he wants to ignore SIGCHLD (as opposed to Ignore by default if no handler is specified), so Linux deletes the zombies automatically, without waiting for a status query.

Eliminate programming errors

When a process his children often "forgets" and can become zombies, especially if he does not respond to a manual SIGCHLD, this is usually an indication of a bug in this program. For the above reasons, the error should be eliminated in the program so as not to compromise the system by the formation of zombies.

Example in C for the generation of zombies

Synchronous waiting for the same child process in a defined sequence can generate zombie processes and this even get over a long period alive. This is not necessarily a programming error, as shown in the following code example:

# include # include # include   int main (void ) {      pid_t pid; / / Space for 10 process IDs ( the child processes)      int i; / / Control variable        for (i = 0; i < 10; i ) {          / / The parent process then creates a child process,          / / Which is independent from the parent process with the          / / Re-execution of the program begins.          / / A child process does not produce a fork of itself          pids [i ] = fork ();          if ( pid [i ] == 0) {              / / Then we are in one of the 10 child processes              / / The first child process waits 10 seconds and each              / / Additional child process waits 1 seconds shorter than the              / / Previous one.              sleep ( 10-i );              exit ( 0); Successfully complete / / child process          }      }        / / Here comes along only the parent process      for (i = 0; i < 10; i ) {          / / The parent process will now wait until the series after each          / / 10 its child processes is terminated. Unfortunately, the          / / Child with the longest waiting time serviced first. although          / / The other kids have long been successfully completed,          / / Blocked the first child of a cleanup process table          waitpid ( pid [i ], NULL, 0);      }        return 0; Successfully complete / / parent process } Nevertheless, the behavior is not optimum. A better alternative would be to wait for any process to highlight it in the array as done and continue this process until the array is empty.

837272
de