分类: 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. Pointcut is not well-formed: expecting 'name pattern' at character position

    配置aop报错:原因是配置切点表达式的时候报错了, 星号后面没有加空格: <aop:config> <aop:pointcut id="transactionPointcu ...

  2. Maven报错: Missing artifact jdk.tools:jdk.tools:jar:1.7

    pom.xml 有小红叉,报错: Missing artifact jdk.tools:jdk.tools:jar:1.7 解决方法:(缺少一个jar包) http://blog.csdn.net/u ...

  3. Mysql将近两个月的记录合并为一行显示

    最近做报表统计,用到要求把近两个月的绩效作比较,并作出一些环比数据等. 场景:将1班同学的两个月的语文的平均成绩合并到一行比较. CREATE TABLE `Chinese_score` ( `id` ...

  4. [日常训练]FJ省夏令营day1

    T1 Description 给出n个矩形的顶点坐标(每个矩形的底边都在x轴上),求这n个矩形所组成图形的轮廓线的顶点. Input 第一行一个整数n,表示矩形个数. 以下n行,每行3个整数,分别表示 ...

  5. linux永久更改eth0的ip地址

    linux中永久修改ip和子网掩码,可以用命令也可以直接到文件里修改.这里推荐直接到文件中修改.1.进入网卡配置文件 vi /etc/sysconfig/network-scripts/ifcfg-e ...

  6. java web项目导入到eclipse中变成了java项目的一种情况的解决办法

    前提,我把代码上传到github上之后,在另外一台电脑上拉下之后,先报出现的是jre不对,然后换成了当前的jre,然后红色的感叹号消失了但是之前项目上那个小地球不见了,也就是说变成了java项目. - ...

  7. C语言之流的重定向

    写c的小程序断不了需要输入输出,手动输入可太麻烦了.下面介绍IO的重定向方式: .重定向标准输入输出和错误,直接在命令行使用符号< > > >> >>等,还可 ...

  8. 基本概率分布Basic Concept of Probability Distributions 2: Poisson Distribution

    PDF version PMF A discrete random variable $X$ is said to have a Poisson distribution with parameter ...

  9. AlsaInfo

    这是一个不能不说的故事. 我装了Ubuntu以后,耳机一直不能用. 查了各种资料也搞不定. DEBUG声音问题时有一个重要的参考就是AlsaInfo,里面详细列出了关于声音的各种配置信息. 如何获得这 ...

  10. MAC中Django中runserver提示Can't connect to local MySQL server through socket '/tmp/mysql.sock错误

    好像不止遇到一次,直接Google就可以了,在stackoverflow中就有答案,答案就是你没有开MySQL - -. stackoverflow链接见 http://stackoverflow.c ...