Fork () function execution process, orphan process and zombie process

Source: Internet
Author: User
Tags terminates

Speaking of fork, you have to mention cow (copy on write), which is "copy on write". That is, when the fork occurs, the child process simply does not copy the parent process's memory page, but shares it with the parent process. When a child process or parent process needs to modify a memory page, Linux copies the memory page to the modifier and then modifies it so that the parent-child process does not share any memory from the user's point of view. Cow is the process to write the shared memory page, first copy and then rewrite.

With cow technology, when fork, the child process also needs to copy the parent process's page table. The cost of this copy is very small, and it can take a few clock cycles for the CPU.

1. The orphan process

Orphan process: is a process without a parent process. When a parent process exits, and one or more of its child processes are still running, those child processes become orphans. The orphan process will be adopted by the INIT process (process number 1) and the Init process completes the state collection for them.

2. Zombie Process

Zombie Process: A child process exits if its parent process has not yet called wait () or waitpid (). This child process is the zombie process.

3. Harm

The zombie process consumes system resources and, if many, can severely affect the performance of the server
The orphan process does not consume system resources and is ultimately managed by the Init process to release it from the init process.
Process Flow:
As long as Daddy waits for Wait (sys/wait.h) son, son will become Ghost Zombie (zombie), UNIX default daddy always want to see son after death State (in order to Revenge)   
  if   father to see    before his son;
  Son will be adopted by Init (id   =   1), the final result is zombie son thoroughly Goodbye, system resource release   
  else    
      {  
        son's zombie will always exist, system resource occupied ...   
         if   daddy dead    
             son will be adopted by Init (id   =   1), the final result is zombie son thoroughly Goodbye, system resources released   
   
      else   similar son zombie more and more, the system is waiting to die!!!   
   } 

signal (SIGCHLD, sig_ign); Ignoring the SIGCHLD signal, which is often used as a technique for concurrent server performance
Because concurrent servers often fork many child processes, the end of a child process requires
The server process goes to wait to clean up resources. If this signal is handled in a way that is set to
Ignore, allowing the kernel to transfer the zombie process to the INIT process to handle, eliminating the
A large number of zombie processes occupy system resources. (Linux only)

4. How to prevent zombie processes

First understand how to create a zombie process:
1, the child process after the end of the parent process issued a SIGCHLD signal, the parent process by default ignored it
2. The parent process does not call the wait () or waitpid () function to wait for the end of the child process
The first method: Capture the SIGCHLD signal and call the wait function inside the signal processing function
The UNIX Network programming code for Richard Steven

Int
Main (int argc, char **argv)
{
...
Signal (SIGCHLD, SIG_CHLD);
for (;
}
...
}

void
SIG_CHLD (int signo)
{
pid_t pid;
int stat;

while (PID = Waitpid ( -1, &stat, Wnohang)) >; 0)
printf ("Child%d terminated/n", PID);
Return
}

The second method: two times fork (): reproduced
In the advanced programming for Unix environment, there is a very clear description of this in section 8.6.

Instance
Recall 8. Discussion of the zombie process in section 5. If a process is going to fork a child process, but it does not require it to wait
Child process terminates, and does not want the child process to be in a zombie state until the parent process terminates, the trick to implement this requirement is to call F o r k
Twice. Program 8-5 implements this.
Call S L e E p in the second child process to ensure that the first child process is terminated when printing the parent process I d. After F o r K,
Both parent and child processes can continue--we cannot predict which one will be executed first. If you do not make the second child process sleep, the
After F o r K, it may execute first than its parent process, so it prints the parent process I d will be the parent process that created it, not
I-n i-t process (process ID 1).

#include <sys/types.h>
#include <sys/wait.h>
#include "Ourhdr.h"

int main (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 calls exit () in the statement above.
                    Here's where we ' d continue executing, knowing that's when
                    we ' re done, Init would 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. */

Exit (0);
}

Fork () function execution process, orphan process and zombie process

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.