Usage of waitpid and wait in "Turn" Linux

Source: Internet
Author: User
Tags define function

Original URL: http://www.2cto.com/os/201203/124851.html

Wait (waits for the child process to break or end)


Header file       #include <sys/types.h>      #include <sys/wait.h> define function pid_t Wait (int * status); function description:    Wait () temporarily stops the execution of the current process until a signal arrives or the child process ends.     If the child process has ended when you call Wait (), wait () returns the child process end status value immediately.     The end state value of the child process is returned by the parameter status, and the process identifier of the child process is returned together.     The parameter status can be set to NULL if the end status value is not the intention.       End status values for subprocess refer to Waitpid ()      If successful execution returns a child process identifier (PID) and returns a return value of 1 if an error occurs. The reason for failure is stored in errno. pid_t  pid1;  int status=0;     i=wait (&status); I return the identification code of the child process, the end state of the child process is stored in pidstatus, and the Status of exit (3) of the child process can be obtained by wexitstatus (status). , then 3;      waitpid (Waiting for the child process to break or end) header file       #include <sys/types.h>      #include <sys/wait.h> define function  pid_t waitpid (pid_t pid,int * status,int options); function Description:    Waitpid () will temporarily stop the execution of the current process until a signal arrives or the child process ends.   If the child process has ended when you call Wait (), then wait () returns the child process end status value immediately.     The end state value of the child process is returned by the parameter status, and the process identifier of the child process returns quickly.     If the end state value is notThe number status can be set to NULL.    parameter PID is the child process identification code to wait, other numeric meanings are as follows:    &NBSP;PID&LT;-1 wait for the process group identifier to be the PID absolute value of any subprocess.      pid=-1 waits for any child process, equivalent to wait ().                pid=0     Wait for the process group identifier to be the same as the current process for any child processes.           pid>0     waits for any child process identification code to be a child of the PID.      parameter option can be combined:    Wnohang for 0 or below if no completed subprocess is returned immediately, not waiting.     Wuntraced returns if the child process enters a paused execution condition, but the end state is ignored.      The end state of the child process is returned and stored in status, there are several macros at the bottom to determine the end condition:    wifexited (status) is a non-0 value if the child process ends normally.    wexitstatus (status) Gets the end code returned by the child process exit (), typically using wifexited to determine whether the macro ends properly before it can be used.     wifsignaled (status) If the child process ends because of a signal, this macro value is true     Wtermsig (status) Gets the signal code that the child process is signaled to abort, typically first This macro is not used until wifsignaled is judged.     wifstopped (status) This macro value is true if the child process is in a paused execution condition. This is generally the case only if you are using wuntraced.     Wstopsig (status) Gets the signal code that causes the child process to pause, typically using wifstopped to determine the macro before use.     If successful execution returns the child process identification Number (PID), if there is an errorOccurs, the return value-1 is returned. The reason for failure is stored in errno.            /*******    waitpid.c-simple wait usage*********/ # Include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #include <stdio.h> #include <stdlib.h> int main (void) {    pid_t childpid;    int status;      &N Bsp Childpid = fork ();        if (-1 = = childpid)     {        perror ( "Fork ()");        exit (exit_failure);   }    Else if (0 = = childpid)     {        puts ("in child process");        sleep (3);//Let the child process sleep for 3 seconds to see the behavior of the parent process & nbsp     printf ("\tchild pid =%d\n", Getpid ());        printf ("\tchild ppid =%d\n", GETP PID ());        exit (exit_success);   }    else     {       Waitpid (childpid, &status, 0);        puts ("in parent");      &NB Sp printf ("\tparent pid =%d\n", Getpid ());        printf ("\tparent ppid =%d\n", Getppid ()  ); &N Bsp     printf ("\tchild process exited with status%d \ n", status);   }    exit (exit_s uccess);}  [[email protected] src]# gcc waitpid.c [[email protected] src]#./a.out In Child Process         Child PID = 4469        Child ppid = 4468in parent      &NB Sp Parent PID = 4468        Parent ppid = 4379        child process exited with Statu S 0 [[email protected] src]#   if will above "Waitpid (Childpid, &status, 0);" The program executes as follows: [[email protected] src]#./a.out in child processin parent        Parent PID = 4481        Parent Ppid = 4379        child process exited with status 1331234400 [[email protected] src]# &NB Sp Children pid = 4482        Child ppid = 1  The subprocess has not exited and the parent process has exited.

"Go" Linux waitpid and wait usage

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.