#include <sys/types.h>#include <sys/wait.h>wait(int*status); waitpidint*statusPtrint options);
Now you know wait
waitpid
what might happen to a process that is called or:
- If all of its child processes are running, it is blocked .
- If a child process has been terminated, the waiting parent process gets to the terminating state, then the terminating state of the child process is taken back immediately.
- If he does not have any child processes, an immediate error is returned.
If a process is called because it received a SIGCHLD
signal wait
, it can expect to wait
return immediately. However, if you call at any time, wait
the process may block.
#include "apue.h"#include <sys/wait.h>intMain (void) {pid_t pid1,pid2;printf("Before fork\n");if((pid1=Fork()) <0){printf("Fork Error"); }Else if(pid1==0){printf("Child process ' spid=%d\ n", Getpid ());Sleep(3); }Else{pid2=wait(NULL);printf("Wait process ' s pid=%d\ n", PID2); }Exit(0); }
Results:
When the program is running, it is obvious that you have waited three seconds for the last line of output. That is, the parent process waits for the child process to end. The parent process can catch the child process and then get wait
the results you want to get.
Parameter status:
status
The parameter is an integer pointer . If status
the value of the parameter is not NULL
, it wait
will take out the state when the process exits and enter it, which is an integer value ( int
) indicating whether the child process exits normally or is abnormally terminated (a process can also be signaled by other processes). And the return value at the normal end, or the end of the signal. Since this information is stored in a different bits of an integer , it can be cumbersome to read in a regular way, and people have designed a special set of macros ( macro
) to do the work, so let's take a look at the two most common ones:
WIFEXITED(status)
This macro is used to indicate whether a child process is exiting normally, and if so, it returns a value other than 0 .
(Note that, although the name is the same, the argument here is status
different from the wait
unique parameter – the pointer to an integer status
, but the integer that the pointer points to, remember not to confuse it.) )
WEXITSTATUS(status)
When a WIFEXITED
value other than 0 is returned, the description exits normally. We can use this macro to extract the return value of the child process , which is returned if the child process call exit(5)
exits, WEXITSTATUS(status)
5
or 7 if the child process is called exit(7)
WEXITSTATUS(status)
. Note that if the process does not exit normally, that is, WIFEXITED
return 0
, this value is meaningless.
Waitpid function
When we need to wait for the function of a particular process , we need to use the function at this time waitpid
. As seen from the previous waitpid
function prototypes, we all know that there is a pid_t
parameter.
The explanations are as follows:
Pid=-1
, waiting for either child process. and wait
equivalent.
Pid>0
Wait for its process ID
to pid
end with an equal child process, that is, the process waiting for the process number Pid
to be.
Pid==0
, waiting for its group ID
ID
to equal the any child process that invokes the process group.
Pid<-1
, waiting for ID
pid
any child process whose group equals absolute value.
Waitpid
Returns the process that terminates the child process ID
. The terminating state of the child process is stored in a status
pointed storage unit.
Waitpid
The function provides wait
three functions that the function does not provide.
Waitpid
You can wait for a specific process, and wait
then return the state of either of the terminating child processes.
Waitpid
Non-blocking versions are available. Sometimes the user wants to get the status of a child process, but does not want to block.
Waitpid
Support Job control.
Waitpid return values and errors
waitpid
The return value is wait
slightly more complex, with a total of 3 cases:
waitpid
returns the process of the child process that was collected when normal return ID
;
- If the option is set and the
WNOHANG
call waitpid
finds that no child processes that have exited can be collected, it is returned 0
;
- If an error occurs in the call, it is returned
-1
and is then errno
set to the corresponding value to indicate where the error occurred;
When the pid
indicated child process does not exist, or the process exists, but not the child process of the calling process, an waitpid
error is returned, at which point it errno
is set to ECHILD
;
waitpid
Waits until a child process ends. When a child process finishes, the state of the end child process is saved in the integer that the parameter pointer points to, and the parent process, the original function, resumes execution. If the waitpid
child process has ended before the parent process is called, the operating system returns the saved information immediately to the function.
Unix Multi-process-destroy zombie process-wait () and waitpid () functions