...
Code Block |
---|
|
void handler(int signum) {
signal(signum, handler);
/* handle signal */
}
/* ... */
signal(signumSIGUSR1, handler);
|
Unfortunately this solution contains a race window, starting when the host environment resets the signal and ending when the handler calls signal()
. During that time, a second signal sent to the program will trigger the default signal behavior, thereby defeating the persistent behavior implied by the call to signal()
from within the handler to reassert the binding.
...
Code Block |
---|
|
void handler(int signum) {
/* handle signal */
}
/* ... */
signal(signumSIGUSR1, handler);
|
Compliant Solution (POSIX)
...
Code Block |
---|
|
void handler(int signum) {
/* handle signal */
}
/* ... */
/* Equivalent to signal( signumSIGUSR1, handler);
but make signal persistent */
struct sigaction act;
act.sa_handler = handler;
act.sa_flags = 0;
if (sigemptyset( &act.sa_mask) != 0) {
/* handle error */
}
if (sigaction(signumSIGUSR1, &act, NULL) != 0) {
/* handle error */
}
|
...