Linux 为什么在 C 中创建睡眠时间时,clock_nanosleep 优于 nanosleep?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7794955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 06:43:03 来源:igfitidea点击:
Why is clock_nanosleep preferred over nanosleep to create sleep times in C?
提问by payyans4u
Which one of the two functions is better
两个功能哪个更好
#include <time.h>
int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *rqtp, struct timespec *rmtp);
OR
或者
#include <time.h>
int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
采纳答案by R.. GitHub STOP HELPING ICE
The advantages of clock_nanosleep
over nanosleep
are:
clock_nanosleep
over的优点nanosleep
是:
- You can specify an absolute time to sleep untilrather than an interval to sleep. This makes a difference for the realtime (wall time) clock, which can be reset by the administrator or
ntpd
, etc. Withnanosleep
and precalculating the interval to sleep to reach a given absolute time, you'll fail to wake if the clock is reset and the desired time arrives "early". Also, there's a race condition with scheduling using interval times: If you compute the interval you want to sleep, but you get preempted before callingnanosleep
and don't get scheduled again for a while, you'll again sleep too long. - You can sleep on timers other than the realtime clock. The most useful is usually the monotonic clock (which can't be reset and increases monotonically with the progression of actual time), but there are also other interesting applications like having one thread in a multi-threaded process sleep on the process's cpu time clock (so it wakes after the process has used a given amount of cpu time).
- 你可以指定一个绝对时间睡觉,直到而非间隔的睡眠。这对实时(挂钟)时钟有所不同,可以由管理员或
ntpd
等重置。通过nanosleep
并预先计算睡眠间隔以达到给定的绝对时间,如果时钟被重置,您将无法唤醒并且所需的时间“提前”到达。此外,使用间隔时间进行调度存在竞争条件:如果您计算想要睡眠的间隔,但是在调用之前被抢占nanosleep
并且有一段时间没有再次被调度,那么您将再次睡眠太久。 - 您可以在实时时钟以外的计时器上睡觉。最有用的通常是单调时钟(它不能被重置并随着实际时间的进展单调增加),但也有其他有趣的应用,比如让多线程进程中的一个线程在进程的 CPU 时钟上休眠(因此它在进程使用了给定的 CPU 时间后唤醒)。
回答by NPE
On my system, man 2 clock_nanosleep
explains the differences between the two functions thus:
在我的系统上,man 2 clock_nanosleep
解释了这两个函数之间的差异:
Like nanosleep(2), clock_nanosleep() allows the caller to sleep for an interval specified with nanosecond precision. It differs in allowing the caller to select the clock against which the sleep interval is to be measured, and in allowing the sleep interval to be specified as either an absolute or a relative value. The clock_id argument [...] can have one of the following values: CLOCK_REALTIME A settable system-wide real-time clock. CLOCK_MONOTONIC A non-settable, monotonically increasing clock that measures time since some unspecified point in the past that does not change after system startup. CLOCK_PROCESS_CPUTIME_ID A settable per-process clock that measures CPU time consumed by all threads in the process.