确定终止的进程的PID [英] Determine pid of terminated process
问题描述
我试图找出PID是发送SIGCHLD信号的过程是什么,我想这样做在我的SIGCHLD建立了一个信号处理程序。我会怎么做呢?我想:
I'm trying to figure out what the pid is of a process that sent the SIGCHLD signal, and I want to do this in a signal handler I created for SIGCHLD. How would I do this? I'm trying:
int pid = waitpid(-1, NULL, WNOHANG);
因为我想等待被产生的子进程。
because I want to wait for any child process that is spawned.
推荐答案
如果您使用 waitpid函数()
或多或少如图所示,你会被告知之一PID已经死去的子进程 - 通常是将是已死亡的唯一方法,但是如果你让他们乱舞,你可能会得到一个信号,许多尸体收集。因此,使用:
If you use waitpid()
more or less as shown, you will be told the PID of one of the child processes that has died — usually that will be the only process that has died, but if you get a flurry of them, you might get one signal and many corpses to collect. So, use:
void sigchld_handler(int signum)
{
pid_t pid;
int status;
while ((pid = waitpid(-1, &status, WNOHANG)) != -1)
{
unregister_child(pid, status); // Or whatever you need to do with the PID
}
}
您可以替换&放大器;状态
与 NULL
,如果你不关心孩子的退出状态
You can replace &status
with NULL
if you don't care about the exit status of the child.
这篇关于确定终止的进程的PID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!