[Learning Notes] parent process wait and waitpid

Source: Internet
Author: User
Tags function prototype terminates

1. Why wait and Waitpid appear

SIGCHLD

Q When a child process exits, the kernel sends a SIGCHLD signal to the parent process, and the exit of the child process is an asynchronous event (the child process can terminate at any time the parent process runs)

When the Q process exits, the kernel resets the subprocess to zombie state, a process known as a zombie process that retains only the smallest number of kernel data structures so that the parent process queries the exit status of the child process.

The parent process can query the exit state of the child process with the Wait/waitpid function

2. Wait and Waitpid function usage

Wait

q  header file <sys/types.h> and <sys/wait.h>

q  function function: When we start a process with fork, the child process has its own life and will run independently. Sometimes we need to know if a child process is over, and we can schedule the parent process to finish after the child process through wait.

q  function prototype

q  pid_t Wait (int *status)

q  function parameter

q  status: This parameter gets the information you are waiting for the child process

q  return value:

q  successfully waits for the child process function to return, the return value is the ID of the waiting child process

q  Wait system call causes the parent process to pause until one of its child processes has ended.

q  returns the PID of the child process, which is usually the end of the child process

q  state information that allows the parent process to determine the exit state of the child process, that is, the value returned from the main function of the child process or the exit code of the exit statement in the child process.

q  If status is not a null pointer, the state information is written to the location it points to

Wait gets status post detection processing

Macro Definition Description

wifexited (status) Returns a non-0 value if the child process ends normally

Wexitstatus (status) Returns the child process exit code if wifexited nonzero

wifsignaled (status) child process terminates because of a capture signal, returns a value other than 0

Wtermsig (status) if wifsignaled nonzero, returns the signal code

wifstopped (status) Returns a non-0 value if the child process is paused

Wstopsig (status) if wifstopped nonzero, returns a signal code

Waitpid

Q function: Used to wait for the end of a particular process

Q Function Prototype:

pid_t waitpid (pid_t pid, int *status,int options)

Q parameter:

Q Status: If it is not empty, it writes the status information to the location it points to.

Qoptions: The most useful option for allowing changes to waitpid behavior is Wnohang, which is to prevent waitpid from suspending the execution of the caller

Q return Value: If the ID of the waiting child process is returned successfully, the failure returns-1

The interpretation of the p I d parameter for Waitpid is related to its value:

Q pid = =-1 waits for any child process. So waitpid is equivalent to wait in this function.

The Q- pid > 0 waits for its process I D to be equal to the P I d Sub-process.

Q pid = = 0 waits for its group I d to be equal to any child process that invokes the group I d of the process. In other words, the process is in the same group as the caller process.

Q- PID <-1 waits for its group I D to be equal to the absolute value of p I D for any of the sub-processes.

3, wait PK Waitpid

Wait and waitpid differences and connections

Q before a child process terminates, wait causes its callers to block, and Waitpid has a selection that allows the caller to not block.

Q Waitpid does not wait for the first terminating child process-it has several choices that can control the specific process it waits for.

Q Actually the wait function is a special case of the Waitpid function.

Zombie Process

Q When a child process finishes running, the association between it and its parent process is also persisted until the parent process ends up running properly or the parent process calls wait to terminate.

The data item in the process table that represents the child process is not released immediately, and although it is no longer active, the child process remains in the system because its exit code needs to be saved for subsequent wait calls in the parent process. It will be called a "zombie process"

How to avoid zombie processes

Q Call the wait or Waitpid function to query the child process exit status, and the parent process of this method will be suspended.

Q If you do not want the parent process to hang, you can add a statement to the parent process: signal (sigchld,sig_ign), which indicates that the parent process ignores the SIGCHLD signal, which is sent to the parent process when the child process exits.

#include <sys/types.h>#include<unistd.h>#include"errno.h"#include"stdio.h"#include"stdlib.h"#include"string.h"#include<sys/types.h>#include<sys/wait.h>voidLoopfunc (intnum) {printf ("Loopfunc ()%d....\n", num);}intMainvoid ){    intI, j =0; printf ("hello...\n"); intProcnum =0; intLoopnum =0; printf ("\ n Please enter the number of processes to run"); scanf ("%d", &procnum); printf ("\ n Please enter the number of laps per process"); scanf ("%d", &loopnum);    pid_t pid; printf ("befor fork pid:%d\n", Getpid ()); //Fork Sub-process     for(i=0; i<procnum; i++) {PID=Fork (); if(PID = =-1)        {            //On failure, A-1 'll be Retur//errnoPerror ("Fork Err"); return 0; }        if(PID = =0)        {             for(j=0; j<loopnum; J + +) {Loopfunc (j); } exit (0);//let the child process run the circle process does not participate in the next fork        }        if(PID >0)//greater than 0 is the parent process        {            ; }    }    //Exitprintf"After fork \ n"); intMypid;  while(Mypid = Waitpid (-1, NULL, Wnohang)) >0) {printf ("Child dead:%d\n", Mypid); }return 0;}

Copy to Google TranslateTranslation Results

[Learning Notes] parent process wait and waitpid

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.