When the server processes multiple request links, it is common practice to receive a request, the server fork a child process,
If the parent process calls the wait function to handle state information at the end of the child process, the wait function must wait for the child process's
End, at the same time the server can only process a request, if you consider not to use the wait function, then accept a
Request, the result is a zombie process because the parent process has been fork,init the process and cannot take over the end of the Wahabbi
Process , this time, because the number of processes generated by a server is limited, not unlimited, and the zombie process will inevitably account for
According to a process number, there are a number of zombie processes that can cause the new request link to be processed. So
How to Avoid this happens is to use the fork function two times.
Pseudo code:
socket();
bind();
listen();
while(1)
{
accept();
while(1)
{
pid = fork();
if(pid>0)
{
wait();
}
else if(pid==0)
{
pid2 = fork();
if(pid2>0)
{
exit();
}
else if(pid2==0)
{
while(1)
{
process();//
}
close();
exit();
}
}
}
}
Icon:
This guarantees 1 fast processing of multiple request links, and there is no zombie process to spawn
From for notes (Wiz)
How to avoid the generation of zombie processes when the server is concurrently processing