linux修改執行緒優先順序

2021-08-10 07:52:25 字數 4027 閱讀 7541

***

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

;

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

#include

#include

#include

#include

static int get_thread_policy(pthread_attr_t *attr)

return policy;

}static void show_thread_priority(pthread_attr_t *attr,int policy)

static int get_thread_priority(pthread_attr_t *attr)

static void set_thread_policy(pthread_attr_t *attr,int policy)

int main(void)

下面是測試程式的執行結果:

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

這裡測試一下其中的兩種特性,sched_other和sched_rr,還有就是優先順序的問題,是不是能夠保證,高優先順序的執行緒,就可以保證先執行。

下面的這個測試程式,建立了三個執行緒,預設建立的執行緒的排程策略是sched_other,其餘的兩個執行緒的排程策略設定成sched_rr。我的linux的核心版本是2.6.31。sched_rr是根據時間片來確定執行緒的排程。時間片用完了,不管這個執行緒的優先順序有多高都不會在執行,而是進入就緒佇列中,等待下乙個時間片的到了,那這個時間片到底要持續多長時間?在《深入理解linux核心》中的第七章程序排程中,是這樣描訴的,linux採取單憑經驗的方法,即選擇盡可能長、同時能保持良好相應時間的乙個時間片。這裡也沒有給出乙個具體的時間來,可能會根據不同的cpu 來定,還有就是多cpu 的情況。

#include

#include

#include

#include

void thread1()

printf("thread 1\n");

}printf("pthread 1 exit\n");

}void thread2()

printf("thread 2\n");

}printf("pthread 2 exit\n");

}void thread3()

printf("thread 3\n");

}printf("pthread 3 exit\n");

}int main()

下面是該程式的其中之一的執行結果:

sudo ./prio_test

the current user is root

sched_other

sched_rr

sched_rr 1

thread 1

thread 1

thread 1

thread 1

thread 1

thread 1

thread 1

thread 1

thread 1

pthread 1 exit

thread 2

thread 2

thread 2

thread 2

thread 2

thread 2

thread 2

thread 2

thread 2

pthread 2 exit

thread 3

thread 3

thread 3

thread 3

thread 3

thread 3

thread 3

thread 3

thread 3

pthread 3 exit

sudo ./prio_test

the current user is root

sched_other

sched_rr 1

thread 1

thread 1

thread 1

thread 1

thread 1

thread 1

thread 1

thread 1

thread 1

pthread 1 exit

sched_rr

thread 2

thread 2

thread 2

thread 2

thread 2

thread 2

thread 2

thread 2

thread 2

pthread 2 exit

thread 3

thread 3

thread 3

thread 3

thread 3

thread 3

thread 3

thread 3

thread 3

pthread 3 exit

可以看出並沒有每一次都保證高優先順序的執行緒先執行。

linux 執行緒優先順序

linux核心的三種排程方法 1,sched other 分時排程策略,2,sched fifo實時排程策略,先到先服務 3,sched rr實時排程策略,時間片輪轉 sched rr排程和sched fifo排程的程序屬於實時程序,以分時排程的程序是非實時程序。當實時程序準備就緒後,如果當前cpu...

linux執行緒優先順序

建立執行緒的順序 other rr low pri fifo low pri rr high pri fifo high pri rr low pri 可以搶占 other fifo low pri 可以搶占 rr low pri rr high pri 可以搶占 fifo low pri fifo...

Linux執行緒優先順序

linux核心的三種排程策略 1.sched other 分時排程策略 2.sched fifo 實時排程策略,先到先服務。一旦占用cpu則一直執行。一直執行直到有更高優先順序任務到達或自己放棄 3.sched rr實 時排程策略,時間片輪轉。當程序的時間片用完,系統將重新分配時間片,並置於就緒佇列...