简体   繁体   English

父母和孩子的不同的信号处理程序

[英]Different signal handlers for parent and child

I have a program with a signal handler: 我有一个带有信号处理程序的程序:

signal(SIGINT, signalhandler);

Then the program forks and the child needs a different signal handler so: 然后程序分叉和子代需要一个不同的信号处理程序,所以:

pid = fork();

/* What happens here? */

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

So what happens if a SIGINT is called right after the fork but before the new sign handler is assigned? 那么如果在fork之后但在分配新的签名处理程序之前调用SIGINT会发生什么?

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? 我如何将信号排队给孩子,以便有时间获得新的处理程序?

I know that the probabilities, if they exist, must be almost 0, but I want to make sure the application is robust in this aspect. 我知道概率(如果存在的话)必须几乎为0,但我想确保应用程序在这方面是健壮的。

So what happens if a SIGINT is called right after the fork but before the new sign handler is assigned? 那么如果在fork之后但在分配新的签名处理程序之前调用SIGINT会发生什么?

The signal handler installed in the parent will be called. 将调用父项中安装的信号处理程序。 Child process inherits it. 子进程继承它。

Can this happen or there is no possibility to be interrupted before the child gets the new signal handler. 这可能发生,或者在孩子获得新的信号处理程序之前不可能被中断。

Cetainly can happen. 可能会发生。

If it is possible. 如果可能的话。 How could I queue the signal to the child so it gets time to get the new handler? 我如何将信号排队给孩子,以便有时间获得新的处理程序?

To ensure, you need to block SIGINT before calling fork() and then reinstall a different for SIGINT in the child process and then unblock SGINT. 要确保,您需要调用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 */
}

Look at sigprocmask() and pthread_sigmask() for blocking and unblocking signals. 查看sigprocmask()pthread_sigmask()以阻止和解除阻塞信号。

You may also find the GNU documentation on signal blocking useful. 您还可以找到有关信号阻塞GNU文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM