WIFEXITED/WEXITSTATUS/WIFSIGNALED

 WIFEXITED/WEXITSTATUS/WIFSIGNALED 
If the exit status value (*note Program Termination::) of the child
process is zero, then the status value reported by `waitpid' or `wait'
is also zero. You can test for other kinds of information encoded in
the returned status value using the following macros. These macros are
defined in the header file `sys/wait.h'.
-- Macro: int WIFEXITED (int STATUS)
     This macro returns a nonzero value if the child process terminated
     normally with `exit' or `_exit'.
-- Macro: int WEXITSTATUS (int STATUS)
     If `WIFEXITED' is true of STATUS, this macro returns the low-order
     8 bits of the exit status value from the child process. *Note
     Exit Status::.
-- Macro: int WIFSIGNALED (int STATUS)
     This macro returns a nonzero value if the child process terminated
     because it received a signal that was not handled. *Note Signal
     Handling::.
子进程的结束状态返回后存于status,底下有几个宏可判别结束情况
WIFEXITED(status)如果子进程正常结束则为非0值。
WEXITSTATUS(status)取得子进程exit()返回的结束代码,一般会先用WIFEXITED 来判断是否正常结束才能使用此宏。
WIFSIGNALED(status)如果子进程是因为信号而结束则此宏值为真
WTERMSIG(status)取得子进程因信号而中止的信号代码,一般会先用WIFSIGNALED 来判断后才使用此宏。
WIFSTOPPED(status)如果子进程处于暂停执行情况则此宏值为真。一般只有使用WUNTRACED 时才会有此情况。
WSTOPSIG(status)取得引发子进程暂停的信号代码,
http://blog.chinaunix.net/uid-10554711-id-2948333.html

 

### C/C++ 中 `sys/wait.h` 头文件使用方法 #### 函数概述 `sys/wait.h` 提供了一系列用于等待子进程终止并获取其退出状态的函数。这些函数对于父进程管理子进程非常重要。 #### 主要函数介绍 - **wait(int *status)** 此函数会挂起调用它的进程,直到其中一个子进程结束为止。如果没有任何子进程存在,则立即返回 `-1` 并设置 errno 为ECHILD[^2]。 ```c #include <sys/types.h> #include <sys/wait.h> pid_t pid; int status; pid = wait(&status); if (pid == -1) { perror("wait"); } ``` - **waitpid(pid_t pid, int *status, int options)** 提供了更灵活的方式去等待特定 PID 的子进程,并允许通过选项参数控制行为。例如可以指定不阻塞(waitpid(..., WNOHANG)) 或者只关心已停止的子进程(WUNTRACED)[^3]。 ```c pid_t child_pid; child_pid = fork(); if(child_pid == 0){ // 子进程中执行的任务... }else{ int wstatus; while ((w = waitpid(-1,&wstatus,WNOHANG | WUNTRACED)) > 0 ) { printf("Child %d exited\n", w); } } ``` - **宏定义** 为了方便解析由上述两个函数返回的状态码,在 `<sys/wait.h>` 中还提供了一些有用的宏来提取信息: | 宏名 | 描述 | |--| | WIFEXITED(status) | 如果子进程正常终止则返回真 | | WEXITSTATUS(status)| 当WIFEXITED为真时,返回子进程传递给exit()的实际值 | | WTERMSIG(status) | 返回导致子程序异常终止信号编号 | | WCOREDUMP(status) | 若子进程因接收到致命信号而创建核心转储,则返回非零 | #### 示例代码展示如何捕获子进程退出状态 下面是一个简单的例子展示了怎样利用 `wait()` 和相关宏处理子进程的信息: ```c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> int main(void) { pid_t pid; int status; /* 创建新进程 */ switch (pid = fork()) { case -1: perror("fork error"); exit(1); case 0: /* code for the child process */ puts("This is Child Process."); sleep(2); /* simulate some work being done by this process */ _exit(7); /* terminate with non-zero status */ default: /* parent continues here */ puts("Waiting for child to complete..."); if (wait(&status) != pid) { fprintf(stderr,"wait error\n"); exit(1); } if (WIFEXITED(status)) printf("Normal termination, exit status=%d\n", WEXITSTATUS(status)); else if (WIFSIGNALED(status)) printf("Abnormal termination, signal number=%d%score dump\n", WTERMSIG(status), WCOREDUMP(status)? " (":" no "); break; } return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值