Linux内核的三种调度策略

SCHED_OTHER

分时调度策略。

它是默认的线程分时调度策略,所有的线程的优先级别都是0,线程的调度是通过分时来完成的。简单地说,如果系统使用这种调度策略,程序将无法设置线程的优先级。请注意,这种调度策略也是抢占式的,当高优先级的线程准备运行的时候,当前线程将被抢占并进入等待队列。这种调度策略仅仅决定线程在可运行线程队列中的具有相同优先级的线程的运行次序。

SCHED_FIFO

实时调度策略,

先到先服务。一旦占用cpu则一直运行。一直运行直到有更高优先级任务到达或自己放弃。

它是一种实时的先进先出调用策略,且只能在超级用户下运行。这种调用策略仅仅被使用于优先级大于0的线程。它意味着,使用SCHED_FIFO的可运行线程将一直抢占使用SCHED_OTHER的运行线程J。此外SCHED_FIFO是一个非分时的简单调度策略,当一个线程变成可运行状态,它将被追加到对应优先级队列的尾部。当所有高优先级的线程终止或者阻塞时,它将被运行。对于相同优先级别的线程,按照简单的先进先运行的规则运行。我们考虑一种很坏的情况,如果有若干相同优先级的线程等待执行,然而最早执行的线程无终止或者阻塞动作,那么其他线程是无法执行的,除非当前线程调用如pthread_yield之类的函数,所以在使用SCHED_FIFO的时候要小心处理相同级别线程的动作。

SCHED_RR

实时调度策略,时间片轮转。

当进程的时间片用完,系统将重新分配时间片,并置于就绪队列尾。放在队列尾保证了所有具有相同优先级的RR任务的调度公平。

鉴于SCHED_FIFO调度策略的一些缺点,SCHED_RR对SCHED_FIFO做出了一些增强功能。从实质上看,它还是SCHED_FIFO调用策略。它使用最大运行时间来限制当前进程的运行,当运行时间大于等于最大运行时间的时候,当前线程将被切换并放置于相同优先级队列的最后。这样做的好处是其他具有相同级别的线程能在“自私“线程下执行。

系统创建线程时,默认的线程是SCHED_OTHER。所以如果我们要改变线程的调度策略的话,可以通过下面的这个函数实现:

int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);

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使用了下面的这个数据结构:

struct sched_param
{
int __sched_priority; //所要设定的线程优先级
};

例如:

param.sched_priority = 51; //设置优先级

测试程序

#include <stdio.h>
#include <pthread.h>
#include <sched.h>
#include <assert.h> //打印当前的线程调度策略
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_FIFO\n");
break;
case SCHED_RR:
printf("policy= SCHED_RR\n");
break;
case SCHED_OTHER:
printf("policy=SCHED_OTHER\n");
break;
default:
printf("policy=UNKNOWN\n");
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=%d\n",priority);
priority= sched_get_priority_min(policy);
assert(priority!=-1);
printf("min_priority=%d\n",priority);
} //打印当前线程的优先级
static int get_thread_priority(pthread_attr_t *attr)
{
struct sched_param param;
int rs = pthread_attr_getschedparam(attr,&param);
assert(rs==0);
printf("priority=%d\n",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); //获取默认的线程调度策略
printf("== Show current configuration of priority ==\n");
int policy = get_thread_policy(&attr);
show_thread_priority(&attr,policy);
//打印当前线程的优先级
printf("show priority of current thread\n");
int priority = get_thread_priority(&attr); printf("== Show different Scheduling method priority ==\n");
//获取SCHED_FIFO调度策略的最高和最低优先级
printf("show SCHED_FIFO of priority:\n");
show_thread_priority(&attr,SCHED_FIFO); //获取SCHED_RR调度策略的最高和最低优先级
printf("show SCHED_RR of priority:\n");
show_thread_priority(&attr,SCHED_RR); printf("== Set thread policy == \n");
//设置线程的调度属性为SCHED_FIFO
printf("set SCHED_FIFO policy:\n");
set_thread_policy(&attr,SCHED_FIFO);
priority = get_thread_priority(&attr);
sched.__sched_priority = 99;
pthread_attr_setschedparam(&attr,&sched);
priority = get_thread_priority(&attr); //设置线程的调度属性为SCHED_FIFO
printf("set SCHED_RR policy:\n");
set_thread_policy(&attr,SCHED_RR);
priority = get_thread_priority(&attr);
sched.__sched_priority = 55;
pthread_attr_setschedparam(&attr,&sched);
priority = get_thread_priority(&attr); //恢复线程的调度策略
printf("Restore current policy:\n");
set_thread_policy(&attr,policy); rs = pthread_attr_destroy(&attr);
assert(rs==0);
return 0;
}

Linux 线程调度策略与线程优先级的更多相关文章

  1. Linux多线程实践(3) --线程属性

    初始化/销毁线程属性 int pthread_attr_init(pthread_attr_t *attr); int pthread_attr_destroy(pthread_attr_t *att ...

  2. inux进程/线程调度策略与 进程优先级

    目的: 系统性的认识linux的调度策略(SCHED_OTHER.SCHED_FIFO.SCHED_RR): 实时调度?分时调度? 混搭系统(实时任务+分时任务),怎样调度. linux的调度策略 l ...

  3. Linux C多线程编程-线程互斥

    Linux下的多线程编程需要注意的是程序需要包含头文件pthread.h,在生成可执行文件的时候需要链接库libpthread.a或者libpthread.so. 线程创建函数: pthread_cr ...

  4. Linux学习笔记22——线程属性(转)

    本文来自博客园:http://www.cnblogs.com/yc_sunniwell/archive/2010/06/24/1764204.html 一.线程属性线程具有属性,用pthread_at ...

  5. 在Linux下写一个线程池以及线程池的一些用法和注意点

    -->线程池介绍(大部分来自网络)  在这个部分,详细的介绍一下线程池的作用以及它的技术背景以及他提供的一些服务等.大部分内容来自我日常生活中在网络中学习到的一些概念性的东西. -->代码 ...

  6. Linux多线程实践(1) --线程理论

    线程概念 在一个程序里的一个执行路线就叫做线程(thread).更准确的定义是:线程是"一个进程内部的控制序列/指令序列"; 一切进程至少有一个执行线程; 进程  VS. 线程  ...

  7. Linux中进程与线程的概念以及区别

    linux进程与线程的区别,早已成为IT界经常讨论但热度不减的话题.无论你是初级程序员,还是资深专家,都应该考虑过这个问题,只是层次角度不同罢了.对于一般的程序员,搞清楚二者的概念并在工作中学会运用是 ...

  8. linux查看进程的线程数

    top -H -p $PID  #查看对应进程的那个线程占用CPU过高 1.top -H 手册中说:-H : Threads toggle 加上这个选项启动top,top一行显示一个线程.否则,它一行 ...

  9. 一只简单的网络爬虫(基于linux C/C++)————线程相关

    爬虫里面采用了多线程的方式处理多个任务,以便支持并发的处理,把主函数那边算一个线程的话,加上一个DNS解析的线程,以及我们可以设置的max_job_num值,最多使用了1+1+max_job_num个 ...

随机推荐

  1. Qt Customize QVariant

    Customize QVariant #include <QCoreApplication> #include <QVariant> #include <QDebug&g ...

  2. Vue 路由缓存

    问题 在路由切换时不需要每次 点击都刷新子路由   尤其是在form表单的情况下  不能让用户 输入一半之后点击其他页面  再点回来 表单数据不见了 解决方案   vue 2.0     之中  有k ...

  3. CoreImage

    [CoreImage] CIContext is an object through which Core Image draws the results produced by a filter. ...

  4. 8 种提升 ASP.NET Web API 性能的方法 (转)

    出处:http://www.oschina.net/translate/8-ways-improve-asp-net-web-api-performance ASP.NET Web API 是非常棒的 ...

  5. 5 CrawlSpider操作

    CrawlSpider 提问:如果想要通过爬虫程序去爬取"糗百"全站数据新闻数据的话,有几种实现方法? 方法一:基于Scrapy框架中的Spider的递归爬取进行实现(Reques ...

  6. LVS初步

    LVS初步 一见 目录 目录 1 1. 前言 2 2. 思考 2 3. 名词解释 2 4. OSI参考模型 3 5. LVS架构 4 5.1. 负载均衡器(Load Balancer) 4 5.2.  ...

  7. 移动端html5页面导航栏悬浮遮挡内容第一行解决办法

    参考:https://zhidao.baidu.com/question/1608232105428062147.html 1.设置导航栏div属性position:fixed; .nav-fixed ...

  8. 5、Docker架构和底层技术

    5.1 Docker Platform Docker提供了一个开发,打包,运行APP的平台 把APP和底层infrastructure隔离开来 5.2 Docker Engine 后台进程(docke ...

  9. Android-自定义开关(ViewGroup版)

    虽然实现自定义开关,通常情况下都是继承View,比较合理方便快捷一些 但是我今天想去继承ViewGroup来实现自定义开关来玩玩 效果图: 布局代码: <!-- 自定义开关ViewGroup版 ...

  10. Buffer Pool--内存总结2

    按内存划分: 1.DATABASE CACHE 用于存放数据页面的缓冲区,8KB每页 2.各项组件 A)数据库连接(CONNECTION) B)通用数据,如果事务上下文,表和索引的元数据 C)执行计划 ...