我是靠谱客的博主 细腻大炮,最近开发中收集的这篇文章主要介绍clock_nanosleep避免过度睡眠,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

/*
 *   “oversleeping” problem is particularly marked for a process that uses a loop to re start
 *   a sleep that is interrupted by a signal handler. If signals are delivered at a high rate
 *   , then a relative sleep (of the type performed by nanosleep()) can lead to large inaccuracies 
 *   in the time a process spends sleeping. We can avoid the oversleeping problem by making an
 *   initial call to clock_gettime() to retrieve the time, adding the  desired amount to that time, 
 *   and then calling  clock_nanosleep() with the TIMER_ABSTIME  flag (and restarting the system
 *   call if it is interrupted by a signal handler).
 */

struct timespec request;
/* Retrieve current value of CLOCK_REALTIME clock */
if (clock_gettime(CLOCK_REALTIME, &request) == -1) 
    errExit("clock_gettime");
request.tv_sec += 20;               /* Sleep for 20 seconds from now */
s = clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &request, NULL); 
if (s != 0) {
    if (s == EINTR)
        printf("Interrupted by signal handlern");
    else
        errExitEN(s, "clock_nanosleep");
}

最后

以上就是细腻大炮为你收集整理的clock_nanosleep避免过度睡眠的全部内容,希望文章能够帮你解决clock_nanosleep避免过度睡眠所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(59)

评论列表共有 0 条评论

立即
投稿
返回
顶部