gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 11:40:05 25 4
gpt4 key购买 nike

我想知道我的子进程何时退出。但我不想阻止我的应用程序,所以我使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com