gpt4 book ai didi

c - Linux子进程信号丢失

转载 作者:太空宇宙 更新时间:2023-11-04 04:46:32 24 4
gpt4 key购买 nike

我有两个子进程和一个父进程。两个 child 同时发送一个SIGUSR1信号。处理程序只处理其中一个,父级也只接收其中一个。我认为它可以通过使用实时信号来解决,但我现在不知道该怎么做。感谢您的帮助。

void handler(int signalnumber)
{
//do stuff
}
int main()
{
sigset_t blockset;
sigfillset(&blockset);

struct sigaction action;
action.sa_handler = handler;
sigemptyset(&action.sa_mask);
sigaction(SIGUSR1, &action, NULL);

pid_t pid = getpid();
pid_t pids[2];

for(i = 0; i < 2; ++i)
{
if(getpid() == pid)
pids[i] = fork();
}

if(getpid() != pid)
{
while(1)
{
kill(getppid(), SIGUSR1);
}
} else
{
while(1)
{
sigdelset(&blockset, SIGUSR1);
sigsuspend(&blockset);
//do stuff
}
}
}

编辑:我用 SIGRTMIN+1 替换了 SIGUSR1。现在处理程序接收到这两个信号,但父级没有。 (我认为,因为它不等待任何。)

最佳答案

来自 man sigaction

sa_mask specifies a mask of signals  which  should  be  blocked  (i.e.,
added to the signal mask of the thread in which the signal handler is
invoked) during execution of the signal handler. In addition, the sig‐
nal which triggered the handler will be blocked, unless the SA_NODEFER
flag is used.`

所以,使用 SA_NODEFER。

关于c - Linux子进程信号丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20305082/

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