分类: 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. Subversion详解

    Subversion特性 1.版本化目录 可以跟踪目录树的变更.文件和目录都是版本化的,SVS只能跟踪单个文件: 2.精确的文件跟踪 可以对文件和目录进行 增加.复制.改名,解决了同名 而 无关的文件 ...

  2. BZOJ 4144: [AMPPZ2014]Petrol

    4144: [AMPPZ2014]Petrol Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 457  Solved: 170[Submit][Sta ...

  3. Linux下创建文本文件(vi/vim命令使用详解)

    vi test.txt 或者 vim test.txt vim是vi的升级版,指令更多,功能更强. 下面是收集的vim用法,当在vim里面要实现退出,首先要做的是按[Esc],然后再输入[:wq] 一 ...

  4. IIS提示“异常详细信息: System.Runtime.InteropServices.ExternalException: 无法执行程序”

    先来看错误提示: 无法执行程序.所执行的命令为 "C:/Windows/Microsoft.NET/Framework/v3.5/csc.exe" /noconfig /fullp ...

  5. hdu 4324 拓扑排序

    题意:给出一堆人的喜爱关系,判断有没有三角恋-_-|| 其实就是判断是否存在三条边的环. 一开始我是这么想的: 先拓扑排序,如果没有环那就直接No 如果有环?挑出环里的任意一个点(拓扑排序结束后不在拓 ...

  6. POI2007_zap 莫比乌斯反演

    题意:http://hzwer.com/4205.html 同hdu1695 #include <iostream> #include <cstring> #include & ...

  7. wildfly jsf 文件 上传后 可以下载 访问

    // String aa = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath(); // lo ...

  8. Arcgis 几何网络分析

    ArcGIS:网络分析(转)   由于之前对网络分析的理解有很多问题,所以在网上找了一些资料,这是其中一篇我觉得比较好的,所以就整理了一下,发到网上来,留个底吧,呵呵 注:关于几何网络的建立见前面的& ...

  9. win7下如何建立ftp服务器

    前段时间正在做一个项目,需要上传东西到ftp服务器,纠结于如何建立ftp服务器.经过一番摸索.终于成功建立ftp服务器.现将我的经验跟大家分享一下.不足之处还望多多指点! 步骤/方法 首先在本地机器上 ...

  10. 自然语言20_The corpora with NLTK

    QQ:231469242 欢迎喜欢nltk朋友交流 https://www.pythonprogramming.net/nltk-corpus-corpora-tutorial/?completed= ...