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.

相关推荐
老一岁6 小时前
csdn-mysql索引与redis命令
linux
handsome_sai8 小时前
《Linux 小白查看系统与软件商店指南》
linux·运维·服务器
Rabitebla8 小时前
【Linux】 文件系统(五):挂载与软硬链接,多个文件系统怎样组成统一目录树
linux·运维·服务器·c++·笔记·学习·算法
数字新视界8 小时前
动环智能监控历史数据留存与故障回溯方法
运维·服务器·网络·机房·模块化机房
皓月盈江9 小时前
云服务器上部署Hexo静态博客
linux·服务器·hexo·个人博客·云服务器·个人网站·静态博客
qeen8710 小时前
【Linux】线程的基本认识及与进程的关系、对比
linux·运维
Smoothcloud润云11 小时前
GPU服务器租用训练缓存小文件与inode耗尽排查实战
运维·服务器·人工智能·缓存·ai·gpu算力
疯华整猫11 小时前
一条 kill -9 杀不掉的进程:AI 时代,程序员为什么必须精通 Linux
linux
禾小西13 小时前
Navicat 通过 SSH 跳板连接 PostgreSQL:双服务器配置与排障实录
服务器·postgresql·ssh
爆米花byh14 小时前
在RockyLinux9环境下安装Rust1.100.0环境
linux·rust