Last active
December 15, 2015 22:29
-
-
Save droyo/5333884 to your computer and use it in GitHub Desktop.
signalfd SFD_NONBLOCK test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <sys/signalfd.h> | |
#include <signal.h> | |
#include <errno.h> | |
#include <stdlib.h> | |
int sigfd_read(int fd) { | |
ssize_t n; | |
struct signalfd_siginfo buf; | |
n = read(fd, &buf, sizeof buf); | |
return (n <= 0) ? n : buf.ssi_signo ; | |
} | |
int sigfd_make(void) { | |
sigset_t mask; | |
sigemptyset(&mask); | |
sigaddset(&mask, SIGUSR1); | |
sigaddset(&mask, SIGUSR2); | |
if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) { | |
perror("sigprocmask"); | |
return -1; | |
} | |
return signalfd(-1, &mask, SFD_NONBLOCK); | |
} | |
int main(int argc, char **argv) { | |
int fd; | |
int sig; | |
if ((fd = sigfd_make()) < 0) { | |
perror("sigfd"); | |
exit(1); | |
} | |
for(;;) { | |
printf("Listen for signal...\n\t"); | |
sig = sigfd_read(fd); | |
switch(sig) { | |
case SIGUSR1: | |
puts(" User signal 1"); | |
break; | |
case SIGUSR2: | |
puts(" User signal 2"); | |
break; | |
case -1: | |
if (errno == EAGAIN) { | |
printf(" nothing\n"); | |
} else { | |
perror("read signal"); | |
exit(1); | |
} | |
break; | |
default: | |
break; | |
} | |
sleep(1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment