如何从信号处理程序内部向其他进程发送通知?
How to send notification to other process from inside Signal handler?
我有 2 个进程 A 和 B。进程 A 将从用户那里获取输入并进行一些处理。
进程A和B之间没有parent/child关系。
如果进程 A 被信号杀死,有什么方法可以从信号处理程序内部将消息发送到进程 B?
注意:根据我的要求,一旦我完成处理已经收到来自用户的输入并在收到 SIGHUP 信号时退出主循环。
我脑子里有以下想法。这个设计有什么缺陷吗?
进程 A
#include <stdio.h>
#include <signal.h>
int signal;// variable to set inside signal handler
sig_hup_handler_callback()
{
signal = TRUE;
}
int main()
{
char str[10];
signal(SIGHUP,sig_hup_handler_callback);
//Loops which will get the input from the user.
while(1)
{
if(signal == TRUE) { //received a signal
send_message_to_B();
return 0;
}
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
}
试想,如果进程 A 正在执行 do_process(str);
并且发生崩溃,那么在回调中 Flag 将被更新,但您的 while 循环将永远不会在下次调用,因此您的 send_message_to_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
}
正如 Jeegar 在另一个答案中提到的,致命信号将中断进程主执行并调用信号处理程序。并且控制不会回到被中断的地方。因此,您现在显示的代码在处理致命信号后将永远不会调用 send_message_to_B
。
注意从信号处理程序调用的函数。从信号处理程序调用某些函数被认为是不安全的 - Refer section - Async-signal-safe functions
我有 2 个进程 A 和 B。进程 A 将从用户那里获取输入并进行一些处理。
进程A和B之间没有parent/child关系。
如果进程 A 被信号杀死,有什么方法可以从信号处理程序内部将消息发送到进程 B?
注意:根据我的要求,一旦我完成处理已经收到来自用户的输入并在收到 SIGHUP 信号时退出主循环。
我脑子里有以下想法。这个设计有什么缺陷吗?
进程 A
#include <stdio.h>
#include <signal.h>
int signal;// variable to set inside signal handler
sig_hup_handler_callback()
{
signal = TRUE;
}
int main()
{
char str[10];
signal(SIGHUP,sig_hup_handler_callback);
//Loops which will get the input from the user.
while(1)
{
if(signal == TRUE) { //received a signal
send_message_to_B();
return 0;
}
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
}
试想,如果进程 A 正在执行 do_process(str);
并且发生崩溃,那么在回调中 Flag 将被更新,但您的 while 循环将永远不会在下次调用,因此您的 send_message_to_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
}
正如 Jeegar 在另一个答案中提到的,致命信号将中断进程主执行并调用信号处理程序。并且控制不会回到被中断的地方。因此,您现在显示的代码在处理致命信号后将永远不会调用 send_message_to_B
。
注意从信号处理程序调用的函数。从信号处理程序调用某些函数被认为是不安全的 - Refer section - Async-signal-safe functions