Process waiting in linux (wait and waitpid)

Source: Internet
Author: User
Wait (waiting for sub-process interruption or termination) header file include & lt; policypesh & gt; include & lt; s
Wait (waiting for the sub-process to be interrupted or terminated)
Header file # include # Include Define the pid_t wait (int * status) function. 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 wait () is called, wait () immediately returns the child process end status value. The end status value of a sub-process is returned by the status parameter, and the process identifier of the sub-process is returned together. If you do not care about the end status value, you can set the parameter status to NULL. For the end status value of the sub-process, see waitpid (). If the execution is successful, the sub-process identifier (PID) is returned. If an error occurs, the return value is-1. The cause of failure is stored in errno. Pid_t pid1; int status = 0; I = wait (& status); I returns the ID of the child process; PIDstatus stores the end status of the child process; WEXITSTATUS (status) is available) get the exit (3) status of the sub-process, which is 3; waitpid (waiting for the sub-process to interrupt or stop) header file # include # Include Define the 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 sub-process ends. If the child process has ended when wait () is called, wait () immediately returns the child process end status value. The end status value of a sub-process is returned by the status parameter, and the process identifier of the sub-process is returned quickly. If you do not care about the end status value, you can set the parameter status to NULL. The pid parameter is the child process identifier to be waited for. other values have the following meanings: pid <-1 any child process whose ID is the absolute value of the pid. Pid =-1 waits for any sub-process, which is equivalent to wait (). Pid = 0 wait for any sub-process with the same process ID as the current process. Pid> 0: wait for any sub-process whose ID is pid. The option parameter can be 0 OR a combination of OR below: if WNOHANG does not have any child processes that have ended, it will be returned immediately without waiting. WUNTRACED: if the sub-process is paused, it will return immediately, but the end state will not be ignored. The sub-process's end status is returned and stored in status. There are several macros under it to determine the end: WIFEXITED (status) if the sub-process ends normally, it is not 0. WEXITSTATUS (status) gets the end code returned by the sub-process exit (). generally, WIFEXITED is used to determine whether the macro can be used until it ends normally. WIFSIGNALED (status) if the sub-process ends because of a signal, the macro value is true WTERMSIG (status) to obtain the signal code that the sub-process stops due to a signal, this macro is usually used only after WIFSIGNALED is used for determination. WIFSTOPPED (status) if the sub-process is paused, the macro value is true. This is generally the case only when WUNTRACED is used. WSTOPSIG (status) gets the signal code that causes the sub-process to pause. this macro is generally used only after WIFSTOPPED is used for determination. If the execution is successful, the sub-process identifier (PID) is returned. If an error occurs, the return value is-1. The cause of failure is stored in errno.
Oot @ wl-MS-7673:/home/wl/desktop/c ++ # cat-n wait. cpp 1 # include
     
      
2 # include
      
       
3 # include
       
        
4 # include
        
         
5 # include
         
          
6 # include
          
            7 # include
           
             8 9/* 10 * program entry 11 **/12int main (void) 13 {14pid_t child; 15 16/* create sub-process */17if (child = fork ()) =-1) 18 {19 printf ("Fork Error \ n"); 20 exit (1); 21} 22 else 23 {24if (child = 0) // sub-process 25 {26 printf ("the child process is run \ n"); 27 sleep (1); // sub-process sleeps for one second, but it does not run the parent process 28 printf ("I am the child: % d \ n", getpid (); 29 exit (0 ); 30} 31 else // parent process 32 {33 wait (NULL); // wait until the child process exits, the parent process runs 34 printf ("the father process is run \ n"); 35 printf ("I am the father: % d \ n", getpid ()); 36 return 0; 37} 38} 39} 40 41root @ wl-MS-7673:/home/wl/desktop/c ++ # g ++ wait. cpp-o waitroot @ wl-MS-7673:/home/wl/desktop/c ++ #. /wait the child process is runI am the child: 19742the father process is runI am the father: 19741root @ wl-MS-7673:/home/wl/desktop/c ++ #. /wait
           
          
         
        
       
      
     

In this example, the parent process is executed only after the child process ends .~

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.