C Signal usage and process using IPC message queues
I have a program that uses a signal (SIGUSR2) for setup a catch handler function to process high priority incoming messages.
The program receives incoming messages off an IPC message queue using msgrcv() in its main loop. When the sender of messages to the IPC message queue wants to notify the program that a high priority one is incoming, it sends SIGUSR2 to the process to have it stop processing any current message that may be being processed.
In the signal catch handler function I first upon entry do:
signal(SIGUSR2, SIG_IGN);
to ignore any new signals for preemption to occur.
then the code processes the preemption request where it stores the currently being processed message back into the queue, housekeeping, etc. and then just before returning from the signal handler function it does:
signal(SIGUSR2, sighandler_func);
Question: If another flash processing signal is received just a nanosecond after the above signal call is issued, will the process re-dispatch to the signal handler function again? ie: if the code in the main loop where it does the sighold(SIGUSR2) and sigrelse(SIGUSR2) to stop / start the receipt of the preemption signal take precedence or is it just the above signal call that re-energizes the signal handler?
You should use sigaction for that:
struct sigaction act;
memset(&act, 0, sizeof(act);
act.sa_handler = sighandler_func;
sigaction(SIGUSR2, &act, NULL);
This way, the signal handler is automatically called with the signal blocked, which caused the event (in your case SIGUSR2). If now a SIGUSR2
arrives during the execution of the handler, it is blocked until the signal handler returns. Then, (when the signal is unblocked), the signal handler is called immediately again.
However, you will have to handle the case, that multiple SIGUSR2
arrive during one execution of the signal handler properly, since it will be called only once after return of the handler then.
上一篇: sigwaitinfo()可以等待超级进程信号掩码吗?
下一篇: C使用IPC消息队列的信号使用和处理