已阅读5页,还剩8页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Linux内核的三种调度策略:1,SCHED_OTHER 分时调度策略,2,SCHED_FIFO实时调度策略,先到先服务。一旦占用cpu则一直运行。一直运行直到有更高优先级任务到达或自己放弃 3,SCHED_RR实时调度策略,时间片轮转。当进程的时间片用完,系统将重新分配时间片,并置于就绪队列尾。放在队列尾保证了所有具有相同优先级的RR任务的调度公平Linux线程优先级设置 首先,可以通过以下两个函数来获得线程可以设置的最高和最低优先级,函数中的策略即上述三种策略的宏定义:int sched_get_priority_max(int policy);int sched_get_priority_min(int policy); SCHED_OTHER是不支持优先级使用的,而SCHED_FIFO和SCHED_RR支持优先级的使用,他们分别为1和99,数值越大优先级越高。设置和获取优先级通过以下两个函数int pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param);int pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param); param.sched_priority = 51; /设置优先级 系统创建线程时,默认的线程是SCHED_OTHER。所以如果我们要改变线程的调度策略的话,可以通过下面的这个函数实现。int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);上面的param使用了下面的这个数据结构:struct sched_param int _sched_priority; /所要设定的线程优先级;我们可以通过下面的测试程序来说明,我们自己使用的系统的支持的优先级:#include #include #include #include static int get_thread_policy(pthread_attr_t *attr) int policy; int rs = pthread_attr_getschedpolicy(attr,&policy); assert(rs=0); switch(policy) case SCHED_FIFO: printf(policy= SCHED_FIFOn); break; case SCHED_RR: printf(policy= SCHED_RR); break; case SCHED_OTHER: printf(policy=SCHED_OTHERn); break; default: printf(policy=UNKNOWNn); break; return policy;static void show_thread_priority(pthread_attr_t *attr,int policy) int priority = sched_get_priority_max(policy); assert(priority!=-1); printf(max_priority=%dn,priority); priority= sched_get_priority_min(policy); assert(priority!=-1); printf(min_priority=%dn,priority);static int get_thread_priority(pthread_attr_t *attr) struct sched_param param; int rs = pthread_attr_getschedparam(attr,¶m); assert(rs=0); printf(priority=%d,param._sched_priority); return param._sched_priority;static void set_thread_policy(pthread_attr_t *attr,int policy) int rs = pthread_attr_setschedpolicy(attr,policy); assert(rs=0); get_thread_policy(attr);int main(void) pthread_attr_t attr; struct sched_param sched; int rs; rs = pthread_attr_init(&attr); assert(rs=0); int policy = get_thread_policy(&attr); printf(Show current configuration of priorityn); show_thread_priority(&attr,policy); printf(show SCHED_FIFO of priorityn); show_thread_priority(&attr,SCHED_FIFO); printf(show SCHED_RR of priorityn); show_thread_priority(&attr,SCHED_RR); printf(show priority of current threadn); int priority = get_thread_priority(&attr); printf(Set thread policyn); printf(set SCHED_FIFO policyn); set_thread_policy(&attr,SCHED_FIFO); printf(set SCHED_RR policyn); set_thread_policy(&attr,SCHED_RR); printf(Restore current policyn); set_thread_policy(&attr,policy); rs = pthread_attr_destroy(&attr); assert(rs=0); return 0;下面是测试程序的运行结果:policy=SCHED_OTHERShow current configuration of prioritymax_priority=0min_priority=0show SCHED_FIFO of prioritymax_priority=99min_priority=1show SCHED_RR of prioritymax_priority=99min_priority=1show priority of current threadpriority=0Set thread policyset SCHED_FIFO policypolicy= SCHED_FIFOset SCHED_RR policypolicy= SCHED_RRRestore current policypolicy=SCHED_OTHERLinux线程调度策略与优先级(二) 上一篇文章介绍了Linux下的调度策略和优先级,在Ubuntu09.10上的一些特性,这里测试一下其中的两种特性,SCHED_OTHER和SCHED_RR,还有就是优先级的问题,是不是能够保证,高优先级的线程,就可以保证先运行。 下面的这个测试程序,创建了三个线程,默认创建的线程的调度策略是SCHED_OTHER,其余的两个线程的调度策略设置成SCHED_RR。我的Linux的内核版本是2.6.31。SCHED_RR是根据时间片来确定线程的调度。时间片用完了,不管这个线程的优先级有多高都不会在运行,而是进入就绪队列中,等待下一个时间片的到了,那这个时间片到底要持续多长时间?在深入理解Linux内核中的第七章进程调度中,是这样描诉的,Linux采取单凭经验的方法,即选择尽可能长、同时能保持良好相应时间的一个时间片。这里也没有给出一个具体的时间来,可能会根据不同的CPU 来定,还有就是多CPU 的情况。#include #include #include #include void Thread1() sleep(1); int i,j; int policy; struct sched_param param; pthread_getschedparam(pthread_self(),&policy,¶m); if(policy = SCHED_OTHER) printf(SCHED_OTHERn); if(policy = SCHED_RR); printf(SCHED_RR 1 n); if(policy=SCHED_FIFO) printf(SCHED_FIFOn); for(i=1;i10;i+) for(j=1;j5000000;j+) printf(thread 1n); printf(Pthread 1 exitn);void Thread2() sleep(1); int i,j,m; int policy; struct sched_param param;pthread_getschedparam(pthread_self(),&policy,¶m); if(policy = SCHED_OTHER) printf(SCHED_OTHERn); if(policy = SCHED_RR); printf(SCHED_RRn); if(policy=SCHED_FIFO) printf(SCHED_FIFOn); for(i=1;i10;i+) for(j=1;j5000000;j+) printf(thread 2n); printf(Pthread 2 exitn);void Thread3() sleep(1); int i,j; int policy; struct sched_param param;pthread_getschedparam(pthread_self(),&policy,¶m); if(policy = SCHED_OTHER) printf(SCHED_OTHERn); if(policy = SCHED_RR) printf(SCHED_RR n); if(policy=SCHED_FIFO) printf(SCHED_FIFOn); for(i=1;i10;i+) for(j=1;j5000000;j+) printf(thread 3n); printf(Pthread 3 exitn);int main() int i; i = getuid(); if(i=0) printf(The current user is rootn); else printf(The current user is not rootn); pthread_t ppid1,ppid2,ppid3; struct sched_param param; pthread_attr_t attr,attr1,attr2; pthread_attr_init(&attr1);pthread_attr_init(&attr);pthread_attr_init(&attr2); param.sched_priority = 51; pthread_attr_setschedpolicy(&attr2,SCHED_RR); pthread_attr_setschedparam(&attr2,¶m); pthread_attr_setinheritsched(&attr2,PTHREAD_EXPLICIT_SCHED);/要使优先级其作用必须要有这句话 param.sched_priority = 21; pthread_attr_setschedpolicy(&attr1,SCHED_RR); pthread_attr_setschedparam(&attr1,¶m); pthread_attr_setinheritsched(&attr1,PTHREAD_EXPLICIT_SCHED); pthread_create(&ppid3,&attr,(void *)Thread3,NULL); pthread_create(&ppid2,&attr1,(void *)Thread2,NULL); pthread_create(&ppid1,&attr2,(void *)Thread1,NULL); pthread_join(ppid3,NULL); pthread_join(ppid2,NULL); pthread_join(ppid1,NULL); pthread_attr_destroy(&attr2); pthread_attr_destroy(&attr1); return 0;下面是该程序的其中之一的运行结果:sudo ./prio_testThe current user is rootSCHED_OTHERSCHED_RRSCHED_RR 1thread 1thread 1thread 1thread 1thread 1thread 1thread 1thread 1thread 1Pthread 1 exitthread 2thread 2thread 2thread 2thread 2thread 2thread 2thread 2thread 2Pthread 2 exitthread 3thread 3thread 3thread 3thread 3thread 3thread 3thread 3thread 3Pthread 3 exit 这里我们可以看到,由于线程3的调度策略是SCHED_OTHER,而线程2的调度策略是SCHED_RR,所以,在Thread3中,线程3被线程1,线程2给抢占了。由于线程1的优先级大于线程2的优先级,所以,在线程1以先于线程2运行,不过,这里线程2有一部分代码还是先于线程1运行了。 我原以为,只要线程的优先级高,就会一定先运行,其实,这样的理解是片面的,特别是在SMP的PC机上更会增加其不确定性。 其实,普通进程的调度,是CPU根据进程优先级算出时间片,这样并不能一定保证高优先级的进程一定先运行,只不过和优先级低的进程相比,通常优先级较高的进程获得的CPU时间片会更长而已。其实,如果要想保证一个线程运行完在运行另一个线程的话,就要使用多线程的同步技术,信号量,条件变量等方法。而不是绝对依靠优先级的高低,来保证。 不过,从运行的结果上,我们可以看到,调度策略为SCHED_RR的线程1,线程2确实抢占了
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 小学五年级劳动教育教学设计:旧筷新造防烫锅垫
- 高中语文高一上册第五单元文学阅读与写作指导教学设计
- 17.2 生物的进化第1课时 初中八年级生物教学设计
- 小学六年级劳动教学设计-毕业纪念册的策划与制作
- 财会岗运维岗事业单位面试经典考题试卷及解析
- 核算岗 往期考题题库及答案
- 核算岗事业单位面试岗位真题题库及答案
- 2027年阜阳市事业单位招聘考试上半年判断推理模拟试卷及答案
- 小学生必背75首古诗模拟试题及答案
- 2026年广州市海珠区素社街道雇员招聘考试考试模拟试题(含答案)
- 职工代表选举流程规范及表格模板
- 华能玉环电厂4×1000MW海水淡化及补给水除盐系统运行规程
- 2026年广东东莞中小学教师招聘考试真题解析含答案
- 工作坊组织流程及实践操作指南
- 汽车寄售合同范本2026
- 药房员工培训合同协议书2025年版
- 合同付款方变更协议书
- 小学德育工作亮点汇报
- 儿童营养包知识培训课件
- 基于“教-学-评”一致性的初中生物单元作业设计
- 土方外运处理协议合同
评论
0/150
提交评论