Once the process calls wait, it immediately blocks itself. Wait automatically analyzes whether a subprocess of the current process has exited. If it finds such a subprocess that has become a zombie, wait The information of this child process will be collected and returned after it is completely destroyed. If such a child process is not found, wait will block until it appears.
wait (waiting for interrupt or end of child process) related function waitpid, fork header file #include<sys/types.h>#include<sys/wait.h>definition function pid_t wait (int * status) The function says that wait() 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() will immediately return the child process end state value. The end state value of the child process is returned by the parameter status, and the process ID of the child process is returned as soon as possible. If you do not care about the end state value, the parameter status can be set to NULL. For the end state value of the child process, please refer to waitpid(). The return value returns the child process ID (PID) if the execution is successful, and returns -1 if an error occurs. The reason for the failure is in errno.
Example one #include<stdlib.h>#include<unistd.h>#include<sys/types.h>#include<sys/wait.h>int main(){pid_t pid;int status ,i;if(fork()= =0){printf(“This is the child process .pid =%d\ ”,getpid());exit(5);}else{sleep(1);printf (“This is the parent process ,wait for child...\ ”;pid=wait(&status);i=WEXITSTATUS(status);printf(“child’s pid =%d .exit status= %d\ ”, pid, i);}}
Execute This is the child process.pid=1501This is the parent process .wait for child...child’s pid =1501,exit status = 5
Example 2
#include<iostream>#include<unistd.h>#include<sys/wait.h>using namespace std;int main(void ) {pid_t pid; pid = fork(); if (pid<0)exit(0); else if (pid == 0){//If the child process sleeps for 20 seconds cout<<"children : " <<getpid()<<endl;sleep(20);}else{ cout<<"hello! i'm parent process!"<<endl;//If the parent process is Wait here pid_t pr = wait(N ULL);cout<<pr<<endl;}return 0;}
waitpid (waiting for interrupt or end of the child process) related function wait, fork header file #include<sys/types.h> #include<sys/wait.h>Define the function pid_t waitpid(pid_t pid, int * status, int options); the 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 waitpid() is called, waitpid() will immediately return the child process end state value. The end state value of the child process is returned by the parameter status, and the process ID of the child process is returned as soon as possible. If you do not care about the end state value, the parameter status can be set to NULL.
The parameter pid is the subprocess ID to be waited for. Other values have the following meaning: pid<-1 Any subprocess waiting for the process group ID to be the absolute value of pid. Pid=-1 Wait for any child process, equivalent to wait(). Pid=0 Any child process waiting for the process group ID to be the same as the current process. Pid>0 waits for any child process whose child process ID is pid.
The parameter option can be 0 or the following OR combination: WNOHANG If there are no child processes that have ended, they will return immediately and will not wait. WUNTRACED Returns immediately if the child process enters a suspended execution, but the end state is ignored.
The end state of the child process is returned to status after returning. There are several macros to determine the end condition: WIFEXITED(status) is non-zero if the child process ends normally. WEXITSTATUS(status) gets the end code returned by the child process exit(). Generally, it is used to determine whether it ends normally by using WIFEXITED. WIFSIGNALED (status) If the subprocess ends because of the signal, the macro value is true WTERMSIG(status). The signal code that the subprocess is aborted due to the signal is generally used by the WIFSIGNALED to judge the macro. WIFSTOPPED(status) This macro value is true if the child process is in a suspended execution. This is usually the case only when using WUNTRACED. WSTOPSIG(status) Get the signal code that causes the child process to pause. Generally, it will be judged by WIFSTOPPED before using this macro.
Return value Returns the child process ID (PID) if successful, or -1 if an error occurs. The reason for the failure is in errno.
Examples refer to wait().
We have been doing a task in the usual way until we know there is a better way to handle it. As a fo
Generally speaking, after installing ubuntu, the centralized display driver has been installed. Howe
The Linux operating system is an open source, free operating system that is not only secure, stable,
If you have used Linux for a while and feel that the hard disk is not enough, you
Redhat/Centos NIC binding basic tutorial
Want to be an embedded programmer should know 0x10 basic questions
Linux kernel root directory Makefile annotation
Self-made Linux Pinyin Wubi input method
Oracle, informix database environment variable settings and compile
Use Ghost to achieve backup of Linux system
Kobject, kset and its relationship of device model
Linux modify system time detailed solution
VIM block indentation and replacement
Windows XP SP3 desktop icon has a background color how to eliminate it
Win8 blue screen prompt error code 0x00000018 how to solve?
How to open the win10wifi hotspot
Win10 Build 9901 new discovery Add a variety of new icons
Win7 system can access the Internet but the icon shows that it is not connected. How to do
How to refuse to remotely modify the key value of the Win8 registry
How to set up the computer time switch machine?
How to install Win10 office activation? Win10 activates Office 2013 skills