After a parent process creates a child process through fork, the execution sequence of the parent process and child process cannot be controlled. you can use vfork to create or the parent process to call the wait function.
For the difference between fork and vfork, refer to apue. I will also write relevant articles to explain them in the future.
This article mainly introduces wait and similar waitpid calls.
1. when a process is terminated, it sends a sigchld signal to its parent process. This asynchronous event can occur at any time when the parent process is running, including normal and abnormal termination. the process that calls wait and waitpid may have the following three situations:
- Blocking (if all its sub-processes are still running)
- The Process Termination status is normal (one of the sub-processes is terminated)
- Error returned (no sub-process)
The prototype is as follows:
# Include <sys/types. h>
# Include <sys/Wait. H>
Pid_t wait (int * statloc );
Function return: if the operation succeeds, the process ID is returned. If the operation fails, the process ID is-1.
Parameter description:
Statloc: if it is a null pointer, the return status of the sub-process is saved in the integer variable pointed to by the pointer; if it is a null pointer, the return status is omitted.
2. waitpid function:
If a process has several sub-processes, as long as one sub-process returns, wait returns. there are two methods to wait for a specified sub-process. first, the early UNIX system must call wait, and then compare the returned PID with the expected PID. If not, save the PID and continue to call wait until the process is terminated. the second is to use waitpid.
# Include <sys/types. h>
# Include <sys/Wait. H>
Pid_t waitpid (pid_t PID, int * statloc, int options );
Function return: if the operation succeeds, the process ID is returned. If the operation fails, the process ID is-1.
Parameter description:
PID:
- PID =-1 wait for any sub-process. At this time, waitpid is equivalent to wait.
- PID> 0: Wait for the sub-process whose ID is equal to the PID.
- PID = 0 any sub-process that waits for the group ID and the group ID of the called process.
- PID <-1 waits for any sub-process whose group ID is equal to the absolute value of PID.
For the waitpid function, an error occurs if the specified process or process group does not exist or the called process does not have any child process.
Options:
This parameter allows us to further control the waitpid operation. This parameter is either 0 or one of the following bitwise OR constants:
Wnohang: If the sub-process specified by the PID is not immediately available, the waitpid is not blocked, and the return value is 0.
Wuntraced: if a certain implementation supports job control, the status of any sub-process specified by the PID has been suspended and has not been reported since the suspension. the wifstopped macro determines whether the returned value corresponds to a paused sub-process.
3. Differences between waitpid and wait:
Waitpid provides three functions that cannot be implemented by Wait functions:
- Waitpid waits for a specific sub-process, and wait returns any terminated sub-process;
- Waitpid provides a non-blocking version of wait;
- Waitpid supports job control (with the wuntraced option ).
4. Macro used to check the termination status returned by the wait and waitpid functions:
The sub-process statuses returned by these two functions are saved in the statloc pointer. You can use the following three macros to check the status:
- Wifexited (Status): true if it is terminated normally. This can be executed
- Wexitstatus (Status): takes the 8-bit lower value that the sub-process sends to the exit or _ exit parameter.
- Wifsignaled (Status): true if an exception is terminated. This can be executed
- Wtermsig (Status): gets the signal number that causes the sub-process to terminate.
- Wifstopped (Status): If the sub-process is currently suspended, it is true.
- Wstopsig (Status): gets the signal number used to suspend the sub-process.
For more information about the instance code, see apue.