waitpid的waitstatus 含义源码解读

当我们在调用pid_t waitpid(pid_t pid , int *stat_loc , int options )时,其中第二个参数stat_loc会提供子进程退出的详细信息,为此posix还提供了一组宏来解析这个status.

在\glibc\bits\waitstatus.h

/* If WIFEXITED(STATUS), the low-order 8 bits of the status. */

#define __WEXITSTATUS(status) (((status) & 0xff00) >> 8)

/* If WIFSIGNALED(STATUS), the terminating signal. */

#define __WTERMSIG(status) ((status) & 0x7f)

/* If WIFSTOPPED(STATUS), the signal that stopped the child. */

#define __WSTOPSIG(status) __WEXITSTATUS(status)

/* Nonzero if STATUS indicates normal termination. */

#define __WIFEXITED(status) (__WTERMSIG(status) == 0)

/* Nonzero if STATUS indicates termination by a signal. */

#define __WIFSIGNALED(status) \

(((signed char) (((status) & 0x7f) + 1) >> 1) > 0)

/* Nonzero if STATUS indicates the child is stopped. */

#define __WIFSTOPPED(status) (((status) & 0xff) == 0x7f)

/* Nonzero if STATUS indicates the child continued after a stop. We only

define this if <bits/waitflags.h> provides the WCONTINUED flag bit. */

#ifdef WCONTINUED

define __WIFCONTINUED(status) ((status) == __W_CONTINUED)

#endif

/* Nonzero if STATUS indicates the child dumped core. */

#define __WCOREDUMP(status) ((status) & __WCOREFLAG)

/* Macros for constructing status values. */

#define __W_EXITCODE(ret, sig) ((ret) << 8 | (sig))

#define __W_STOPCODE(sig) ((sig) << 8 | 0x7f)

#define __W_CONTINUED 0xffff

#define __WCOREFLAG 0x80

我们重点看一下#define __W_EXITCODE(ret, sig) ((ret) << 8 | (sig)),其中:

ret是exit code,就是子进程在退出时,如果有调用exit()时,产生的exitcode. 比如子进程显示调用了exit(1),那么exitcode就是1。

sig是signal,比如SIGTERM,这时可以认为子进程是被动kill掉的,所以需要记录此时的SIGNAL。

示例:

pclose return 30720 = 0x7800, 0x78 = 120,

sys --- System-specific parameters and functions --- Python 3.13.3 documentation

Changed in version 3.6: If an error occurs in the cleanup after the Python interpreter has caught SystemExit (such as an error flushing buffered data in the standard streams), the exit status is changed to 120.

相关推荐
NEXU53 小时前
Linux:套接字
linux·服务器·网络
morliz子轩4 小时前
基于WSL搭建Ubuntu 22.04.x LTS开发环境
linux·运维·ubuntu
Janspran5 小时前
嵌入式linux学习 -- 进程和线程
linux·运维·学习
FreeBuf_5 小时前
CERT/CC警告:新型HTTP/2漏洞“MadeYouReset“恐致全球服务器遭DDoS攻击瘫痪
服务器·http·ddos
Cosmoshhhyyy5 小时前
linux远程部署dify和mac本地部署dify
linux·运维·macos
麦兜*7 小时前
【swift】SwiftUI动画卡顿全解:GeometryReader滥用检测与Canvas绘制替代方案
服务器·ios·swiftui·android studio·objective-c·ai编程·swift
路多辛7 小时前
Debian新一代的APT软件源配置文件格式DEB822详解
linux·运维·ubuntu·debian
-VE-7 小时前
Linux线程控制
linux
驱动探索者7 小时前
USB ADB 简介
linux·adb·驱动·usb
dessler9 小时前
Hadoop HDFS-部署和基本操作
linux·运维·hdfs