转载:https://www.cnblogs.com/tongye/p/9615625.html

kthread_create 创建线程的时候是默认的优先级默认的RT线程rt_prio是0,rt_prio范围是0-99

调整线程优先级的函数sched_setschduler 可以调整线程优先级,因为GPL的原因会有必要绕到user space调用的userspace的sched_setscheduler 函数来调整线程优先级

传参5 调整后结果  [static_prio 120]  [normal_prio 5]  [prio:5]  [rt_prio 94] 对于RT thread 只关注rt_prio即可

(ps  -el 是查看不到  rt_prio的 以上结果用debug tool看到的)

/*** sched_setscheduler - change the scheduling policy and/or RT priority of a thread.* @p: the task in question.* @policy: new policy./*** sched_setscheduler - change the scheduling policy and/or RT priority of a thread.* @p: the task in question.* @policy: new policy.* @param: structure containing the new RT priority.** Return: 0 on success. An error code otherwise.** NOTE that the task may be already dead.*/
int sched_setscheduler(struct task_struct *p, int policy,const struct sched_param *param)
{return _sched_setscheduler(p, policy, param, true);
}
EXPORT_SYMBOL_GPL(sched_setscheduler);
       #include <sched.h>int sched_setscheduler(pid_t pid, int policy,const struct sched_param *param);The sched_setscheduler() system call sets both the scheduling policyand parameters for the thread whose ID is specified in pid.  If pidequals zero, the scheduling policy and parameters of the callingthread will be set.

_______________________________________________________________________________________________

Linux 中采用了两种不同的优先级范围,一种是 nice 值,一种是实时优先级。在上一篇粗略的说了一下 nice 值和实时优先级,仍有不少疑问,本文来详细说明一下进程优先级。linux 内核版本为 linux 2.6.34 。

  进程优先级的相关信息,存放在进程描述符 task_struct 中:

struct task_struct {...int prio, static_prio, normal_prio;unsigned int rt_priority;...
}

  可以看到,有四种进程优先级: prio、static_prio、normal_prio 和 rt_priority,它们的具体定义在 kernel/sched.c 中,在介绍这四种优先级之前,先介绍一下以下宏定义:

/* linux-kernel 2.6.34 /include/linux/sched.h *//** Priority of a process goes from 0..MAX_PRIO-1, valid RT* priority is 0..MAX_RT_PRIO-1, and SCHED_NORMAL/SCHED_BATCH* tasks are in the range MAX_RT_PRIO..MAX_PRIO-1. Priority* values are inverted: lower p->prio value means higher priority.** The MAX_USER_RT_PRIO value allows the actual maximum* RT priority to be separate from the value exported to* user-space.  This allows kernel threads to set their* priority to a value higher than any user task. Note:* MAX_RT_PRIO must not be smaller than MAX_USER_RT_PRIO.*/#define MAX_USER_RT_PRIO     100
#define MAX_RT_PRIO          MAX_USER_RT_PRIO#define MAX_PRIO            (MAX_RT_PRIO + 40)
#define DEFAULT_PRIO        (MAX_RT_PRIO + 20)    // 默认优先级,对应 nice 值为 0 的静态优先级

1、prio 动态优先级

  prio 的值是调度器最终使用的优先级数值,即调度器选择一个进程时实际选择的值。prio 值越小,表明进程的优先级越高。prio  值的取值范围是 0 ~ MAX_PRIO,即 0 ~ 139(包括 0 和 139),根据调度策略的不同,又可以分为两个区间,其中区间 0 ~ 99 的属于实时进程,区间 100 ~139 的为非实时进程。用语言不好描述,我们通过内核代码来详细描述 prio:

/* linux-kernel 2.6.34  /kernel/sched.c  */#include "sched_idletask.c"
#include "sched_fair.c"
#include "sched_rt.c"
#ifdef CONFIG_SCHED_DEBUG
#include "sched_debug.c"
#endif/** __normal_prio - return the priority that is based on the static prio*/
static inline int __normal_prio(struct task_struct *p)    // _normal_prio 函数,返回静态优先级值
{return p->static_prio;
}/** Calculate the expected normal priority: i.e. priority* without taking RT-inheritance into account. Might be* boosted by interactivity modifiers. Changes upon fork,* setprio syscalls, and whenever the interactivity* estimator recalculates.*/
static inline int normal_prio(struct task_struct *p)    // normal_prio 函数
{int prio;if (task_has_rt_policy(p))                 // task_has_rt_policy 函数,判断进程是否为实时进程,若为实时进程,则返回1,否则返回0prio = MAX_RT_PRIO-1 - p->rt_priority;        // 进程为实时进程,prio 值为实时优先级值做相关运算得到: prio = MAX_RT_PRIO -1 - p->rt_priorityelseprio = __normal_prio(p);                // 进程为非实时进程,则 prio 值为静态优先级值,即 prio = p->static_prioreturn prio;
}/** Calculate the current priority, i.e. the priority* taken into account by the scheduler. This value might* be boosted by RT tasks, or might be boosted by* interactivity modifiers. Will be RT if the task got* RT-boosted. If not then it returns p->normal_prio.*/
static int effective_prio(struct task_struct *p)       // effective_prio 函数,计算进程的有效优先级,即prio值,这个值是最终调度器所使用的优先级值
{p->normal_prio = normal_prio(p);              // 计算 normal_prio 的值/** If we are RT tasks or we were boosted to RT priority,* keep the priority unchanged. Otherwise, update priority* to the normal priority:*/if (!rt_prio(p->prio))return p->normal_prio;                  // 若进程是非实时进程,则返回 normal_prio 值,这时的 normal_prio = static_prioreturn p->prio;                         // 否则,返回值不变,依然为 prio 值,此时 prio = MAX_RT_PRIO -1 - p->rt_priority
} /*********************** 函数 set_user_nice ****************************************/
void set_user_nice(struct task_struct *p, long nice)
{....p->prio = effective_prio(p);                   // 在函数 set_user_nice 中,调用 effective_prio 函数来设置进程的 prio 值....
}

  从上面代码中我们知道,当进程为实时进程时, prio 的值由实时优先级值(rt_priority)计算得来;当进程为非实时进程时,prio 的值由静态优先级值(static_prio)得来。即:

prio = MAX_RT_PRIO - 1 - rt_priority    // 进程为实时进程

prio = static_prio          // 进程为非实时进程

  简单计算上面的两个式子,可以知道,prio 值的范围是 0 ~ 139 。

2、static_prio 静态优先级

  静态优先级不会随时间改变,内核不会主动修改它,只能通过系统调用 nice 去修改 static_prio,如下:

/** Convert user-nice values [ -20 ... 0 ... 19 ]* to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],* and back.*/
#define NICE_TO_PRIO(nice)    (MAX_RT_PRIO + (nice) + 20)
#define PRIO_TO_NICE(prio)    ((prio) - MAX_RT_PRIO - 20)
#define TASK_NICE(p)        PRIO_TO_NICE((p)->static_prio)/** 'User priority' is the nice value converted to something we* can work with better when scaling various scheduler parameters,* it's a [ 0 ... 39 ] range.*/
#define USER_PRIO(p)        ((p)-MAX_RT_PRIO)
#define TASK_USER_PRIO(p)    USER_PRIO((p)->static_prio)
#define MAX_USER_PRIO        (USER_PRIO(MAX_PRIO))/********************* 函数 set_user_nice *****************************/
p->static_prio = NICE_TO_PRIO(nice);        // 当有需要时,系统会通过调用 NICE_TO_PRIO() 来修改 static_prio 的值

  由上面代码知道,我们可以通过调用 NICE_TO_PRIO(nice) 来修改 static_prio  的值, static_prio 值的计算方法如下:

static_prio = MAX_RT_PRIO + nice +20

  MAX_RT_PRIO 的值为100,nice 的范围是 -20 ~ +19,故 static_prio 值的范围是 100 ~ 139。 static_prio 的值越小,表明进程的静态优先级越高

3、normal_prio 归一化优先级

  normal_prio 的值取决于静态优先级和调度策略,可以通过 _setscheduler 函数来设置 normal_prio 的值 。对于非实时进程,normal_prio 的值就等于静态优先级值 static_prio;对于实时进程,normal_prio = MAX_RT_PRIO-1 - p->rt_priority。代码如下:

static inline int normal_prio(struct task_struct *p)    // normal_prio 函数
{int prio;if (task_has_rt_policy(p))                 // task_has_rt_policy 函数,判断进程是否为实时进程,若为实时进程,则返回1,否则返回0prio = MAX_RT_PRIO-1 - p->rt_priority;        // 进程为实时进程,prio 值为实时优先级值做相关运算得到: prio = MAX_RT_PRIO -1 - p->rt_priorityelseprio = __normal_prio(p);                // 进程为非实时进程,则 prio 值为静态优先级值,即 prio = p->static_prioreturn prio;
}

linux 线程优先级 rt_prio static_prio prio normal_prio相关推荐

  1. linux降低线程优先级,Linux线程优先级,行为不正常

    在下面的代码片段中,我创建了6个线程.各有不同的优先级.全局优先级数组中提到了优先级.我正在根据线索索引在每个线程内连续增加全局变量.如果线程优先级更高,我期待计数更高.但我的输出不遵循优先概念pl. ...

  2. Linux 线程优先级设置(内含C语言版线程创建、绑定CPU和优先级设置代码)

    参考链接: https://blog.csdn.net/wushuomin/article/details/80051295 //详细讲解pthread_create 函数 https://blog. ...

  3. Linux线程优先级范围

    Linux定义线程优先级范围在头文件<linux/sched.h> /** Priority of a process goes from 0..MAX_PRIO-1, valid RT* ...

  4. linux 线程优先级的高低和执行顺序的关系,混乱的Linux内核实时线程优先级

    原标题:混乱的Linux内核实时线程优先级 背景 Linux会把进程分为普通进程和实时进程,普通进程采用CFS之类调度算法,而实时进程则是采用SCHED_FIFO或SCHED_RR. 无论优先级高低, ...

  5. linux 线程优先级算法,能讲一下在Linux系统中时间片是怎么分配的还有优先级的具体算法是...

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 图 1 RT-Linux结构 RT -Linux的关键技术是通过软件来模拟硬件的中断控制器.当Linux系统要封锁CPU的中断时时,RT-Linux中的实 ...

  6. linux线程优先级和nice值,如何使用nice和renice命令设置Linux进程优先级

    在本文中,我们将简要介绍内核调度程序 (也称为进程调度程序 )和进程优先级 ,这些主题超出了本指南的范围. 然后,我们将深入了解一些Linux进程管理 :了解如何运行具有修改优先级的程序或命令,还可以 ...

  7. linux线程调度函数,Linux调度策略及线程优先级设置

    Linux内核的三种调度策略: 1,SCHED_OTHER 分时调度策略, 2,SCHED_FIFO实时调度策略,先到先服务.一旦占用cpu则一直运行.一直运行直到有更高优先级任务到达或自己放弃 3, ...

  8. Android 中设置线程优先级的正确方式(2种方法)

    Android 中设置线程优先级的正确方式(2种方法) 在 Android 中,有两种常见的设置线程优先级的方式: 第一种,使用 Thread 类实例的 setPriority 方法,来设置线程优先级 ...

  9. c语言怎么设置cpu优先级,线程优先级,设置,setPriority()方法

    package seday08.thread; /** * @author xingsir * 线程优先级 * 线程启动后纳入到线程调度,线程时刻处于被动获取CPU时间片而无法主动获取.我们可以通过调 ...

最新文章

  1. mysql 主从热备_windows10本地两个mysql8服务配置主从热备
  2. dplyr和data.table让你的数据分析事半功倍
  3. [网络安全提高篇] 一〇九.津门杯CTF的Web Write-Up万字详解(SSRF、文件上传、SQL注入、代码审计、中国蚁剑)
  4. 皮尔逊相关性_皮尔逊的相关性及其在机器学习中的意义
  5. linux ls命令无法执行,更新了个依赖程序,结果悲剧了,连ls命令都不能用,大神帮帮忙!...
  6. 李白打酒java_蓝桥杯-李白打酒-java
  7. C语言指针详解(经典,非常详细)
  8. 自动化测试框架cucumber_10分钟学会 Cucumber+Watir 自动化测试框架
  9. win7用ip查找网络计算机,win7通过ip地址查找计算机名的两种方法(图文教程)
  10. Android仿虾米音乐播放器之布局介绍
  11. Android_GitHub_xUtils之DbUtils、ViewUtils、HttpUtils、BitmapUtils
  12. 《东周列国志》第七十五回 孙武子演阵斩美姬 蔡昭侯纳质乞吴师
  13. 在vue中渲染数学公式 - MathJax
  14. 用matlab画玫瑰花函数,网上收到的用matlab画玫瑰花的代码怎么不行啊,报告错误,求大神...
  15. oracle备份数据库dmp定时,SCO Unix系统下定时备份Oracle数据库dmp文件的设置
  16. 课程设计:通讯录系统(数据库)
  17. 安装manjaro-i3 conky 乱码问题
  18. 2021年建5G基站60万个;中兴遭减持;三大运营商2020年成绩单;电信发布新手机...
  19. DPDK-IP分片和重组库
  20. python登录系统账号检测_一种基于python的惠普打印机默认用户名密码检测方法与流程...

热门文章

  1. Python:实现pancake sort煎饼排序算法(附完整源码)
  2. 兼容ie的多行文本溢出隐藏,并显示省略号
  3. foreach遍历list删除元素一定会报错?
  4. LeetCode 2353. 设计食物评分系统 维护哈希表+set
  5. 关于freenas 安装frp 设置
  6. 凸包 (Convex Hull)
  7. 《气候宣言》和We Mean Business联盟合作推出全方位行动,助力企业推行更具雄心的碳减排目标
  8. ppt如何转换pdf格式
  9. 组织行为学--名词解释
  10. 国防科大计算机学院导师推荐,我的导师