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:
- IntMain (Void)
- {
- Pid_t pid;
- If(Pid = fork () <0)
- Err_sys ("Fork error");
- ELsE If(Pid = 0)
- {/* First child */
- If(Pid = fork () <0)
- Err_sys ("Fork error");
- Else If(Pid> 0)
- Exit (0 );/* Parent from second fork = first child */
- /* We're the second child; our parent becomes init as soon
- As our real parent callexit () inStatEment above.
- Here's where we 'd continue exeCutIng, knowing that when
- We're re done, init will reap our status .*/
- Sleep(2 );
- Printf ("Second child, parent pid = % d \ n", Getppid ());
- Exit (0 );
- }
- If(Waitpid (pid, NULL, 0 )! = Pid)/* Wait for first child */
- Err_sys ("Waitpid error");
- /* We're the parent (the original process); we continue executing,
- Knowing that we're not the parent of the second child .*/
- Exit (0 );
- }
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.