博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
linux wait函数解析
阅读量:4140 次
发布时间:2019-05-25

本文共 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/

你可能感兴趣的文章
ldd -r xxx.so命令的重要作用------见招拆招地解决缺库问题(undefined symbol)
查看>>
再来聊聊linux中的nm命令(nm与ldd命令实战)
查看>>
大白话解释互联网后台为什么常用异步server?
查看>>
确定性这剂毒药,你喝过没?
查看>>
GNU makefile英文官方介绍------干货
查看>>
makefile指定头文件和库出错的那点破事
查看>>
C/C++为什么要短路求值?
查看>>
做好参数校验, 不要过分信任前端传过来的值
查看>>
map如何按value来排序------用带pair的vector吧
查看>>
那点代码, 谁都会写!------以后我会在博客中多加入一些思路方面的东东, 少写一些实际的代码
查看>>
配置文件的重要性------轻化操作
查看>>
cp后文件时间会变, mv后文件时间不会变化------定位一个低概率core问题时, 差点误导了自己
查看>>
又是缓存惹的祸!!!
查看>>
为什么要实现程序指令和程序数据的分离?
查看>>
我对C++ string和length方法的一个长期误解------从protobuf序列化说起(没处理好会引起数据丢失、反序列化失败哦!)
查看>>
一起来看看protobuf中容易引起bug的一个细节
查看>>
无protobuf协议情况下的反序列化------貌似无解, 其实有解!
查看>>
make -n(仅列出命令, 但不会执行)用于调试makefile
查看>>
make -k(keep going)命令会在发现错误时继续执行(用于一次发现所有错误)
查看>>
makefile中“-“符号的使用
查看>>