Process--wait with waitpid, zombie process and orphan process

Source: Internet
Author: User

Zombie Process: The child process terminated, but the parent process did not reclaim the resource PCB for the child process. Make it a zombie process

Orphan process: The parent process ends with the child process, which causes the child process to lose the parent process, and this time the subprocess is adopted by the 1th process init process as an orphan process

To prevent the above two cases, we should be sure to reclaim all the resources of the child process before the end of the parent process

So there was wait and waitpid.

 #include <sys/types.h> #include  <sys/wait.h>pid_t Wait ( int  *status); pid_t waitpid (pid_t pid,  int  *status, int   options); status is an outgoing parameter. Waitpid PID Parameter selection:  <-1   reclaims any child processes within the specified process group  =-1   reclaims any child process  = 0   recycle and current call waitpid all child processes of a group  > 0  recycle the child process with the specified ID 

At the end of a process, all the file descriptors are closed, all the memory space is released, but his PCB is still there, some state information of the child process is saved, if the process exits normally, the state of normal exit is saved, if the exit is not normal, then which signal causes the process to exit, The parent process can see this information through wait and waitpid, and completely clear, we know that the shell is the parent process of all processes, in the shell can be viewed through the exit status number, so can be viewed and cleared through the shell, if a process exits, The parent process does not call the wait and waitpid process cleanup, then a zombie process is created, and normally the zombie process is cleaned up by the parent process.

The following is an unhealthy program, so that the parent process does not end

#include <unistd.h>#include<stdlib.h>intMainvoid) {pid_t pid=Fork (); if(pid<0) {perror ("Fork"); Exit (1); }    if(pid>0) {/*Parent*/         while(1); }    /* Child*/    return 0;}

Above, the parent process has been in the while (1) loop, so that the parent process does not exit, view PS aux can find the parent process state is R, the child process status is Z (Zombie State Zombie)

 The wait or Waitpid function returns the cleaned child process ID if the call succeeds, and returns 1 if the call is faulted. A situation that may occur when a parent process calls wait or waitpid:

    1. Blocked (if all of its child processes are still running).
    2. The termination information for the tape process is returned immediately (if a child process has been terminated and is waiting for the parent process to read its termination information).
    3. An error is returned immediately (if it does not have any child processes).

See, Wai. Successful return value

*******************************************************************************************

  For wait and waitpid two functions, the difference is:

    1. If all the child processes of the parent process are still running, calling wait causes the parent process to block, and if you specify Wnohang in the options parameter when you call Waitpid, you can return 0 immediately without blocking the parent process.
    2. Wait waits for the first terminating child process, and waitpid can specify which child process to wait through the PID parameter.

Therefore, calling wait and waitpid not only can get the terminating information of the child process, but also can cause the parent process to block waiting for the child process to terminate and play the role of inter-process synchronization. If the parameter status is not a null pointer, the terminating information for the child process is passed through this parameter, and the status parameter can be specified as null if it is only for synchronization and does not care about the child process's termination information.

1#include <stdio.h>2#include <unistd.h>3#include <stdlib.h>4#include <sys/types.h>5 6 intMainvoid)7 {8 pid_t Pid,pid_c;9 Ten     intn =Ten; OnePID =fork (); A  -     if(PID >0 ) -{/*In parent*/ the          while(1) -         { -printf"I am Parent%d\n", Getpid ()); -             //Wait is a blocking function that waits for the child process resource to be reclaimed, and if there are no child processes, wait returns-1 +Pid_c =Wait (NULL); -printf"wait for child%d\n", Pid_c); +Sleep1); A         } at     } -     Else if(PID = =0) -{/* in child*/ -printf"I am Child%d\n", Getpid ()); -SleepTen); -     } in  -     return 0; to } +  -  the Operation Result: *I am Parent4797 $I am Child4798Panax NotoginsengWait forChild4798 -I am Parent4797 theWait forChild-1 +I am Parent4797 AWait forChild-1 theI am Parent4797

Orphan process

#include <stdio.h>#include<unistd.h>#include<stdlib.h>intMainvoid) {pid_t pid; intn=Ten; PID=Fork (); if(PID >0)    {//the parent process exits after the creation is complete.printf"I am parent\n"); Exit (0); }    Else if(PID = =0)    {//at this point the parent process exits, the child process is taken over by the INIT program, the process's parent process number becomes 1         while(n--) {printf ("I am%d, my parent is%d\n", Getpid (), Getppid ()); Sleep (1); }    }    Else{perror ("Fork"); Exit (-1); }    return 0;} Operation Result: I am4813, My Parent is 1I am4813, My Parent is 1I am4813, My Parent is 1I am4813, My Parent is 1I am4813, My Parent is 1I am4813, My Parent is 1

Waitpad

1#include <sys/types.h>2#include <sys/wait.h>3#include <unistd.h>4#include <stdio.h>5#include <stdlib.h>6 7 intMainvoid)8 {9 pid_t pid;TenPID =fork (); One     if(PID <0) A     { -Perror ("Fork Failed"); -Exit1); the     } -  -     if(PID = =0) -{// in child +         inti; -          for(i =3; i >0; i--) { +printf"The child %d\n", Getpid ()); ASleep1); at         } -Exit3);//returns 3, which can be seen at run time -         //the child process sleeps for 3 seconds and then exits. -     } -  -     Else in{//In parent -         intStat_val; toWaitpid (PID, &stat_val,0);//waits for the child process to be reclaimed in a blocking manner, and the third parameter, 0, indicates the blocking mode +         if(wifexited (Stat_val))//Normal Exit -printf"Child exited with code%d\n", Wexitstatus (Stat_val)); the         Else if(wifsignaled (Stat_val))//See what signal is off *printf"Child terminated abnormally, signal%d\n", Wtermsig (Stat_val)); $     }Panax Notoginseng     return 0; -}

Process--wait with waitpid, zombie process and orphan 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.