c - 将 waitpid 与 WNOHANG 一起使用时的错误状态

标签 c linux unix posix

我想知道我的子进程何时退出。但我不想阻止我的应用程序,所以我使用 WNOHANG。

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

void handler(int nsig) {
  int status;
  waitpid(-1, &status, WNOHANG);
  printf("nsig=%i; status=%i\n", nsig, status);
  if (WIFEXITED(status)) {
    printf("Exited\n");
  }
}

int main() {  
  struct sigaction act;
  act.sa_handler = handler;
  if (sigaction(SIGCHLD, &act, 0) < 0) {
    fprintf(stderr, "sigaction failed\n");
    exit(-1);
  }

  pid_t fpid;
  fpid = fork();
  if (fpid == 0) {
    execlp("okular", "okular", 0);
  }
  while(1);
  return 0;
}

如果我像往常一样关闭“okular”,它会正常工作。

$ ./test
nsig=17; status=0
Exited

但是如果我做类似的事情

kill -STOP OKULAR_PID

我有相同的输出,这对我来说是错误的,因为 okular 实际上并没有退出。

最佳答案

我认为这是正确的,因为 SIGCHLD 被定义为 Child terminated or stopped 可以在这个 man page 中看到为信号。 SIGCLD 是 SIGCHLD 的同义词。

关于c - 将 waitpid 与 WNOHANG 一起使用时的错误状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4251221/

相关文章:

c - 信号处理函数不断循环

在 cython 中调用内置的 gcc?

linux - 将多个 PDF 文档转换为多个图像文件

linux - 如何找到指向特定文件夹的所有符号链接(symbolic link)?

c - 在 C 中使用 strcat 获取权限

c - 第二次 recv 调用后,recv 始终读取 0 字节

c - 如何检测堆栈溢出点

linux - 用于识别分隔符并将内容拆分为 excel 单独单元格的命令

c - "iovcnt"中 "writev"参数的允许最大值?

linux - 如何从命令提示符关闭 gvim