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 handler\n");
    else
        errExitEN(s, "clock_nanosleep");
}

目录
相关文章
|
23天前
|
存储 算法 Linux
理解和使用 C++ 中的 `times` 函数:度量进程时间
理解和使用 C++ 中的 `times` 函数:度量进程时间
36 0
|
11月前
|
机器学习/深度学习 监控 自动驾驶
STDC升级 | STDC-MA 更轻更快更准,超越 STDC 与 BiSeNetv2
STDC升级 | STDC-MA 更轻更快更准,超越 STDC 与 BiSeNetv2
207 0
STDC升级 | STDC-MA 更轻更快更准,超越 STDC 与 BiSeNetv2
十一、Belady现象和LRU、FIFO、clock的比较
十一、Belady现象和LRU、FIFO、clock的比较
|
开发者
Alarm-Clock 实验过程|学习笔记
快速学习 Alarm-Clock 实验过程
148 0
Alarm-Clock 实验过程|学习笔记
|
传感器 芯片
使用系统定时器SysTick实现精确延时微秒和毫秒函数
使用系统定时器SysTick实现精确延时微秒和毫秒函数
338 0
使用系统定时器SysTick实现精确延时微秒和毫秒函数
|
测试技术
软件测试面试题:什么是Think Time?你如何改变这个阈值?
软件测试面试题:什么是Think Time?你如何改变这个阈值?
115 0
|
编解码 安全 Linux
Linux内核开发基础-低精度timer_list和高精度hrtimer定时器
上篇文章讲解了如何正确的使用内核延时函数,在进行驱动开发时,可能会经常用到精确地延时操作。除此之外,如果要实现一个定时任务,那就需要用到定时器。作为一项基础功能需求,Linux内核提供了定时器相关的实现。下面就具体看一下,Linux内核所提供的定时器实现。
958 0
|
存储 JavaScript 算法