C 如何从信号处理器内部向其他进程发送通知?

C 如何从信号处理器内部向其他进程发送通知?,c,signals,ipc,C,Signals,Ipc,我有两个进程,比如A和B。进程A将从用户那里获得输入并进行一些处理 进程A和进程B之间没有父/子关系 如果进程A被信号杀死,有没有办法从信号处理程序内部将消息发送给进程B 注:根据我的要求,一旦我完成处理,已经收到用户的输入,如果收到SIGHUP信号,则退出主回路,这很好 我脑子里有以下想法。这个设计有缺陷吗 过程A #包括 #包括 int信号;//要在信号处理程序内部设置的变量 sig_hup_handler_callback() { 信号=真; } int main() { char-str

我有两个进程,比如A和B。进程A将从用户那里获得输入并进行一些处理

进程A和进程B之间没有父/子关系

如果进程A被信号杀死,有没有办法从信号处理程序内部将消息发送给进程B

注:根据我的要求,一旦我完成处理,已经收到用户的输入,如果收到SIGHUP信号,则退出主回路,这很好

我脑子里有以下想法。这个设计有缺陷吗

过程A
#包括
#包括
int信号;//要在信号处理程序内部设置的变量
sig_hup_handler_callback()
{
信号=真;
}
int main()
{
char-str[10];
信号(SIGHUP、sig_hup_处理器_回调);
//将从用户处获取输入的循环。
而(1)
{
如果(信号==TRUE){//收到信号
将_消息_发送到_B();
返回0;
}
scanf(“%s”,str);
do_process(str);//对输入进行一些处理
}
返回0;
}
/*函数将通知发送到进程B*/
无效发送消息到
{
//使用msg que发送消息
}

只要想想进程A是否正在执行
do\u进程(str)和崩溃发生,然后在回拨标志将被更新,但您的while循环将永远不会在下次调用,所以您的
发送_消息_到_B()将不会被调用。所以最好只将该函数放在回调函数中

如下所示

#include <stdio.h>
#include <signal.h>

int signal;// variable to set inside signal handler

sig_hup_handler_callback()
{
     send_message_to_B();
}


int main()
{
  char str[10];
  signal(SIGHUP,sig_hup_handler_callback);
  //Loops which will get the input from the user.
   while(1)
  {

    scanf("%s",str);
    do_process(str); //do some processing with the input
  }

  return 0;
}

/*function to send the notification to process B*/
void send_message_to_B()
{
     //send the message using msg que
}
#包括
#包括
int信号;//要在信号处理程序内部设置的变量
sig_hup_handler_callback()
{
将_消息_发送到_B();
}
int main()
{
char-str[10];
信号(SIGHUP、sig_hup_处理器_回调);
//将从用户处获取输入的循环。
而(1)
{
scanf(“%s”,str);
do_process(str);//对输入进行一些处理
}
返回0;
}
/*函数将通知发送到进程B*/
无效发送消息到
{
//使用msg que发送消息
}

正如Jeegar在另一个答案中提到的,致命信号将中断进程主执行并调用信号处理程序。控制不会回到中断的地方。因此,在处理致命信号后,现在显示的代码将永远不会调用
发送消息到\u B

请注意从信号处理程序调用哪些函数。从信号处理程序调用某些函数被认为是不安全的-

#include <stdio.h>
#include <signal.h>

int signal;// variable to set inside signal handler

sig_hup_handler_callback()
{
     send_message_to_B();
}


int main()
{
  char str[10];
  signal(SIGHUP,sig_hup_handler_callback);
  //Loops which will get the input from the user.
   while(1)
  {

    scanf("%s",str);
    do_process(str); //do some processing with the input
  }

  return 0;
}

/*function to send the notification to process B*/
void send_message_to_B()
{
     //send the message using msg que
}