parent 和 child 的不同信号处理程序
Different signal handlers for parent and child
我有一个带有信号处理程序的程序:
signal(SIGINT, signalhandler);
然后程序 fork 并且 child 需要一个不同的信号处理程序,所以:
pid = fork();
/* What happens here? */
if(pid==0)
{
signal(SIGINT, signalhandler_for_child);
}
那么如果在分叉之后但在分配新的签名处理程序之前立即调用 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()
and pthread_sigmask()
以获取阻塞和解除阻塞信号。
您可能还会发现 GNU documentation on signal blocking 有用。
我有一个带有信号处理程序的程序:
signal(SIGINT, signalhandler);
然后程序 fork 并且 child 需要一个不同的信号处理程序,所以:
pid = fork();
/* What happens here? */
if(pid==0)
{
signal(SIGINT, signalhandler_for_child);
}
那么如果在分叉之后但在分配新的签名处理程序之前立即调用 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()
and pthread_sigmask()
以获取阻塞和解除阻塞信号。
您可能还会发现 GNU documentation on signal blocking 有用。