How is the zombie process generated in the title Unix/linux system? What's the harm? How to avoid it?
When a process calls the Exit command to end its own life, he is not actually destroyed, but rather leaves behind a data structure called the zombie process (Zombie) (System call exit, whose role is to make the process exit, but only a normal process becomes a zombie process, not be completely destroyed). In the state of the Linux process, the zombie process is a very special one, it has abandoned almost all memory space, no executable code, and can not be dispatched, just keep a position in the process list, record the process's exit status information for other processes to collect, in addition, The zombie process no longer occupies any storage space. He needed his father's process to bury him, if his parent process did not install the SIGCHLD signal handler call wait or waitpid () wait for the child process to end, there is no explicit ignore the signal, then it remains zombie state, if this time the parent process is finished, Then the init process will automatically take over the child process, and he will be able to get rid of it. But if the parent process is a loop and does not end, then the child process remains zombie, which is why there are sometimes a lot of zombie processes in the system.
Ways to avoid Zombie
1) in SVR4, if you call signal or Sigset to set the SIGCHLD configuration to ignore, no zombie subprocess will be generated. In addition, with SVR4 version sigaction, you can set the SA_NOCLDWAIT flag to avoid child process zombies. This can also be used in Linux, where a program starts calling this function signal (sigchld,sig_ign);
2) Call Fork two times
3) Wait for the child process to return with Waitpid.