Created
March 2, 2018 04:39
-
-
Save dimalinux/72f714ced08c634f3b6b90b05ff1fe03 to your computer and use it in GitHub Desktop.
Using futex syscall to implement sleep
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 <stdint.h> | |
#include <time.h> | |
#include <unistd.h> | |
#include <sys/syscall.h> | |
#include <linux/futex.h> | |
int | |
futex_sleep (time_t seconds, long nanoseconds) | |
{ | |
uint32_t futexWord = 0; | |
struct timespec timeout = { seconds, nanoseconds }; | |
return syscall (SYS_futex, &futexWord, FUTEX_WAIT, futexWord, &timeout, | |
NULL, 0); | |
} | |
int | |
main () | |
{ | |
time_t secs = 2; | |
printf ("Before futex_sleep for %ld seconds\n", secs); | |
futex_sleep (2, 0); | |
printf ("After futex_sleep\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment