如何在linux/unix中設定線程的優先順序

來源:互聯網
上載者:User

在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;
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.