Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么clock_nanosleep比nanosleep更适合在C语言中创建睡眠时间?_C_Linux_Linux Kernel_System Calls - Fatal编程技术网

为什么clock_nanosleep比nanosleep更适合在C语言中创建睡眠时间?

为什么clock_nanosleep比nanosleep更适合在C语言中创建睡眠时间?,c,linux,linux-kernel,system-calls,C,Linux,Linux Kernel,System Calls,这两个功能中哪一个更好 #include <time.h> int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *rqtp, struct timespec *rmtp); #包括 int clock_nanosleep(clockid_t clock_id,int标志,const struct timespec*rqtp,struct timespec*rmtp); 或 #包括 int

这两个功能中哪一个更好

#include <time.h>
int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *rqtp, struct timespec *rmtp);
#包括
int clock_nanosleep(clockid_t clock_id,int标志,const struct timespec*rqtp,struct timespec*rmtp);

#包括
int nanosleep(const-struct-timespec*rqtp,struct-timespec*rmtp);

在我的系统上,
man 2 clock\u 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. 与nanosleep(2)一样,clock_nanosleep()允许调用者睡眠一段时间 以纳秒精度指定的间隔。不同之处在于允许 调用者选择睡眠时间间隔所对应的时钟 进行测量,并允许将睡眠间隔指定为 绝对值或相对值。 clock_id参数[…]可以具有以下值之一: 时钟\实时一种可设置的全系统实时时钟。 时钟是一种不可设置的、单调递增的时钟 从过去的某个未指定点开始测量时间 这在系统启动后不会改变。 时钟\u进程\u CPU使用时间\u ID 测量CPU时间的可设置的每个进程时钟 由进程中的所有线程使用。
nanosleep
相比,
clock\u nanosleep
的优点是:

  • 您可以指定睡眠的绝对时间,而不是睡眠的间隔。这对实时(墙上时间)时钟产生了影响,管理员或ntpd等可以重置实时(墙上时间)时钟。使用
    nanosleep
    并预先计算睡眠间隔以达到给定的绝对时间,如果时钟重置且所需时间“提前”到达,您将无法唤醒。此外,使用间隔时间进行调度也存在一个竞争条件:如果您计算了想要睡眠的间隔,但在调用
    nanosleep
    之前被抢占,并且在一段时间内没有再次进行调度,那么您将再次睡得太久
  • 你可以在实时时钟以外的计时器上睡觉。最有用的通常是单调时钟(它不能重置,并且随着实际时间的推移单调增加),但也有其他有趣的应用程序,比如在多线程进程中有一个线程在进程的cpu时钟上睡眠(因此它在进程使用了给定的cpu时间后唤醒)

  • 定义“好”。便携、高效、知名、易懂、准确,什么?你看过手册了吗@桑贾辛托:没有人读手册。就像我问你“你读过Facebook的服务条款了吗”或者一个机械师问你“你读过用户手册了吗”当你不明白为什么你的远光灯不能工作时=PThanks。。。我使用posix precision sleep standard lib函数来睡眠,但它似乎返回得太快了。请检查此代码并提供修复函数以正确睡眠正确时间的方法的详细信息。只想添加注释以防止混淆。。。Linux中的nanosleep()使用时钟\单调时钟。因此,如果你想在Linux中使用时钟以外的其他时钟,那你就需要使用clock_nanosleep()。@It'sPete:相对时间睡眠不是一样吗? 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.