I have 2 process lets say A and B. process A will get the input from user and do some processing.
我有2个进程让我们说A和B.进程A将获得用户的输入并进行一些处理。
There is no parent/child relation between process A and B.
过程A和B之间没有父/子关系。
If process A get killed by a signal, Is there any way i can send the message to process B from inside signal handler ?
如果进程A被信号杀死,有没有办法可以从内部信号处理程序向进程B发送消息?
Note: For my requirement it if fine that once i done with processing already received input from the user and exit from main loop if SIGHUP signal is received.
注意:对于我的要求,如果处理完毕,一旦处理完已经接收到用户输入并且如果收到SIGHUP信号则退出主循环。
I am having following idea in my mind. Is there any flaw in this design ?
我心中有这样的想法。这个设计有什么缺陷吗?
process 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
}
2 个解决方案
#1
1
Just think if process A is executing do_process(str);
and crash happen then in call back Flag will be updated but you while loop will never called for next time so your send_message_to_B();
will not be called. So better to put that function in callback only..
试想一下,如果进程A正在执行do_process(str);然后在回调中发生崩溃标志将被更新,但是你的while循环将永远不会被调用,所以你的send_message_to_B();不会被叫。所以最好只将该函数放入回调中..
Just as below.
如下所示。
#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
}
#2
1
As mentioned by Jeegar in the other answer, fatal signals will interrupt the process main execution and have the signal handler called. And the control won't be back to where it was interrupted. Hence your code as it is shown now will never call send_message_to_B
after handling fatal signal.
正如Jeegar在另一个答案中所提到的,致命信号将中断进程主执行并调用信号处理程序。控件不会回到被中断的地方。因此,现在显示的代码在处理致命信号后将永远不会调用send_message_to_B。
Be careful on what functions you invoke from the signal handlers. Some functions are considered not safe to be called from signal-handlers - Refer section - Async-signal-safe functions
注意从信号处理程序调用的函数。从信号处理程序调用某些功能被认为是不安全的 - 参见章节 - 异步信号安全功能
#1
1
Just think if process A is executing do_process(str);
and crash happen then in call back Flag will be updated but you while loop will never called for next time so your send_message_to_B();
will not be called. So better to put that function in callback only..
试想一下,如果进程A正在执行do_process(str);然后在回调中发生崩溃标志将被更新,但是你的while循环将永远不会被调用,所以你的send_message_to_B();不会被叫。所以最好只将该函数放入回调中..
Just as below.
如下所示。
#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
}
#2
1
As mentioned by Jeegar in the other answer, fatal signals will interrupt the process main execution and have the signal handler called. And the control won't be back to where it was interrupted. Hence your code as it is shown now will never call send_message_to_B
after handling fatal signal.
正如Jeegar在另一个答案中所提到的,致命信号将中断进程主执行并调用信号处理程序。控件不会回到被中断的地方。因此,现在显示的代码在处理致命信号后将永远不会调用send_message_to_B。
Be careful on what functions you invoke from the signal handlers. Some functions are considered not safe to be called from signal-handlers - Refer section - Async-signal-safe functions
注意从信号处理程序调用的函数。从信号处理程序调用某些功能被认为是不安全的 - 参见章节 - 异步信号安全功能