函数 sleep() 在被动时是主动的吗?

Is function sleep() active on passive?

C中的sleep()函数是主动等待还是被动等待?

既然它停止了线程 运行,它是否总是检查时间是否已经过去,如:

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

或者像这样的被动:

alarm(sec);

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

系统为unix

sleep 不是 标准 C 库函数。

如果你的平台有它,它几乎肯定会调用操作系统来挂起线程(也就是说,在你的符号中,passive)。

它不会采用 while(1){} 类型的习语,因为那样会不必要地烧毁 CPU。

sleep() 函数的实现有两种通用方法。

一种是使用alarm()函数调度一个SIGALRM信号,然后挂起等待该信号的调用线程。二是实施独立设施。

函数 sleep() 在 unix 中处于被动等待状态。

sleep() 是一个 standard POSIX function:

The sleep() function shall cause the calling thread to be suspended from execution until either the number of realtime seconds specified by the argument seconds has elapsed or a signal is delivered to the calling thread and its action is to invoke a signal-catching function or to terminate the process. The suspension time may be longer than requested due to the scheduling of other activity by the system.

...