sigwait in Linux (Fedora 13) vs OS X
Posted
by Silas
on Stack Overflow
See other posts from Stack Overflow
or by Silas
Published on 2010-06-03T04:15:11Z
Indexed on
2010/06/03
4:24 UTC
Read the original article
Hit count: 176
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;
}
© Stack Overflow or respective owner