Skip to content

Instantly share code, notes, and snippets.

@vtjnash
Last active December 14, 2016 01:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vtjnash/92a0165948e3fffd93644af6db39ab25 to your computer and use it in GitHub Desktop.
Save vtjnash/92a0165948e3fffd93644af6db39ab25 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <pthread.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <sys/time.h>
static volatile int nsignals, nerrors;
static void *signal_listener(void *arg)
{
sigset_t sset;
while (1) {
int sig = SIGALRM;
sigemptyset(&sset);
sigaddset(&sset, SIGHUP);
sigaddset(&sset, SIGINT);
sigaddset(&sset, SIGINFO);
errno = 0;
if (sigwait(&sset, &sig))
abort();
if (errno)
nerrors++;
else
printf("got %d, errno %d\n", sig, errno);
nsignals++;
}
}
static void sigchld(int sig, struct __siginfo *siginfo, void *dummy) { }
static void sigalrm(int sig, struct __siginfo *siginfo, void *dummy) { }
int main(int argc, char **argv)
{
pthread_t signals_thread;
struct sigaction act;
memset(&act, 0, sizeof(struct sigaction));
sigemptyset(&act.sa_mask);
act.sa_sigaction = sigchld;
if (sigaction(SIGCHLD, &act, NULL) < 0)
abort();
act.sa_sigaction = sigalrm;
if (sigaction(SIGALRM, &act, NULL) < 0)
abort();
if (pthread_create(&signals_thread, NULL, signal_listener, NULL) != 0)
abort();
struct itimerval tm;
tm.it_value.tv_sec = 0;
tm.it_value.tv_usec = 25;
tm.it_interval.tv_sec = 0;
tm.it_interval.tv_usec = 25;
setitimer(ITIMER_REAL, &tm, NULL);
puts("start.\n");
int i;
sigset_t sset, oset;
sigfillset(&sset);
for (i = 0; i < 1024; i++) {
sigprocmask(SIG_SETMASK, &sset, &oset);
usleep(1000);
sigprocmask(SIG_SETMASK, &oset, NULL);
}
puts("done.\n");
printf("wakeups: %d\nerrors: %d\n", nsignals, nerrors);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment