Waitpid and wait usage in linux

Source: Internet
Author: User
The header file # include & lt; sys/types. h & gt; # include & lt; sys/wait. h & gt; define the function pid_twait (int * status); function description: wait () will temporarily stop the execution of the current process until a signal arrives or the child process ends ....
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. /******* Waitpid. c-Simple wait usage **********/# include # Include # Include # Include # Include Int main (void) {pid_t childpid; int status; childpid = fork (); if (-1 = childpid) {perror ("fork ()"); exit (EXIT_FAILURE);} else if (0 = childpid) {puts ("In child process"); sleep (3); // let the sub-process sleep for 3 seconds, look at the behavior of the parent process printf ("\ tchild pid = % d \ n", getpid (); printf ("\ tchild ppid = % d \ n ", getppid (); exit (EXIT_SUCCESS);} else {waitpid (childpid, & status, 0); puts ("in parent"); printf ("\ tparen T pid = % d \ n ", getpid (); printf (" \ tparent ppid = % d \ n ", getppid ()); printf ("\ tchild process exited with status % d \ n", status);} exit (EXIT_SUCCESS);} [root @ localhost src] # gcc waitpid. c [root @ localhost src] #. /. out In child process child pid = 4469 child ppid = 4468in parent pid = 4468 parent ppid = 4379 child process exited with status 0 [root @ localhost src] # if the above "waitpid (childpi D, & status, 0); "line comment out. the program execution result is as follows: [root @ localhost src] #. /. out In child processin parent pid = 4481 parent ppid = 4379 child process exited with status 1331234400 [root @ localhost src] # child pid = 4482 child ppid = 1 child process has not exited, the parent process has exited. Author: kongweile
Related Article

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.