linux 进程优先级 之设置实时进程 (另一种方式是设置nice值)【转】
转自:https://www.cnblogs.com/jkred369/p/6731353.html
Linux内核的三种调度策略:
1,SCHED_OTHER 分时调度策略,
2,SCHED_FIFO实时调度策略,先到先服务。一旦占用cpu则一直运行。一直运行直到有更高优先级任务到达或自己放弃
首先,可以通过以下两个函数来获得线程可以设置的最高和最低优先级,函数中的策略即上述三种策略的宏定义:
int sched_get_priority_max(int policy);
int sched_get_priority_min(int policy);
SCHED_OTHER是不支持优先级使用的,而SCHED_FIFO和SCHED_RR支持优先级的使用,他们分别为1和99,数值越大优先级越高。
设置和获取优先级通过以下两个函数
|
系统创建线程时,默认的线程是SCHED_OTHER。所以如果我们要改变线程的调度策略的话,可以通过下面的这个函数实现。
|
上面的param使用了下面的这个数据结构:
|
我们可以通过下面的测试程序来说明,我们自己使用的系统的支持的优先级:
static int get_thread_policy(pthread_attr_t *attr) static void show_thread_priority(pthread_attr_t *attr,int policy) static int get_thread_priority(pthread_attr_t *attr) static void set_thread_policy(pthread_attr_t *attr,int policy) int main(void) int policy = get_thread_policy(&attr); printf("Set thread policy\n"); rs = pthread_attr_destroy(&attr); |
下面是测试程序的运行结果:
|
这里测试一下其中的两种特性,SCHED_OTHER和SCHED_RR,还有就是优先级的问题,是不是能够保证,高优先级的线程,就可以保证先运行。
下面的这个测试程序,创建了三个线程,默认创建的线程的调度策略是SCHED_OTHER,其余的两个线程的调度策略设置成SCHED_RR。我的Linux的内核版本是2.6.31。SCHED_RR是根据时间片来确定线程的调度。时间片用完了,不管这个线程的优先级有多高都不会在运行,而是进入就绪队列中,等待下一个时间片的到了,那这个时间片到底要持续多长时间?在《深入理解Linux内核》中的第七章进程调度中,是这样描诉的,Linux采取单凭经验的方法,即选择尽可能长、同时能保持良好相应时间的一个时间片。这里也没有给出一个具体的时间来,可能会根据不同的CPU
来定,还有就是多CPU 的情况。
void Thread1() for(i=1;i<10;i++) void Thread2() for(i=1;i<10;i++) void Thread3() for(i=1;i<10;i++) int main() pthread_t ppid1,ppid2,ppid3; pthread_attr_t attr,attr1,attr2; param.sched_priority = 21; |
下面是该程序的其中之一的运行结果:
|
这里我们可以看到,由于线程3的调度策略是SCHED_OTHER,而线程2的调度策略是SCHED_RR,所以,在Thread3中,线程3被线程1,线程2给抢占了。由于线程1的优先级大于线程2的优先级,所以,在线程1以先于线程2运行,不过,这里线程2有一部分代码还是先于线程1运行了。
我原以为,只要线程的优先级高,就会一定先运行,其实,这样的理解是片面的,特别是在SMP的PC机上更会增加其不确定性。
其实,普通进程的调度,是CPU根据进程优先级算出时间片,这样并不能一定保证高优先级的进程一定先运行,只不过和优先级低的进程相比,通常优先级较高的进程获得的CPU时间片会更长而已。其实,如果要想保证一个线程运行完在运行另一个线程的话,就要使用多线程的同步技术,信号量,条件变量等方法。而不是绝对依靠优先级的高低,来保证。
不过,从运行的结果上,我们可以看到,调度策略为SCHED_RR的线程1,线程2确实抢占了调度策略为SCHED_OTHER的线程3。这个是可以理解的,由于SCHER_RR是实时调度策略。
只有在下述事件之一发生时,实时进程才会被另外一个进程取代。
(1) 进程被另外一个具有更高实时优先级的实时进程抢占。
(2) 进程执行了阻塞操作并进入睡眠
(3)进程停止(处于TASK_STOPPED 或TASK_TRACED状态)或被杀死。
(4)进程通过调用系统调用sched_yield(),自愿放弃CPU 。
(5)进程基于时间片轮转的实时进程(SCHED_RR),而且用完了它的时间片。
基于时间片轮转的实时进程是,不是真正的改变进程的优先级,而是改变进程的基本时间片的长度。所以基于时间片轮转的进程调度,并不能保证高优先级的进程先运行。
下面是另一种运行结果:
|
可以看出并没有每一次都保证高优先级的线程先运行。
linux 进程优先级 之设置实时进程 (另一种方式是设置nice值)【转】的更多相关文章
- [操作系统知识储备,进程相关概念,开启进程的两种方式、 进程Queue介绍]
[操作系统知识储备,进程相关概念,开启进程的两种方式.进程Queue介绍] 操作系统知识回顾 为什么要有操作系统. 程序员无法把所有的硬件操作细节都了解到,管理这些硬件并且加以优化使用是非常繁琐的工作 ...
- python核心高级学习总结3-------python实现进程的三种方式及其区别
python实现进程的三种方式及其区别 在python中有三种方式用于实现进程 多进程中, 每个进程中所有数据( 包括全局变量) 都各有拥有⼀份, 互不影响 1.fork()方法 ret = os.f ...
- Linux经常使用的文件传输的几种方式
Linux经常使用的文件传输的几种方式 1.终端新建stfp协议连接 或者命令方式: sftp -P22 root@192.168.11.100 端口可以不用填写,默认是22,端口的P是大写. 将本地 ...
- HTML中设置背景图的两种方式
HTML中设置背景图的两种方式 1.background background:url(images/search.png) no-repeat top; 2.background-image ...
- Day9 进程理论 开启进程的两种方式 多进程实现并发套接字 join方法 Process对象的其他属性或者方法 守护进程 操作系统介绍
操作系统简介(转自林海峰老师博客介绍) #一 操作系统的作用: 1:隐藏丑陋复杂的硬件接口,提供良好的抽象接口 2:管理.调度进程,并且将多个进程对硬件的竞争变得有序 #二 多道技术: 1.产生背景: ...
- Latex中如何设置字体颜色(3种方式)
Latex中如何设置字体颜色(三种方式) 1.直接使用定义好的颜色 \usepackage{color} \textcolor{red/blue/green/black/white/cyan/ma ...
- ps | grep app 命令不显示grep app本身进程的几种方式
ps | grep app 命令不显示grep app本身进程的几种方式 使用ps命令查询进程,常常我们不想打印出"ps | grep app"这个当前进程,比如如下: [root ...
- 设置session超时的三种方式
设置session超时的三种方式 1. 在容器中设置:如在tomcat-7\conf\web.xml中设置 Tomcat默认session超时时间为30分钟,可以根据需要修改,负数或0为不限制sess ...
- delphi杀进程的两种方式
delphi杀进程的两种方式 uint unit Tlhelp32; 第一种:比较简单,根据标题,找到窗口,再找到进程,杀死进程 procedure KillProgram(WindowTitle : ...
- 设置npm源的几种方式
设置npm源的几种方式 原始源 # the original source https://registry.npmjs.org/ 方案: 使用nrm 安装 npm install -g nrm 列出 ...
随机推荐
- 第123天:移动web开发中的常见问题
一.函数库 underscoreJS _.template: <ol class="carousel-indicators"> <!--渲染的HTML字符串--& ...
- Moya/RxSwift/ObjectMapper/Alamofire开发
废话不多说直接上代码 // // MoyaNetWorking.swift // GreenAir // // Created by BruceAlbert on 2017/9/18. // Copy ...
- android面试(2)----组件
1.anroid:id的作用? android:id是作为控件的唯一标示符.可以使用与releativelayout中,也可以再Activity中通过findviewbyid来获得指定的控件. 2.a ...
- C++解析(10):struct和class的区别
0.目录 1.默认访问级别 2.默认继承方式 2.1 分别独立继承 2.2 struct继承class 2.3 class继承struct 3.小结 1.默认访问级别 在用struct定义类时,所有成 ...
- 【51Nod1258】序列求和V4(FFT)
[51Nod1258]序列求和V4(FFT) 题面 51Nod 多组数据,求: \[Ans=\sum_{i=1}^ni^k,n\le 10^{18},k\le50000\] 题解 预处理伯努利数,时间 ...
- HDU.1846 Brave Game (博弈论 巴什博弈)
HDU.1846 Brave Game (博弈论 巴什博弈) 题意分析 巴什博奕裸题 博弈论快速入门 代码总览 include <bits/stdc++.h> using namespac ...
- [学习笔记]Min-25筛
%%yyb %%zsy 一. 基本操作:筛1~N中的素数个数.n=1e9 设F(M,j)表示,2~M的所有数中,满足以下条件之一的数的个数:①x是质数②x最小质因子大于(注意是大于没有等号)$P_j$ ...
- android ndk 编译的时候指令集的选取
android ndk在编译的时候默认生成的是thumb指令(拇指)不是arm(手臂)指令,但是有时候在看反汇编的时候,不太熟悉thumb指令或者说thumb指令看起来更费劲,需要生成arm指令,这个 ...
- 洛谷P2398 GCD SUM (数学)
洛谷P2398 GCD SUM 题目描述 for i=1 to n for j=1 to n sum+=gcd(i,j) 给出n求sum. gcd(x,y)表示x,y的最大公约数. 输入输出格式 输入 ...
- swagger2的常用注解,传递参数的注意使用方法
背景介绍: 刚开始的时候,在controller层使用@RequestParam的时候,发现这个参数是必须要输入值的,但是我们有时候必须查询的时候允许参数为空,使用这个注解就不行了. 在集成了swag ...