我有一个主进程和一些子进程从中衍生。在一个时间点,我必须给SIGINT信号所有的子进程,但不给主进程。我无法为所有子进程存储pid。所以我使用SIG_IGN来忽略主进程中的SIGINT,并在我的操作后设置为默认值。但这并没有奏效。
请查看下面的代码片段:
/* Find group id for process */
nPgid = getpgid(parentPID);
/* Ignore SIGINT signal in parent process */
if (signal(SIGINT, SIG_IGN) == SIG_ERR)
{
cout << "Error in ignoring signal n");
}
/* Send SIGINT signal to all process in the group */
nReturnValue = kill ( (-1 * nPgid), SIGINT);
if (nReturnValue == RETURN_SUCCESS)
{
cout << "Sent SIGINT signal to all process in group successfully n";
}
else
{
cout << "Alert!!! Unable to send SIGINT signal to all process in the group n";
}
/* Set SIGINT signal status to default */
signal (SIGINT, SIG_DFL);
sleep(2);
我没有得到任何错误。但是父母要被杀了。我做错什么了吗?
nPgid = getpgid(parentPID);
parentPID
是什么?通过0
或getpid()
的结果得到主叫进程的组。
From man getpgid()
:
getpgid()返回pid指定的进程的PGID。如果pid为0,则使用调用进程的进程ID。(检索很少需要调用者以外的进程的PGID,并且POSIX.1 getpgrp()优先用于该任务。
从上面这篇文章我得出结论要做
nPgid = getpgid(o);