简体   繁体   English

Linux子进程信号丢失

[英]Linux child process signal loss

I have two child processes and one parent process. 我有两个子进程和一个父进程。 The two child send a SIGUSR1 signal at the same time. 两个孩子同时发送SIGUSR1信号。 The handler handles only one of them, and the parent receives only one of them too. 处理程序仅处理其中之一,而父级也仅接收其中之一。 I think it can be solved by using real time signal, but i don't now how to do it. 我认为可以通过使用实时信号来解决,但现在我不知道如何做到。 Thank you for your help. 谢谢您的帮助。

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
      }
   }
}

Edit: I replaced SIGUSR1 with SIGRTMIN+1. 编辑:我用SIGRTMIN + 1替换了SIGUSR1。 Now the handler receives both signals, but the parent does not. 现在,处理程序会接收到两个信号,但父级不会。 (I think, because it's not waiting for any.) (我认为,因为它没有等待任何东西。)

From man sigaction 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.`

So, use SA_NODEFER. 因此,请使用SA_NODEFER。

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

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