【转】nanosleep的精度与调度算法的关系 来自:bean.blog.chinaunix.net
分类: LINUX
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)来获得更小的分辨率。
- #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,¶m);
- 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
- #define SCHED_OTHER 0
- #define SCHED_FIFO 1
- #define SCHED_RR 2
- #ifdef __USE_GNU
- # define SCHED_BATCH 3
- #endif
【转】nanosleep的精度与调度算法的关系 来自:bean.blog.chinaunix.net的更多相关文章
- listener.ora--sqlnet.ora--tnsnames.ora的关系以及手工配置举例(转载:http://blog.chinaunix.net/uid-83572-id-5510.ht)
listener.ora--sqlnet.ora--tnsnames.ora的关系以及手工配置举例 ====================最近看到好多人说到tns或者数据库不能登录等问题,就索性总结 ...
- 峰Spring4学习(5)bean之间的关系和bean的作用范围
一.bean之间的关系: 1)继承: People.java实体类: package com.cy.entity; public class People { private int id; priv ...
- Spring初学之bean之间的关系和bean的作用域
一.bean之间的关系 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="h ...
- Spring -- Bean自己主动装配&Bean之间关系&Bean的作用域
对于学习spring有帮助的站点:http://jinnianshilongnian.iteye.com/blog/1482071 Bean的自己主动装配 Spring IOC 容器能够自己主动装配 ...
- Jaxb 解析 带有继承关系的bean与xml
具体方法: 1. 在jaxb的setClasstobebounds中,只需要子类的class,无需父类. 2. 父类的前面加如下声明: @XmlAccessorType(XmlAccessType.F ...
- nobup 与 后台运行命令
1. Linux进程状态:R (TASK_RUNNING),可执行状态&运行状态(在run_queue队列里的状态) 2. Linux进程状态:S (TASK_INTERRUPTIBLE),可 ...
- Linux epoll 笔记(高并发事件处理机制)
wiki: Epoll优点: Epoll工作流程: Epoll实现机制: epollevent; Epoll源码分析: Epoll接口: epoll_create; epoll_ctl; epoll_ ...
- linux包之procps之ps与top
概述 阅读man ps页,与man top页,最权威与标准,也清楚 有时候系统管理员可能只关心现在系统中运行着哪些程序,而不想知道有哪些进程在运行.由于一个应用程序可能需要启动多个进程.所以在同等情况 ...
- [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 ...
随机推荐
- Eclipse配置PyDev插件来实现python开发环境
1.安装python解释器(完成的略过) 2.安装PyDev: 首先需要去Eclipse官网下载:http://www.eclipse.org/,Eclipse需要JDK支持,如果Eclipse无法正 ...
- 【BZOJ-4423】Bytehattan 并查集 + 平面图转对偶图
4423: [AMPPZ2013]Bytehattan Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 144 Solved: 103[Submit][ ...
- POJ2796 Feel Good 单调栈
题意:给定一个序列,需要找出某个子序列S使得Min(a[i])*Σa[i] (i属于S序列)最大 正解:单调栈 这题的暴力还是很好想的,只需3分钟的事就可以码完,以每个点拓展即可,但这样的复杂度是O( ...
- SQL Server配置管理器”远程过程调用失败“的问题解决
出现如下错误: 由于服务器上安装了SQLServer2008,然后再安装了VS2015 解决方案一: 由于安装VS2015会默认把[Microsoft SQL Server 2014 Express ...
- HDU4612 Warm up
Time Limit: 5000MS Memory Limit: 65535KB 64bit IO Format: %I64d & %I64u Description N planet ...
- php Unable to find the wrapper "https"
php -m | grep -i --color openssl php 没有openssl模块 cd /data/source/php-5.3.29/ext/openssl #php的解压包下面 y ...
- rsync服务器安装配置
#rsync指定端口号(10002)1 rsync -e 'ssh -p 10002' rsync (server -- client) #2014-3-3 -----------server---- ...
- Swift 函数做参数和闭包做参数的一个细节差别
函数作参数,示例为传入一个String和一个添加前缀的函数,返回一个添加完前缀的String: func demo(str:String,addPrefix:(String)->String)- ...
- FZU xxx游戏(拓扑排序+暴力)
xxx游戏 Time Limit: 1000MS Memory Limit: 32768 KB Description 小M最近很喜欢玩XXX游戏.这个游戏很简单,仅由3个场景(分别为1.2. ...
- [JavaEE] Entity中Lazy Load的属性序列化JSON时报错
The server encountered an internal error that prevented it from fulfilling this request.org.springfr ...