首页 > 其他 > 详细

clock_nanosleep避免过度睡眠

时间:2014-03-06 23:32:17      阅读:682      评论:0      收藏:0      [点我收藏+]
/*
 *   “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 handler\n");
    else
        errExitEN(s, "clock_nanosleep");
}

clock_nanosleep避免过度睡眠,布布扣,bubuko.com

clock_nanosleep避免过度睡眠

原文:http://blog.csdn.net/nerdx/article/details/20646901

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!