【发布时间】:2016-10-19 06:36:56
【问题描述】:
我有 2 个进程,比如说 A 和 B。进程 A 将从用户那里获取输入并进行一些处理。
进程 A 和 B 之间没有父/子关系。
如果进程 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
}
【问题讨论】: