无法使用 waitid() 和 P_PID 进行编译

标签 c waitpid

我是 Linux 新手。我正在尝试使用 waitid() 来等待子进程。 当我尝试使用 gcc 编译包含以下行的文件时:

id_t cpid = fork();
siginfo_t status;
waitid(P_PID, cpid, &status, WEXITED);

生成了以下错误:

error: ‘P_PID’ undeclared (first use in this function)

我包含了以下库:

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <signal.h>
#include <string.h> 
#include <time.h> 

我错过了什么吗?

另一个问题是如何使用 WIFSIGNALED()siginfo_t 类型中检索信息?

最佳答案

您需要包含<sys/wait.h>并定义_XOPEN_SOURCE , as documented in the manual .

WIFSIGNALED宏必须与从 wait 获得的整数状态一起使用, waitpidwaitid 。以waitpid为例,状态为 si_status siginfo_t的成员结构。换句话说,您可以使用 WIFSIGNALED(info.si_status) , infosiginfo_t 类型的结构您之前传递给其地址 waitid() .

关于无法使用 waitid() 和 P_PID 进行编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26380736/

相关文章:

c - 从c中的子进程获取退出状态

c - 在结构中的函数中传递枚举(在 C 中)

检查所有 malloc 是否受到 NULL 返回保护

子进程在运行 fork() 时接管父进程

linux - 一步杀死并等待

c - 从子进程运行命令

c - 关于使用 fork() 创建的子进程的退出状态值的查询

c - Gcovr --object-directory 选项无法按预期使用与 .o 文件不同的目录中的 .gcda 文件

c++ - 空指针的地址?

c - 如何告诉 GCC 忽略某些源文件中的警告?