So I'm trying to create a signal handler using pthreads which works on both OS X and Linux. The code below works on OS X but doesn't work on Fedora 13.
The application is fairly simple. It spawns a pthread, registers SIGHUP and waits for a signal. After spawning the signal handler I block SIGHUP in the main thread开发者_运维知识库 so the signal should only be sent to the signal_handler thread.
On OS X this works fine, if I compile, run and send SIGHUP to the process it prints "Got SIGHUP". On Linux it just kills the process (and prints Hangup). If I comment out the signal_handler pthread_create the application doesn't die.
I know the application gets to the sigwait and blocks but instead of return the signal code it just kills the application.
I ran the test using the following commands:
g++ test.cc -lpthread -o test
./test &
PID="$!"
sleep 1
kill -1 "$PID"
test.cc
#include <pthread.h>
#include <signal.h>
#include <iostream>
using namespace std;
void *signal_handler(void *arg) {
int sig;
sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGHUP);
while (true) {
cout << "Wait for signal" << endl;
sigwait(&set, &sig);
if (sig == SIGHUP) {
cout << "Got SIGHUP" << endl;
}
}
}
int main() {
pthread_t handler;
sigset_t set;
// Create signal handler
pthread_create(&handler, NULL, signal_handler, NULL);
// Ignore SIGHUP in main thread
sigfillset(&set);
sigaddset(&set, SIGHUP);
pthread_sigmask(SIG_BLOCK, &set, NULL);
for (int i = 1; i < 5; i++) {
cout << "Sleeping..." << endl;
sleep(1);
}
pthread_join(handler, NULL);
return 0;
}
The POSIX spec for sigwait()
says:
The signals defined by set shall have been blocked at the time of the call to
sigwait()
; otherwise, the behavior is undefined.
You're not doing this. If you add:
pthread_sigmask(SIG_BLOCK, &set, NULL);
to your signal_handler()
function immediately after the sigaddset()
, then it works correctly.
精彩评论