本文共 3954 字,大约阅读时间需要 13 分钟。
进程一旦调用了wait,就立即阻塞自己,由wait自动分析是否当前进程的某个子进程已经退出,如果让它找到了这样一个已经变成僵尸的子进程,wait 就会收集这个子进程的信息, 并把它彻底销毁后返回;如果没有找到这样一个子进程,wait就会一直阻塞在这里,直到有一个出现为止。
wait(等待子进程中断或结束)
相关函数 waitpid,fork 表头文件 #include<sys/types.h> #include<sys/wait.h> 定义函数 pid_t wait (int * status); 函数说明 wait()会暂时停止目前进程的执行,直到有信号来到或子进程结束。如果在调用wait()时子进程已经结束,则wait()会立即返回子进程结束状态值。子进程的结束状态值会由参数status 返回,而子进程的进程识别码也会一快返回。如果不在意结束状态值,则参数status 可以设成NULL。子进程的结束状态值请参考waitpid()。 返回值 如果执行成功则返回子进程识别码(PID),如果有错误发生则返回-1。失败原因存于errno 中。 附加说明 范例 一 #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\n”,getpid()); exit(5); }else{ sleep(1); printf(“This is the parent process ,wait for child...\n”; pid=wait(&status); i=WEXITSTATUS(status); printf(“child’s pid =%d .exit status=%d\n”,pid,i); } } 执行 This is the child process.pid=1501 This is the parent process .wait for child... child’s pid =1501,exit status =5范例 二
#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) { //如果是子进程 睡眠20秒 cout<<"children : "<<getpid()<<endl; sleep(20); } else { cout<<"hello! i'm parent process!"<<endl; //如果是父进程在这里等待 pid_t pr = wait(NULL); cout<<pr<<endl; } return 0; } waitpid(等待子进程中断或结束) 相关函数 wait,fork 表头文件 #include<sys/types.h> #include<sys/wait.h> 定义函数 pid_t waitpid(pid_t pid,int * status,int options); 函数说明 waitpid()会暂时停止目前进程的执行,直到有信号来到或子进程结束。如果在调用waitpid()时子进程已经结束,则waitpid()会立即返回子进程结束状态值。子进程的结束状态值会由参数status 返回,而子进程的进程识别码也会一快返回。如果不在意结束状态值,则参数status 可以设成NULL。参数pid 为欲等待的子进程识别码,其他数值意义如下: pid<-1 等待进程组识别码为pid 绝对值的任何子进程。 pid=-1 等待任何子进程,相当于wait()。 pid=0 等待进程组识别码与目前进程相同的任何子进程。 pid>0 等待任何子进程识别码为pid 的子进程。 参数option 可以为0 或下面的OR 组合: WNOHANG 如果没有任何已经结束的子进程则马上返回,不予以等待。 WUNTRACED 如果子进程进入暂停执行情况则马上返回,但结束状态不予以理会。子进程的结束状态返回后存于status,底下有几个宏可判别结束情 况: WIFEXITED(status)如果子进程正常结束则为非0 值。 WEXITSTATUS(status)取得子进程exit()返回的结束代码,一般会先用WIFEXITED 来判断是否正常结束才能使用此宏。 WIFSIGNALED(status)如果子进程是因为信号而结束则此宏值为真 WTERMSIG(status) 取得子进程因信号而中止的信号代码,一般会先用WIFSIGNALED 来判断后才使用此宏。 WIFSTOPPED(status) 如果子进程处于暂停执行情况则此宏值为真。一般只有使用WUNTRACED 时才会有此情况。 WSTOPSIG(status) 取得引发子进程暂停的信号代码,一般会先用WIFSTOPPED 来判断后才使用此宏。 返回值 如果执行成功则返回子进程识别码(PID),如果有错误发生则返回-1。失败原因存于errno 中。 范例 参考wait()。
a.WIFEXITED(status) r eturns true if the child terminated normally, that is, by calling exit(3) or _exit(2), or by returning from main() .
Tiger-John翻译:
WIFEXITED(status) 若子进程是正常结束时则返回一个非零值。即调用exit(3),_exit(3) 或从main()函数返回的值。
b. WEXITSTATUS(status) returns the exit status of the child. This consists of the least significant 8 bits of the status argument that the child specified in a call to exit(3) or _exit(2) or as the argument for a return statement in main(). This macro should only be employed if WIFEXITED returned true.
Tiger-John翻译:
WEXITSTATUS(status) 如果宏WIFEXIED返回值为非零值时,它返回子进程中exit或_exit参数中的低8位。
c.W IFSIGNALED(status) returns true if the child process was terminated by a signal.
Tiger-John翻译:
WIFSIGNALED(status) 若子进程异常终止则返回一个非零值。
d. WTERMSIG(status) returns the number of the signal that caused the child process to terminate. This macro should only be employed if WIFSIGNALED returned true.
Tiger-John翻译:
WTERMSIG(status) 如果宏WIFSIGNALED的返回值非零,则返回使子进程异常终止的信号编号。
e.WIFSTOPPED(status) returns true if the child process was stopped by delivery of a signal; this is only possible if the call was done using WUN‐TRACED or when the child is being traced (see ptrace(2)).
Tiger-John翻译:
WIFSTOPPED(status) 若子进程由于异常暫停,则返回一个非零值。当调用WUN‐TRACED或子进程被跟踪时这才时可能的。
f. WSTOPSIG(status) returns the number of the signal which caused the child to stop.This macro should only be employed if WIFSTOPPED returned true.
Tiger-John翻译:
WSTOPSIG(status) 如果宏WIFSTOPPED返回值非零,则返回使子进程暫停的信号编号。
g.WIFCONTINUED(status) (since Linux 2.6.10) returns true if the child process was resumed by delivery of SIGCONT.
Tiger-John翻译:
WIFCONTINUED(status) (从2.6版本后)如果孩子进程通过SIGCONT恢复则返回一个非零值
转载地址:http://hbhvi.baihongyu.com/