gpt4 book ai didi

c - parent 和 child 的不同信号处理程序

转载 作者:太空狗 更新时间:2023-10-29 17:09:48 26 4
gpt4 key购买 nike

我有一个带有信号处理程序的程序:

signal(SIGINT, signalhandler);

然后程序 fork ,子进程需要一个不同的信号处理程序,所以:

pid = fork();

/* What happens here? */

if(pid==0)
{
signal(SIGINT, signalhandler_for_child);
}

那么,如果在 fork 之后但在分配新的签名处理程序之前立即调用 SIGINT 会发生什么?

在 child 获得新的信号处理程序之前,这会发生还是不可能被打断。

如果可以的话。我怎样才能将信号排队给 child ,以便它有时间获得新的处理程序?

我知道概率(如果存在的话)一定几乎为 0,但我想确保应用程序在这方面是健壮的。

最佳答案

So what happens if a SIGINT is called right after the fork but before the new sign handler is assigned?

将调用安装在父级中的信号处理程序。子进程继承它。

Can this happen or there is no possibility to be interrupted before the child gets the new signal handler.

当然有可能发生。

If it is possible. How could I queue the signal to the child so it gets time to get the new handler?

为了确保,您需要在 调用 fork() 之前阻止 SIGINT,然后为 SIGINT 重新安装一个不同的在子进程中,然后解锁 SGINT。

/* block SIGINT here. */

pid = fork();

if (pid == 0) {
/* Install a new SIGINT handler here. */
/* Unblock SIGINT. */
...
} else if (pid > 0) {
/* The SIGINT handler is already in place. So just unblock SIGINT. */
...
} else {
/* error */
}

sigprocmask()pthread_sigmask()用于阻塞和解除阻塞信号。

您还可以找到 GNU documentation on signal blocking有用。

关于c - parent 和 child 的不同信号处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49404021/

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