Linux wait and waitpid and kill

Source: Internet
Author: User
1. Linux wait

1) function: wait until the sub-process is interrupted or terminated
2) header files
# Include <sys/types. h>
# Include <sys/Wait. H>
3) Function Definition: pid_t wait (int * status );
4) 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 quickly. 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 sub-process identifier (PID) is returned.
5) Return Value
-1. The cause of failure is stored in errno.

2. Linux waitpid

1) function: wait until the sub-process is interrupted or terminated
2) header files
# Include <sys/types. h>
# Include <sys/Wait. H>
3) Function Definition: pid_t waitpid (pid_t PID, int * status, int options );
4) 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 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 identifier of the child process to wait. Other values are as follows:
PID <-1 any child process that waits for the Process Group Identifier to be 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 the following or combination:
If wnohang does not have any child process that has ended, it will return immediately and will not wait.
Wuntraced: If the sub-process is paused, it will return immediately, but the end state will not be ignored.
The sub-process's end state is returned and stored in status. There are several macros under it to determine the end state:
Wifexited (Status) is not 0 if the sub-process ends normally.
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) gets the signal code of the sub-process terminated due to the signal. Generally, this macro is 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.
5) Return Value

If the execution is successful, the sub-process PID is returned, and-1 is returned if an error occurs. The cause of failure is stored in errno.

3. Differences between wait and waitpid

  Waitpid provides three features not available in wait:
1) waitpid allows us to wait for the specified process
2) waitpid provides a non-blocking wait
3) waitpid supports work control

 

4. sigchld

The Processing Method of the kernel signal to the subprocess termination (sigchld) is different from that of other signals. When a process detects that a sub-process is terminated, the process does not receive the signal by default. If the parent process executes the system to call wait, the process will wake up from the system call wait and return to the wait call to execute a series of subsequent operations for the wait call (find
The dead child process, release the child process's progress table), and then return from wait. The sigchld signal is used to wake up a sleep process on the priority that can be interrupted. If the process captures this signal, it will be transferred to the processing routine like normal signal processing. If the process ignores this signal, the system calls wait for different actions, because sigchld only serves to wake up a sleep
If a process with a higher priority is interrupted, the parent process that executes the wait call is awakened to continue the subsequent operations of the wait call, and then waits for other child processes.

If a process calls the signal system and sets the processing method of sigchld, and a sub-process of the process is in zombie state, the kernel sends a sigchld signal to the process.

 

5. Linux kill

1) function: used to send signals to any process group or process.

2) header files

# Include <sys/types. h>
# Include <signal. h>

3) Function Definition: int kill (pid_t PID, int sig );

4) Function Description

PID: the following four options may be selected:

1) if the PID is greater than zero, the PID indicates the process to be sent.
2) If the PID is equal to zero, the signal will be sent to all processes that belong to the same group as the process that calls kill.
3) when the PID is equal to-1, the signal will be sent to all processes that call the process that have the right to send the signal to it, except process 1 (init ).
4) when the PID is less than-1, the signal will be sent to the process marked by-PID.

Sig: the signal code to be sent. If the value is zero, no signal is sent, but the system will perform an error check. Generally, the SIG value is zero to check whether a process is still running.

 

5) Return Value

0 is returned when execution is successful.

-1 is returned for failure, and errno is set to one of the following values

Einval: the specified signal code is invalid (the SIG parameter is invalid)

Eperm: unable to send signals to the specified process due to insufficient Permissions

Esrch: the process or process group specified by the PID parameter does not exist.

 

 

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.