在Linux中設置線程的優先級

如何在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_max和sched_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; //所要設定的線程優先級
};

例:創建優先級爲10的線程
pthread_attr_t   attr; 
struct   sched_param   param; 
pthread_attr_init(&attr); 
pthread_attr_setschedpolicy(&attr,   SCHED_RR); 
param.sched_priority   =   10; 
pthread_attr_setschedparam(&attr,   &param); 
pthread_create(xxx   ,   &attr   ,   xxx   ,   xxx); 
pthread_attr_destroy(&attr); 


附:使用的測試程序:

#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;
}
 
編譯命令:
#g++ pthread_priority3.c -o pthread_priority3 -lpthread

 

http://blog.csdn.net/vividonly/article/details/6902575

發佈了9 篇原創文章 · 獲贊 1 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章