当前位置: 首页 > news >正文

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.

http://www.xdnf.cn/news/759133.html

相关文章:

  • Linux 内核中 skb_orphan 的深度解析:从版本差异到核心机制
  • JOIN使用的注意事项
  • HTTP协议解析
  • 话题通信之python实现
  • 【免杀】C2免杀技术(十三)Inline Hook 概念篇
  • C# winform 教程(一)
  • Hartree-Fock 自洽场计算流程
  • Oracle正则表达式学习
  • 正则表达式笔记
  • yolo目标检测助手:具有模型预测、图像标注功能
  • 【复杂网络分析】什么是modularity?
  • maven中的maven-antrun-plugin插件详解
  • Go语言中的rune和byte类型详解
  • MySQL(49)如何使用DEFAULT指定默认值?
  • 配置Ollama环境变量,实现远程访问
  • 怎么样提高研发质量?
  • 七.MySQL内置函数
  • Practice 2025.6.1—— 二叉树进阶面试题(2)
  • 研读论文《Attention Is All You Need》(13)
  • 【笔记】MSYS2 安装 Python 构建依赖记录Cython + Ninja + Meson + meson-python
  • 七、物理.
  • Flickr30k_Entities数据集
  • 【项目记录】登录认证(下)
  • 6.运算放大器—电源抑制比(五)
  • 2002-2022年 城市市政公用设施水平、环境、绿地等数据-社科经管实证数据
  • 殷咏梅教授:OptiTROP-Breast05亮相2025 ASCO,中国原创TROP2 ADC为mTNBC一线治疗带来新希望
  • 2024年数维杯国际大学生数学建模挑战赛B题空间变量协同估计方法研究解题全过程论文及程序
  • ZIP Cracker版本更新了
  • java中IO流分为几种
  • 深入Java NIO:构建高性能网络应用