The Linux wait function is detailed

Source: Internet
Author: User
Tags function prototype

The Linux wait function is detailed

Why wait and waitpid occur sigchld--when a child process exits, the kernel sigchld the signal to the parent process, and the child process exits with an asynchronous event (the child process can terminate at any time the parent process runs)-when the child process exits, the kernel leaves the child process in zombie state. This process becomes 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 exit state of the parent process query subprocess can be used with the Wait/waitpid function
Wait gets staus after the detection processing macro definition  description wifexited (status) If the process child process ends normally, returns a non-0 value    wexitstatus (status) if wifexited nonzero, Returns the child process exit code wifsignaled (status) child process terminates because of a capture signal, returns a non-0 value    wtermsig (status) if wifsignaled nonzero, returns the signal Code wifstopped (status) If the process is paused, returns a non-0 value    wstopsig (status) if wifstopped nonzero, returns the signal code
 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <errno.h> #include <sys/types.h> #include <sys/wait.h>int main (int Arg,char *args[]) {pid_t pid=fork (    ); if (pid==-1) {printf ("fork () failed!        Error message:%s\n ", Strerror (errno));    return-1;        } if (pid>0) {int status=0;        printf ("Parent process \ n");        Wait (&status);        If wifexited (status)//wifexited the definition of the macro: Wait if exit ed {printf ("Child process return info code:%d\n", Wexitstatus (status));        }else if (wifsignaled (status)) {printf ("Child process signal Interrupt return Info code:%d\n", Wtermsig (status));        }else if (wifstopped (status)) {printf ("Child process pause return Info code:%d\n", Wstopsig (status)); }else {printf ("Other exit info!        \ n ");        }}else if (pid==0) {printf ("I am Child!\n");        Abort ();    Exit (100);    } printf ("Game is over!\n"); return 0;} 
The wait () function successfully returns the PID that waits for the subprocess, and the failure returns-1
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include < errno.h> #include <sys/types.h> #include <sys/wait.h>int main (int arg, char *args[]) {    pid_t pid = 0;< C1/>int i = 0, ret = 0;    for (i = 0; i < i++)    {        pid = fork ();        if (PID = =-1)        {            printf ("fork () failed! Error message:%s\n ", Strerror (errno));            return-1;        }        if (PID = = 0)        {            printf ("Child haved run!\n");            Exit (0);        }    }    The return value of the while (1)    {        //wait () function is the PID        ret = Wait (NULL) of the child process;        printf ("Sub-process pid=%d\n", ret);        if (ret = =-1)        {            //parent process wait () function blocking process, it is possible to be interrupted by other signals, need to do exception handling            if (errno = = eintr)            {                continue;            } Break            ;        }    }    printf ("Game is over!\n");    return 0;}

Waitpid function: An End Function prototype to wait for a particular process:    pid_t waitpid (pid_t pid, int *status, int options), parameter:    status if not empty, Writes the status information to the location it points to. Options    allow changing the behavior of waitpid, the most useful option is Wnohang, which is to prevent the waitpid to suspend the caller's execution of the return value: A successful return of the PID waiting for the child process, failed to return 1

The Linux wait function is detailed

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.