我想知道,当我的孩子进程退出时。但我不想阻止我的申请,所以我使用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实际上没有退出。