Linux線程優先級


轉自:https://www.cnblogs.com/imapla/p/4234258.html

 

Linux內核的三種調度策略:

    1.SCHED_OTHER 分時調度策略

    2.SCHED_FIFO  實時調度策略,先到先服務。一旦占用cpu則一直運行。一直運行直到有更高優先級任務到達或自己放棄

    3.SCHED_RR實  時調度策略,時間片輪轉。當進程的時間片用完,系統將重新分配時間片,並置於就緒隊列尾。放在隊列尾保證了所有具有相同優先級的RR任務的調度公平

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.sched_priority = 51; //設置優先級

    系統創建線程時,默認的線程是 SCHED_OTHER。所以如果我們要改變線程的調度策略的話,可以通過下面的這個函數實現。

int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);

    上面的param使用了下面的這個數據結構:

struct sched_param
{
    int __sched_priority; // 所要設定的線程優先級
};

    我們可以通過下面的測試程序來說明,我們自己使用的系統的支持的優先級:

復制代碼
#include <stdio.h>
#include <pthread.h>
#include <sched.h>
#include <assert.h>

static int api_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");
            break;
        case SCHED_OTHER:
            printf ("policy = SCHED_OTHER\n");
            break;
        default:
            printf ("policy = UNKNOWN\n");
            break; 
    }
    return policy;
}

static void api_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 api_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 api_set_thread_policy (pthread_attr_t *attr,int policy)
{
    int rs = pthread_attr_setschedpolicy (attr, policy);
    assert (rs == 0);
    api_get_thread_policy (attr);
}
    
int main(void)
{
    pthread_attr_t attr;       // 線程屬性
    struct sched_param sched;  // 調度策略
    int rs;

    /* 
     * 對線程屬性初始化
     * 初始化完成以后,pthread_attr_t 結構所包含的結構體
     * 就是操作系統實現支持的所有線程屬性的默認值
     */
    rs = pthread_attr_init (&attr);
    assert (rs == 0);     // 如果 rs 不等於 0,程序 abort() 退出

    /* 獲得當前調度策略 */
    int policy = api_get_thread_policy (&attr);

    /* 顯示當前調度策略的線程優先級范圍 */
    printf ("Show current configuration of priority\n");
    api_show_thread_priority(&attr, policy);

    /* 獲取 SCHED_FIFO 策略下的線程優先級范圍 */
    printf ("show SCHED_FIFO of priority\n");
    api_show_thread_priority(&attr, SCHED_FIFO);

    /* 獲取 SCHED_RR 策略下的線程優先級范圍 */
    printf ("show SCHED_RR of priority\n");
    api_show_thread_priority(&attr, SCHED_RR);

    /* 顯示當前線程的優先級 */
    printf ("show priority of current thread\n");
    int priority = api_get_thread_priority (&attr);

    /* 手動設置調度策略 */
    printf ("Set thread policy\n");

    printf ("set SCHED_FIFO policy\n");
    api_set_thread_policy(&attr, SCHED_FIFO);

    printf ("set SCHED_RR policy\n");
    api_set_thread_policy(&attr, SCHED_RR);

    /* 還原之前的策略 */
    printf ("Restore current policy\n");
    api_set_thread_policy (&attr, policy);

    /* 
     * 反初始化 pthread_attr_t 結構
     * 如果 pthread_attr_init 的實現對屬性對象的內存空間是動態分配的,
     * phread_attr_destory 就會釋放該內存空間
     */
    rs = pthread_attr_destroy (&attr);
    assert (rs == 0);

    return 0;
}
復制代碼

 

下面是測試程序的運行結果:

復制代碼
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=0Set thread policy
set SCHED_FIFO policy
policy= SCHED_FIFO
set SCHED_RR policy
policy= SCHED_RRRestore current policy
policy=SCHED_OTHER
復制代碼

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM