First Attempt - 'usleep()' or 'nanosleep()'
- These functions will sleep a given ammount of time (usleep is BSDish
and accepts micro-seconds, nanosleep is POSIX and accepts 'struct
timespec', which contains nano-seconds).
- nanosleep also tells us how much time is left to sleep, in case we were
woken prematurely (e.g. by receiving a signal). More on the use of this
later.
- They both suffer from the Linux-timeslice effect - they will sleep
at least the minimal time-resolution of the kernel (1/1024 on
i386 hardware with a 2.6 kernel, 1/100 with a 2.4 kernel, etc).
- Thus, to sleep less - we need to avoid sleeping at all, and use
busy-wait.
Originally written by
guy keren