Middle+
What are zombie processes in Linux? Which signals should be sent to terminate them?
sobes.tech AI
Answer from AI
Zombie processes in Linux are processes that have finished execution, but their parent process has not yet read their exit status via the wait() system call. They remain in the process table, occupying an entry, but do not consume CPU or memory.
For proper termination, the parent process needs to call wait() or waitpid(). If the parent does not do this, zombies persist.
Signals do not directly remove zombies, but you can:
- Send a
SIGCHLDsignal to the parent to notify it of the child's termination, prompting it to callwait(). - If the parent does not respond, you can terminate the parent (e.g., with
SIGTERMorSIGKILL), after which the init process (PID 1) becomes the new parent and will callwait(), removing the zombie.
Example in Go for handling child processes:
cmd := exec.Command("somecommand")
err := cmd.Start()
if err != nil {
log.Fatal(err)
}
err = cmd.Wait() // important to call to prevent zombies
if err != nil {
log.Println("Process finished with error:", err)
}