为什么在 C 语言中创建睡眠时间时,clock_nanosleep 优于 nanosleep?

发布于 2024-12-10 12:01:52 字数 299 浏览 1 评论 0原文

这两个功能哪一个更好

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

或者

#include <time.h>
int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);

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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云朵有点甜 2024-12-17 12:01:52

clock_nanosleep 相对于 nanosleep 的优点是:

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

The advantages of clock_nanosleep over nanosleep are:

  1. You can specify an absolute time to sleep until rather 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. With nanosleep 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 calling nanosleep and don't get scheduled again for a while, you'll again sleep too long.
  2. 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).
风为裳 2024-12-17 12:01:52

在我的系统上,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.

On my system, man 2 clock_nanosleep explains the differences between the two functions thus:

   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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文