Why are server processes usually fork () twice?

Source: Internet
Author: User
I received a telephone interview from an Internet company a few days ago. The interviewer asked me twice about the role of fork (). I was confused and said I did not know. The knowledge is still too narrow. The following summarizes the functions of the two fork () statements. First, you need to know what a zombie process is, what an orphan process is, and the conditions required for running server processes. The two fork () was used to solve a telephone interview with an Internet company a few days ago. The interviewer asked me twice about the role of fork (). I was confused and said I didn't know. The knowledge is still too narrow. The following summarizes the functions of the two fork () statements.

First, you need to know what a zombie process is, what an orphan process is, and the conditions required for running server processes. Two fork () is a programming method to solve these problems.

Orphan Process

Orphan process refers to the parent process that died before the child process ends (return orExIt ). As shown in:

However, the orphan process does not last for a long time as shown above. When the system discovers the orphan process,The init process adopted the orphan process and became its father., The Resource Recycling after the child process exit is completed by the init process.

Botnets

A zombie process ends before the parent process.IdReclaim sub-processes. As shown in:

If the parent process does not use wait to recycle the child process, it does not indicate that it will not recycle the child process. The child process sends a SIGCHILD signal to its parent process at the end. The parent process ignores the SIGCHILD signal by default. If the parent process sets the SIGCHILD signal processing function through the signal () function, then, the resources of sub-processes can be recycled in the signal processing function.

In fact, even if the parent process does not set the SIGCHILD signal processing function, it does not matter, because before the parent process ends, the child process can remain zombie. When the parent process ends, the init process is responsible for reclaiming zombie sub-processes.

However, if the parent process is a server process that keeps repeating and does not exit, the child process will remain zombie. Although zombie processes do not occupy any memory resources, too many zombie processes will affect system performance. What should I do when the technology is poor?

At this time, a hero is needed to save the whole world. It is two fork () techniques.

Two fork () Techniques

The two fork () processes are as follows:

The book "Advanced Programming for UNIX environments" (see http://www.linuxidc.net/thread-2063-1-1.html for download) provides an example of fork twice, the Code is as follows:

  1. IntMain (Void)
  2. {
  3. Pid_t pid;
  4. If(Pid = fork () <0)
  5. Err_sys ("Fork error");
  6. ELsE If(Pid = 0)
  7. {/* First child */
  8. If(Pid = fork () <0)
  9. Err_sys ("Fork error");
  10. Else If(Pid> 0)
  11. Exit (0 );/* Parent from second fork = first child */
  12. /* We're the second child; our parent becomes init as soon 
  13. As our real parent callexit () inStatEment above. 
  14. Here's where we 'd continue exeCutIng, knowing that when 
  15. We're re done, init will reap our status .*/
  16. Sleep(2 );
  17. Printf ("Second child, parent pid = % d \ n", Getppid ());
  18. Exit (0 );
  19. }
  20. If(Waitpid (pid, NULL, 0 )! = Pid)/* Wait for first child */
  21. Err_sys ("Waitpid error");
  22. /* We're the parent (the original process); we continue executing, 
  23. Knowing that we're not the parent of the second child .*/
  24. Exit (0 );
  25. }

Of course, the parent process of the second child process is the init process with process number 1.

In a word, the two fork () methods are used to prevent zombie processes from occurring in the system. 

For more information, see http://www.linuxidc.net/thread-2063-1-1.html and http://www.linuxidc.com/Linux/2011-04/34662.htm.

Related Article

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.