"Go" linux:waitpid function

Source: Internet
Author: User
Tags terminates

Original URL: http://blog.csdn.net/jifengszf/article/details/3067841

"Waitpid system call"

Function Description:
Wait for the process to change its state. All of the following calls are used to wait for a change in the state of the child process to get the child process information that the state has changed. The state change can be considered as: 1. The child process has been terminated. 2. The signal causes the child process to stop executing. 3. Execution of the signal recovery sub-process. In the case of a child process termination, the wait call will allow the system to release the resources associated with the child process. If wait is not performed, the terminated child process stays in the "zombie" state.

If a child process is found to have changed state, these calls are returned immediately. Conversely, the call is blocked until the child process state changes or is interrupted by a signal processing handle (if the system call is not restarted by the Sigaction Sa_restart flag).

The wait system call suspends the currently executing process until one of its child processes terminates. Waitpid suspends execution of the current process until the specified sub-process state has changed. By default, Waitpid only waits for a child process to terminate the state, but this behavior can be changed by options. Waitid system calls provide more granular control over which sub-process state changes are awaited.

The child process is terminated and the parent process has not yet performed a wait operation on it, and the child process goes to a "zombie" state. A process in which the kernel is "zombie" retains the least amount of information (process identity, termination state, Resource usage information), and then the parent process can get child process info when it executes wait. As long as a zombie process is not removed from the system through wait, it will occupy a field in the kernel process table. If the process table is filled, the kernel will no longer be able to generate new processes. If the parent process has been terminated, its dead child process will be adopted by the INIT process and will automatically execute wait to remove them.


Usage:
#include <sys/types.h>
#include <sys/wait.h>

pid_t Wait (int *status);
pid_t waitpid (pid_t pid, int *status, int options);
int Waitid (idtype_t idtype, id_t ID, siginfo_t *infop, int options);


Parameters:
PID: Possible values are as follows

Less than-1//means waiting for all its process group identities to be equal to the PID absolute value of the child process.
-1//means waiting for any child process.
0//means waiting for any process whose group identity is equal to the calling process group identity.
A process that is greater than 0//means waiting for its process identity to be equal to PID.

Status: Points to the return status of the child process, which can be retrieved using the following macro

wifexited (status)//return True as fruit the process terminates normally, for example: by calling exit (), _exit (), or from the return statement of Main ().
Wexitstatus (status)//returns the exit status of the child process. This should come from the parameter specified when the child process calls exit () or _exit (), or the lowest byte from the main internal return statement parameter. Only wifexited should be used if the return is true.
wifsignaled (status)//return True as fruit the process is terminated by the signal
Wtermsig (status)//returns the number of signals that caused the child process to terminate. Only wifsignaled should be used if the return is true.
Wcoredump (status)//return True as fruit the process causes the kernel to dump. Only wifsignaled should be used if the return is true. Not all platforms support this macro and should be placed in #ifdef wcoredump ... #endif内部.
wifstopped (status)//return TRUE if the signal causes the child process to stop executing.
Wstopsig (status)//returns the number of signals that caused the child process to stop executing. Only wifstopped should be used if the return is true.
wifcontinued (status)//return TRUE if the signal causes the child process to continue executing.

Options: Can be a combination of 0 or more of the following symbolic constants through or operations

Wnohang//If no child process exits, return immediately
wuntraced//If there is a process in the stopped state will cause the call to return.
wcontinued//If the stopped process continues to run due to the arrival of the Sigcont signal, the call will return.

The following are Linux-specific options that cannot be used for Waitid
__wclone//Wait only for "clone" of the child process. A "clone" process is a process that terminates without sending a signal to the parent process or sending a SIGCHLD signal to the parent process.
__wall//waits for all types of child processes, including "clone" and "Non-clone".
__wnothread//will not wait for descendants of other threads of the same thread group.

Wuntraced and wcontinued function only if the SIGCHLD signal does not have a sa_nocldstop flag set.

Idtype,id: These two parameters together indicate which sub-processes should be selected for waiting, possibly

Idtype = = p_pid//waits for the process to identify the child process that matches the ID.
Idtype = = P_pgid//waits for the process group to identify any child processes that match the ID.
Idtype = = P_all//wait for any child process, id not function

The child process status change flags of interest that can be specified in the options parameter have the following constants, which can be combined by an OR operation

wexited//waits for a child process that has been terminated.
wstopped//Waits for the child process to be executed because the signal has stopped.
wcontinued//Waits for the child process to execute because the signal has resumed.
Wnohang//function as Waitpid.
Wnowait//preserves the waiting state of the child process, and subsequent wait calls can get the status information of the child process again.

INFOP: When a successful return is performed, Waitid populates the following fields of the struct that Infop points to

Si_pid//child process identity.
The real user identity of the SI_UID//child process.
Si_signo//is always set to SIGCHLD.
The exit status of the Si_status//child process, or a signal that causes the child process to exit, stop execution or resume execution, needs to be interpreted according to the Si_code field.
Si_code////possible value has cld_exited (child process call _exit exit), cld_killed (child process is signaled), cld_stopped (signal causes child process to stop execution), cld_continued (signal recovery child process continues execution).

Return Description:
Wait (): When executed successfully, returns the identity of the terminating child process. Failed to return-1;
Waitpid (): A child process identity that returns a status change when executed successfully. Failure returns 1 if the Wnohang flag is specified and the process state specified by the PID does not change, 0 is returned.
Waitid (): The successful execution or the Wnohang flag is set and the ID specified by the child process state is not changed when the 0 is returned. Failed to return-1.

Error values may have the following
Echild: The process specified by the parameter does not exist or is not a child process that invokes the process
Eintr;wnohang is not set, while capturing a non-blocking signal or SIGCHLD signal
Invalid einval:options parameter

Example:

$./a.out &
Child PID is 32360
[1] 32359
$ kill-stop 32360
Stopped by signal 19
$ Kill-cont 32360
Continued
$ kill-term 32360
Killed by Signal 15
[1]+ done./a.out
$

#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>

int main (int argc, char *argv[])
{
pid_t Cpid, W;
int status;

Cpid = fork ();
if (cpid = =-1) {perror ("fork"); exit (exit_failure);}

if (Cpid = = 0) {/* Child process Execution */
printf ("Child PID is%ld/n", (long) getpid ());
if (argc = = 1)
Pause (); /* Wait for signals */
_exit (Atoi (argv[1));

} else {/* Parent Process Execution */
do {
W = waitpid (Cpid, &status, wuntraced | wcontinued);
if (w = =-1) {perror ("Waitpid"); exit (exit_failure);}

if (wifexited (status)) {
printf ("Exited, status=%d/n", Wexitstatus (status));
} else if (wifsignaled (status)) {
printf ("Killed by Signal%d/n", Wtermsig (status));
} else if (wifstopped (status)) {
printf ("Stopped by Signal%d/n", Wstopsig (status));
} else if (wifcontinued (status)) {
printf ("continued/n");
}
} while (! wifexited (status) &&! wifsignaled (status));
Exit (exit_success);
}

"Go" linux:waitpid function

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.