Skip to content

Instantly share code, notes, and snippets.

@depressed-pho
Created February 6, 2023 05:27
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 depressed-pho/5d117dbca872ef7c28ee7786e0ad8a8a to your computer and use it in GitHub Desktop.
Save depressed-pho/5d117dbca872ef7c28ee7786e0ad8a8a to your computer and use it in GitHub Desktop.
#include <pthread.h>
#include <stdio.h>
#include <stdint.h>
#include <sys/timerfd.h>
#include <unistd.h>
void* thread_main(void* arg) {
int fd = *((int*)arg);
printf("Waiting for the timer to fire.\n");
uint64_t count;
read(fd, &count, sizeof(count));
printf("Closing the timerfd on another thread. PREPARE FOR THE KERNEL PANIC!\n");
close(fd);
printf("Wow it survived. We're still alive...\n");
return NULL;
}
int main() {
int fd = timerfd_create(CLOCK_REALTIME, 0);
if (fd == -1) {
perror("timerfd_create");
return 1;
}
struct itimerspec tim = {{1, 0}, {1, 0}};
if (timerfd_settime(fd, 0, &tim, NULL) != 0) {
perror("timerfd_settime");
return 1;
}
printf("Created a timerfd on the main thread: %d\n", fd);
pthread_t thr;
if (pthread_create(&thr, NULL, thread_main, &fd) != 0) {
perror("pthread_create");
return 1;
}
pthread_join(thr, NULL);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment