usleep/sleep/Sleep/etc

I was talking with someone in my CSE class and they said:

They wrote:
I once read that sleep and usleep should never be used simply because the parameter given is the minimum time to sleep, but the system is free to take longer for any reason.


Thoughts? I personally think this is stupid:

I wrote:
Well, that reason is incredibly stupid IMHO. The OS could theoretically take however long it wants to do anything that you tell it to do...
Last edited on
That's POSIX for you, and those are the semantics you'll get on any OS that isn't truly realtime.
I saw usleep(1) takes more than 50 microseconds in some embedded system. A faster CPU can give you a more accurate result. But if you want really accurate function, you may need to use a loop that keeps checking the time stamp using assembly commands.
A faster CPU can give you a more accurate result.
I thought it depended on the scheduler's loop frequency, not the CPU.
Not necessarily.

The granularity of usleep cannot be any finer than that of the hardware clock. As long as the kernel can execute code
when a clock interrupt occurs, the kernel *could* context switch at that point.

You should be aware that some (perhaps older) platforms would busy-loop in the kernel to emulate sleeps of
less than some threshold (like 1 or 2 msecs).
Topic archived. No new replies allowed.