【转】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 ...
随机推荐
- 和ef一起使用的一些知识点。
ObjectContext.ExecuteFunction 方法 (String, ObjectParameter[]) .NET Framework 4.6 and 4.5 执行在数据源中定义 ...
- 【BZOJ-1046】上升序列 DP + 贪心
1046: [HAOI2007]上升序列 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3723 Solved: 1271[Submit][Stat ...
- DataTable是否存在某个列的判断
使用 DataTable.Columns.Contains方法可以判断某个列名是否存在于某个DataTable中 //添加模拟数据 DataTable t = new DataTable(); Dat ...
- poj1379 模拟退火
题意:和上题一样...就是把最小值换成了最大值.. ref:http://www.cppblog.com/RyanWang/archive/2010/01/21/106112.html #includ ...
- ecshop /flow.php SQL Injection Vul
catalog . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 ECSHOP的配送地址页面网页没有验证地区参数的有效性,存在sql注入漏 ...
- Linux C/C++ Memory Leak Detection Tool
目录 . 内存使用情况分析 . 内存泄漏(memory leak) . Valgrind使用 1. 内存使用情况分析 0x1: 系统总内存的分析 可以从proc目录下的meminfo文件了解到当前系统 ...
- koch曲线与koch雪花的MATLAB实现
代码 % -- function koch(Ax, Ay, Bx, By) % 控制递归深度 Deepth = ; % 控制图画大小 Size = ; + (By-Ay)^) < Deepth ...
- Objective-C 谈谈深浅拷贝,copy和mutable copy都不是完全拷贝
(一)字符串中的指针赋值,copy和mutablecopy NSString和NSString (1)指针赋值 肯定指向同一个字符串地址. (2)copy(和直接指向一样) NSString *str ...
- Objective-C 再谈OC指针,对比C++/Java/Swift
1.Objective-C的指针 OC一直是人感觉比较变态的一门语言,为什么呢?因为它的每个变量都是指针型,多的都几乎让人忘了那个*的存在了. 比如我定义了一个Student的Class,new了st ...
- python面相对象进阶
1. 类的成员 python 类的成员有三种:字段.方法.属性 字段 字段包括:普通字段和静态字段,他们在定义和使用中有所区别,而最本质的区别是内存中保存的位置不同, 普通字段 属于对象,只有对象创建 ...