I am writing a server that uses fork()
to spawn handlers for client connections. The server does not need to know about what happens to the forked processes – they work on their own, and when they're done, they should just die instead of becoming zombies. What is an easy way to accomplish this?
相关问题
- Multiple sockets for clients to connect to
- Is shmid returned by shmget() unique across proces
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Index of single bit in long integer (in C) [duplic
There are several ways, but using
sigaction
withSA_NOCLDWAIT
in the parent process is probably the easiest one:Use double forks. Have your children immediately fork another copy and have the original child process exit.
http://thinkiii.blogspot.com/2009/12/double-fork-to-avoid-zombie-process.html
This is simpler than using signals, in my opinion, and more understandable.
How to get rid of zombie processes?
you can’t kill the zombie process with SIGKILL signal as you kill a normall process, As the zombie process can’t recive any signal. so having a good habit is very important.
Then when programming, how to get rid amount of zombie processes? According to the above description, the child process will send SIGCHLD signals to the parent process when its dies. by default, this signal is ignored by system, so the best way is that we can call wait() in the signal processing function, which could avoid the zombie stick around in the system. see more about this: http://itsprite.com/how-to-deep-understand-the-zombie-process-in-linux/