分类: LINUX

    Heartwork前辈在我前一篇博文多线程条件下的计数器(2)中的回复中提到,

 
nanosleep的问题很好解释,看这里……

The nanosleep() function shall cause the current thread to be suspended from execution until either the time interval specified by the rqtp argument has elapsed or a signal is delivered to the calling thread, and its action is to invoke a signal-catching function or to terminate the process. The suspension time may be longer than requested because the argument value is rounded up to an integer multiple of the sleep resolution or because of the scheduling of other activity by the system. But, except for the case of being interrupted by a signal, the suspension time shall not be less than the time specified by rqtp, as measured by the system clock CLOCK_REALTIME.

The use of the nanosleep() function has no effect on the action or blockage of any signal.

简单来说,这个误差是与调度器的时间片和调度策略有关的,也就是可以通过减小时间片大小和使用实时性更好的调度策略(比如SCHED_RR)来获得更小的分辨率。

 
下来我写了段测试代码,对默认情况和设置调度算法后的nanosleep做了测量。
 
  • #include<stdio.h>
  • #include<stdlib.h>
  • #include<unistd.h>
  • #include<sys/time.h>
  • #include<sched.h>
  • #define COUNT 10000
  • #define MILLION 1000000L
  • #define NANOSECOND 1000
  • int main(int argc,char* argv[])
  • {
  • int i;
  • struct timespec sleeptm;
  • long interval;
  • struct timeval tend,tstart;
  • struct sched_param param;
  • if(argc != 2)
  • {
  • fprintf(stderr,"usage:./test sched_method\n");
  • return -1;
  • }
  • int sched = atoi(argv[1]);
  • param.sched_priority = 1;
  • sched_setscheduler(getpid(),sched,&param);
  • int scheduler = sched_getscheduler(getpid());
  • fprintf(stderr,"default scheduler is %d\n",scheduler);
  • sleeptm.tv_sec = 0;
  • sleeptm.tv_nsec = NANOSECOND;
  • if(gettimeofday(&tstart,NULL)!=0)
  • {
  • fprintf(stderr,"get start time failed \n");
  • return -2;
  • }
  • for(i = 0;i<COUNT;i++)
  • {
  • if(nanosleep(&sleeptm,NULL) != 0)
  • {
  • fprintf(stderr,"the %d sleep failed\n",i);
  • return -3;
  • }
  • }
  • if(gettimeofday(&tend,NULL)!=0)
  • {
  • fprintf(stderr,"get end time failed \n");
  • return -4;
  • }
  • interval = MILLION*(tend.tv_sec - tstart.tv_sec)
  • +(tend.tv_usec-tstart.tv_usec);
  • fprintf(stderr,"the expected time is %d us,but real                                   time cost is %lu us\n",COUNT,interval);
  • return 0;
  • }
  • root@libin:~/program/C/timer# ./test 0
  • default scheduler is 0
  • the expected time is 10000 us,but real time cost is 630624 us
  • root@libin:~/program/C/timer# ./test 1
  • default scheduler is 1
  • the expected time is 10000 us,but real time cost is 67252 us
  • root@libin:~/program/C/timer# ./test 2
  • default scheduler is 2
  • the expected time is 10000 us,but real time cost is 82449 us
 
所谓调度算法值 0 1  2 分别是什么呢?bit/sched.h中定义:
 
  • #define SCHED_OTHER 0
  • #define SCHED_FIFO 1
  • #define SCHED_RR 2
  • #ifdef __USE_GNU
  • # define SCHED_BATCH 3
  • #endif
从结果上看,采用默认的算法是SCHED_OTHER nanosleep是最不准确的,理想情况下,每次睡1us,睡10000次,耗时因该是10ms,实际上耗时是630ms。采用时间片轮转的SCHED_RR nanosleep的计时 要准确一些,耗时为82ms.依次类推。
 
 
至于调度算法如何影响nanosleep,我还不是很清楚,里面还有很多东西需要我继续学习。好久没写博客了,先将这篇不成熟的博文拿出,抛砖引玉。
 
参考文献:
1 UNIX 系统编程
2 Heartwork的回复

【转】nanosleep的精度与调度算法的关系 来自:bean.blog.chinaunix.net的更多相关文章

  1. listener.ora--sqlnet.ora--tnsnames.ora的关系以及手工配置举例(转载:http://blog.chinaunix.net/uid-83572-id-5510.ht)

    listener.ora--sqlnet.ora--tnsnames.ora的关系以及手工配置举例 ====================最近看到好多人说到tns或者数据库不能登录等问题,就索性总结 ...

  2. 峰Spring4学习(5)bean之间的关系和bean的作用范围

    一.bean之间的关系: 1)继承: People.java实体类: package com.cy.entity; public class People { private int id; priv ...

  3. Spring初学之bean之间的关系和bean的作用域

    一.bean之间的关系 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="h ...

  4. Spring -- Bean自己主动装配&amp;Bean之间关系&amp;Bean的作用域

    对于学习spring有帮助的站点:http://jinnianshilongnian.iteye.com/blog/1482071 Bean的自己主动装配 Spring IOC 容器能够自己主动装配 ...

  5. Jaxb 解析 带有继承关系的bean与xml

    具体方法: 1. 在jaxb的setClasstobebounds中,只需要子类的class,无需父类. 2. 父类的前面加如下声明: @XmlAccessorType(XmlAccessType.F ...

  6. nobup 与 后台运行命令

    1. Linux进程状态:R (TASK_RUNNING),可执行状态&运行状态(在run_queue队列里的状态) 2. Linux进程状态:S (TASK_INTERRUPTIBLE),可 ...

  7. Linux epoll 笔记(高并发事件处理机制)

    wiki: Epoll优点: Epoll工作流程: Epoll实现机制: epollevent; Epoll源码分析: Epoll接口: epoll_create; epoll_ctl; epoll_ ...

  8. linux包之procps之ps与top

    概述 阅读man ps页,与man top页,最权威与标准,也清楚 有时候系统管理员可能只关心现在系统中运行着哪些程序,而不想知道有哪些进程在运行.由于一个应用程序可能需要启动多个进程.所以在同等情况 ...

  9. [Freescale]Freescale L3.14.52_1.1.0 yocto build

    可参照:http://blog.csdn.net/wince_lover/article/details/51456745 1. Refer to <基于i.mx6处理器的Yocto项目及Lin ...

随机推荐

  1. python time函数

    时间间隔是以秒为单位的浮点小数. 每个时间戳都以自从1970年1月1日午夜(历元)经过了多长时间来表示. Python附带的受欢迎的time模块下有很多函数可以转换常见日期格式.如函数time.tim ...

  2. 【BZOJ-4514】数字配对 最大费用最大流 + 质因数分解 + 二分图 + 贪心 + 线性筛

    4514: [Sdoi2016]数字配对 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 726  Solved: 309[Submit][Status ...

  3. 【BZOJ-4423】Bytehattan 并查集 + 平面图转对偶图

    4423: [AMPPZ2013]Bytehattan Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 144  Solved: 103[Submit][ ...

  4. 如果把表单数据的校验交给了javascript那么后台还有没有必要对数据做校验呢

    现在很多同事,包括我,我表单的数据验证交给了javascript来做,那么新的问题来了,如果交给了javascript那么后台还有没有必要对数据做校验呢

  5. 【bzoj3674】 可持久化并查集加强版

    http://www.lydsy.com/JudgeOnline/problem.php?id=3674 (题目链接) 题意 维护并查集3个操作:合并:回到完成第k个操作后的状态:查询. Soluti ...

  6. BZOJ1922 [Sdoi2010]大陆争霸

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...

  7. Ext FileSystem Family、Ext2、Ext3

    catalog . 简介 . Ext2文件系统 . Ext3文件系统 . 小结 1. 简介 VFS虚拟文件系统接口和数据结构构成了一个框架,各个文件系统的实现都必须在框架内运转,但这并不要求每个文件系 ...

  8. Appium for IOS testing on Mac

    一:环境 1.Mac OS X 10.9.1 2.Xcod 5.0.2 3.Appium 1.3.6 下载地址:https://bitbucket.org/appium/appium.app/down ...

  9. Jacobian矩阵和Hessian矩阵

    1.Jacobian矩阵 在矩阵论中,Jacobian矩阵是一阶偏导矩阵,其行列式称为Jacobian行列式.假设 函数 $f:R^n \to R^m$, 输入是向量 $x \in R^n$ ,输出为 ...

  10. hdu 2042 - 不容易系列之二

    题意:(略)太长了 解法:找规律,发现A[i]=A[i-1]*2-2; 1: #include<stdlib.h> 2: #include<string.h> 3: #incl ...