Skip to content

Instantly share code, notes, and snippets.

Created October 31, 2021 12:20
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
A small app that demonstrates using signalfd to handle signals in a normal thread context
// Error handling omitted for brevity
// compile with: gcc signalfd.c -o signalfd
#include <sys/signalfd.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <poll.h>
void handle_signal(int signal_fd)
struct signalfd_siginfo siginfo;
ssize_t s;
s = read(signal_fd, &siginfo, sizeof(siginfo));
if (s != sizeof(siginfo))
uint32_t signo = siginfo.ssi_signo;
char *signame = strsignal(signo);
printf("Received signal %d (%s)\n", signo, signame);
int main()
sigset_t mask;
sigaddset(&mask, SIGINT);
sigprocmask(SIG_SETMASK, &mask, NULL);
int signal_fd = signalfd(-1, &mask, SFD_NONBLOCK | SFD_CLOEXEC);
struct pollfd pollfd = {
.fd = signal_fd,
.events = POLLIN,
while (poll(&pollfd, 1, 5000) > 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment