-
-
Save depressed-pho/5d117dbca872ef7c28ee7786e0ad8a8a to your computer and use it in GitHub Desktop.
This file contains 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 <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