燕青专栏

读书笔记及技术探讨

原创 如何在linux/unix中设置线程的优先级收藏

 在linux下我们可以通过
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine)(void*), void *arg);
来创建线程,但是如何设置线程的优先级呢?
在讨论这个问题的时候,我们先要确定当前线程使用的调度策略,posix提供了
int pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy);函数来获取所
使用的调度策略,它们是:
SCHED_FIFO, SCHED_RR 和 SCHED_OTHER。
我们可以使用
int sched_get_priority_max(int policy);
int sched_get_priority_min(int policy);
来获取线程线程可是设置的最大和最小的优先级值,如果调用成功就返回最大和最小的优先级值,否则返回-1。
从我现在运行的linux系统中,我使用下列程序获取了对应三种调度策略中的最大和最小优先级:
policy = SCHED_OTHER
Show current configuration of priority
max_priority = 0
min_priority = 0
Show SCHED_FIFO of priority
max_priority = 99
min_priority = 1
Show SCHED_RR of priority
max_priority = 99
min_priority = 1
Show priority of current thread
priority = 0
Set thread policy
Set SCHED_FIFO policy
policy = SCHED_FIFO
Set SCHED_RR policy
policy = SCHED_RR
Restore current policy
policy = SCHED_OTHER

我们可以看到
SCHED_OTHER是不支持优先级使用的,而SCHED_FIFO和SCHED_RR支持优先级的使用,他们分别为1和99,
数值越大
优先级越高。 从上面的结果我们可以看出,如果程序控制线程的优先级,一般是用
pthread_attr_getschedpolicy来获取系统使用的调度策略,如果是SCHED_OTHER的话,表明当前策略
不支持线程优先级的使用,否则可以。当然所设定的优先级范围必须在最大和最小值之间。我们可以通过
sched_get_priority_maxsched_get_priority_min来获取。
可能网友会问,是否我们可以通过
int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);来设定自己所需的
调度策略呢?我觉得是完全可以的(有些系统需要定义
_POSIX_THREAD_PRIORITY_SCHEDULING),只要
系统实现了对应的调用策略。
说了半天,我们还没有说,在系统允许使用线程优先级别的时候,如何设置优先级别呢?
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);

上面两个函数分别用于设置线程的优先级,struct sched_param的定义如下
struct sched_param
{
int __sched_priority; //所要设定的线程优先级
};


使用的测试程序:
#include <iostream>
#include <pthread.h>
#include <sched.h>
#include <assert.h>

using namespace std;

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:
                cout << "policy = SCHED_FIFO" << endl;
                break;

        case SCHED_RR:
                cout << "policy = SCHED_RR" << endl;
                break;

        case SCHED_OTHER:
                cout << "policy = SCHED_OTHER" << endl;
                break;

        default:
                cout << "policy = UNKNOWN" << endl;
                break;
        }

        return policy;
}

static void show_thread_priority( pthread_attr_t &attr, int policy )
{
        int priority = sched_get_priority_max( policy );
        assert( priority != -1 );
        cout << "max_priority = " << priority << endl;

        priority = sched_get_priority_min( policy );
        assert( priority != -1 );
        cout << "min_priority = " << priority << endl;
}

static int get_thread_priority( pthread_attr_t &attr )
{
        struct sched_param param;

        int rs = pthread_attr_getschedparam( &attr, &param );
        assert( rs == 0 );
        cout << "priority = " << param.__sched_priority << endl;

        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 );

        cout << "Show current configuration of priority" << endl;
        show_thread_priority( attr, policy );

        cout << "Show SCHED_FIFO of priority" << endl;
        show_thread_priority( attr, SCHED_FIFO );

        cout << "Show SCHED_RR of priority" << endl;
        show_thread_priority( attr, SCHED_RR );

        cout << "Show priority of current thread" << endl;
        int priority = get_thread_priority( attr );

        cout << "Set thread policy" << endl;
        cout << "Set SCHED_FIFO policy" << endl;
        set_thread_policy( attr, SCHED_FIFO );
        cout << "Set SCHED_RR policy" << endl;
        set_thread_policy( attr, SCHED_RR );
        cout << "Restore current policy" << endl;
        set_thread_policy( attr, policy );


        rs = pthread_attr_destroy( &attr );
        assert( rs == 0 );

        return 0;
}

发表于 @ 2006年08月23日 15:38:00|评论(loading...)

新一篇: Debug with anonymous inner classes | 旧一篇: 关于pthread_cond_signal与pthread_cond_broadcast的使用说明

用户操作
[即时聊天] [发私信] [加为好友]
燕青
订阅我的博客
XML聚合  FeedSky
文章分类
收藏
    c/c++优秀网站
    boost
    c/c++ Reference
    C99标准
    Effective STL 中文版
    stlport
    stlsgi
    编程爱好者
    eBook
    rinet
    Excel/VBA
    VBA参考手册
    VBA自由代码库
    Linux GNU
    GNU Libs HOWTO
    Linux GUI
    QT中文论坛
    MICOM
    AVR MICRO
    WinAVR Tutorial
    MSND
    MSDN在线帮助
    web聊天网
    查看MSN联系人状态
    ebuddy
    iloveim
    imhaha
    meebo
    meebo11
    Web MSN
    米博
    澳洲移民
    前程专业留学移民公司
    百科全书
    QWika
    vesa标准
    wikipedia
    电子元器件网站
    eeworld
    好友链接
    Anders New Blog
    Anders的博客
    Anders的网站
    全胜花的生命痕迹
    小葛的博客
    尧的快乐一家
    捷仔的博客
    老郭的博客
    老钱的博客
    金种子母婴生活馆
    阿耀的博客
    家用网站
    上海移动通信
    中国电信网上营业厅
    金融网站
    纳斯达克
    开源网站
    apache
    opensource
    SVN
    垮平台GUI
    microwindows
    wxWidgets
    敏捷编程
    agilejournal
    extremeprogramming
    jayasoft
    maven
    上海四金网
    上海住房公积金网
    上海养老保险网
    上海医保网
    万年历
    21page万年历
    百渡万年历
    网页脚本教程
    DynamicDrive
    PageSource
    网站交换
    刘韧交换链接网站
    英语听力
    VOA慢速英语
    海词字典
    英语中级听力
    语高级听力
    优秀UNIX/LINUX网站
    IBM Linux论坛
    opengroup
    perl
    pthread
    socket
    UNIX Specification
    unix标准大全
    永远的UNIX
    存档
    Csdn Blog version 3.1a
    Copyright © 燕青