Skip to content

Instantly share code, notes, and snippets.

@dimalinux
Created March 2, 2018 04:39
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 dimalinux/72f714ced08c634f3b6b90b05ff1fe03 to your computer and use it in GitHub Desktop.
Save dimalinux/72f714ced08c634f3b6b90b05ff1fe03 to your computer and use it in GitHub Desktop.
Using futex syscall to implement sleep
#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