与WHOHANG一起使用waitpid时的状态错误

时间:2010-11-22 23:18:31

标签: 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实际上没有退出。

1 个答案:

答案 0 :(得分:1)

我认为这是正确的,因为SIGCHLD被定义为Child终止或停止,因为在此man page信号中可以看到。 SIGCLD是SIGCHLD的同义词。

相关问题