usleep

Heiko Wundram (Beenic) wundram at beenic.net
Mon Feb 25 09:39:37 UTC 2008


Am Montag, 25. Februar 2008 10:10:56 schrieb Sharad Chandra:
> So does it mean, freebsd has limitation. sleeping will only work for its
> value more than 1 milli sec because % of +- error value is comparitivly
> low? I am curious to know, is there any method which sleeps for few
> microseconds. Some one please give me link where to look for "select".

No, this does not mean that FreeBSD has a limitation, but rather that FreeBSD 
is not a real-time operating system. You'll see the similar behaviour on 
pretty much any other operating system that is _not_ a RTOS (for example, 
Windows has a fixed resolution of non-Multimedia-Timers of around 10ms [where 
Multimedia Timers are a specific interface to sleep with a higher resolution, 
designed for Multimedia applications, but the resolution isn't that much 
higher, anyway], and on Linux, it also depends on the Tick-Frequency, similar 
to FreeBSD's HZ setting, but actually I don't know how the tickless 
Linux-kernel [which is implemented only in the latest versions, and not 
default anyway] behaves here).

There is no way (AFAIK) for a userland application to _sleep_ for a finer 
grained amount of time than a timeslice, except in a busy loop which calls 
gettimeofday or similar repeatedly to break when the "timeout" occurs, but 
even then you don't have a guarantee that you won't sleep longer, simply 
because the process might be preempted just before the timeout occurs because 
another process also wants its timeslice.

But, more specifically, why do you need to sleep for a short time in a user 
process anyway? Normally, you're blocking on some form of condition, which 
will "wake you up" as soon as the condition triggers and you can be scheduled 
(and in this "active scheduling" case the time-slice "rule" doesn't apply).

If you give some more info on what you're trying to do (and why you need such 
a high resolution sleep), maybe someone will be able to help you better and 
show you how to achieve what you're trying to do without blocking on a 
timeout.

-- 
Heiko Wundram
Product & Application Development


More information about the freebsd-hackers mailing list