#include #include #include #include #include void handler(int sig) { printf("got wakeup call (signal %d)\n", sig); } void *thread(void *args) { /* make sure SIGALARM is not blocked */ sigset_t mask; sigemptyset(&mask); pthread_sigmask(SIG_SETMASK, &mask, NULL); /* schedule SIGALRM to cut sleep short */ printf("thread going to sleep\n"); alarm(1); sleep(5); printf("thread done\n"); return NULL; } int main() { /* register the alarm signal handler */ struct sigaction sighandler; sighandler.sa_flags = 0; sighandler.sa_handler = handler; sigfillset(&sighandler.sa_mask); sigaction(SIGALRM, &sighandler, NULL); /* create a thread */ pthread_t tid; pthread_create(&tid, NULL, thread, NULL); /* now block all signals for main thread */ sigset_t mask; sigfillset(&mask); sigdelset(&mask, SIGINT); /* except SIGINT for Ctrl-C bailout */ /* sigdelset(&mask, SIGALRM); * this unblocked gets handler called */ pthread_sigmask(SIG_SETMASK, &mask, NULL); /* now wait for SIGHUP */ printf("main waiting for SIGHUP as %d\n", getpid()); int rc, sig; sigset_t expected; sigemptyset(&expected); sigaddset(&expected, SIGHUP); rc = sigwait(&expected, &sig); printf("sigwait rc %d got signal %d: %d\n", rc, sig, errno); /* gets errno=EINTR if SIGALRM is unblocked above */ /* and wait for the thread */ pthread_join(tid, NULL); /* see what signals are still pending */ /* this segfaults, unless SIGALRM in unblocked above sigset_t pending; sigpending(&pending); printf("pending signals: %x\n", pending); */ printf("main done\n"); return 0; }

AltStyle によって変換されたページ (->オリジナル) /