How to avoid the generation of zombie processes when the server is concurrently processing

Source: Internet
Author: User

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:

  
 
  1. socket();
  2. bind();
  3. listen();
  4. while(1)
  5. {
  6. accept();
  7. while(1)
  8. {
  9. pid = fork();
  10. if(pid>0)
  11. {
  12. wait();
  13. }
  14. else if(pid==0)
  15. {
  16. pid2 = fork();
  17. if(pid2>0)
  18. {
  19. exit();
  20. }
  21. else if(pid2==0)
  22. {
  23. while(1)
  24. {
  25. process();//
  26. }
  27. close();
  28. exit();
  29. }
  30. }
  31. }
  32. }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.