分類: 嵌入式學習
2010-08-25 15:57
251人閱讀收藏
舉報 #
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
而不是絕對依靠優先順序的高低,來保證。
不過,從執行的結果上,我們可以看到,
排程策略為sched_rr的執行緒1,執行緒2確實搶占了排程策略為sched_other的執行緒3。這個是可以理解的,由於scher_rr是實時排程策略。
只有在下述事件之一發生時,實時程序才會被另外乙個程序取代。
(1) 程序被另外乙個具有更高實時優先順序的實時程序搶占。
(2) 程序執行了阻塞操作並進入睡眠
(3)程序停止(處於task_stopped 或task_traced狀態)或被殺死。
(4)程序通過呼叫系統呼叫sched_yield(),自願放棄cpu 。
(5)程序基於時間片輪轉的實時程序(sched_rr),而且用完了它的時間片。
基於時間片輪轉的實時程序是,不是真正的改變程序的優先順序,而是改變程序的基本時間片的長度。所以基於時間片輪轉的程序排程,並不能保證高優先順序的程序先執行。
下面是另一種執行結果:
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實時排程策略,先到先服務。一旦占用cpu則一直執行。一直執行直到有更高優先順序任務到達或自己放棄 3,sched rr實時排程策略,時間片輪轉。當程序的時間片用完,系統將重新分配時間片,並置於就緒佇列尾。...
Linux下執行緒的排程策略與優先順序
linux核心的三種排程策略 1,sched other 分時排程策略,2,sched fifo實時排程策略,先到先服務。一旦占用cpu則一直執行。一直執行直到有更高優先順序任務到達或自己放棄 3,sched rr實時排程策略,時間片輪轉。當程序的時間片用完,系統將重新分配時間片,並置於就緒佇列尾。...
Linux下執行緒的排程策略與優先順序(二)
include stdio.h include unistd.h include stdlib.h include pthread.h void thread1 printf thread 1 n printf pthread 1 exit n void thread2 printf thread ...