簡體   English   中英

Linux子進程信號丟失

[英]Linux child process signal loss

我有兩個子進程和一個父進程。 兩個孩子同時發送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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM