coperating-systemsystemsleepsystem-calls

Is function sleep() active or passive?


Is function sleep() in C an active wait or passive wait?

Since it stops the thread running, is it always checking if the time has passed like:

while(1){
//need to wake?
}

Or a passive like:

alarm(sec);

pause(); // wait for the alarm and sleeping?

The system is unix.


Solution

  • sleep is not a standard C library function.

    If your platform has it, it almost certainly will make a call to the operating system to suspend the thread (that is, in your notation, passive).

    It will not adopt a while(1){}-type idiom as that will unnecessarily burn the CPU.