/*設定執行緒優先順序和排程演算法的例項*/
#include
#include
void *thread_one_info(void)
printf("thread %ld:in thread_one——info function\n", (long)pthread_self());
}void * thread_two_info(void)
printf("thread %ld: in thread_two_info function\n ",(long)pthread_self());
}int main(int argc, char * argv)
num1 = atoi(argv[1]);
num2 = atoi(argv[2];
pthread_attr_t attr_obj1;
pthread_attr_t attr_obj2;
//初始化執行緒屬性物件
pthread_attr_init(&attr_obj1);
pthread_attr_init(&attr_obj2);
//獲得執行緒屬性物件的優先順序
ret = pthread_attr_getschedparam(&attr_obj1, ¶m1);
if(ret != 0)
ret = pthread_attr_getschedparam(&attr_obj2, ¶m2);
if(ret != 0)
//輸出優先順序資訊
printf("obj1:thread priority : %d \n", param1.sched_priority);
printf("obj2:thread priorty : %d \n", param2.sched_priority);
//更改執行緒優先順序
param.sched_priority = num1;
param.sched_priority = num2;
printf("*****now thread priority===");
printf("obj1:thread priority : %d\n",param1.sched_priority);
printf("obj2: thread priority : %d\n",param2.sched_priority);
//設定執行緒的排程優先順序
ret = pthread_attr_setschedparam(&attr_obj1, ¶m1);
if(ret != 0)
ret = pthread_attr_setschedparam(&attr_obj2, ¶m2);
if(ret != 0)
//設定排程作用域
ret = pthread_attr_setscope(&attr_obj1, pthread_scope_system);
if(ret != 0)
ret = pthread_attr_setscope(&attr_obj2, pthread_scope_system);
if(ret != 0)
//設定排程策略
ret = pthread_attr_setschedpolicy(&attr_obj1, sched_rr);
ret = pthread_attr_setschedpolicy(&attr_obj2, sched_rr);
//建立新的執行緒
ret = pthread_creare(&pthread_id1, &attr_obj1, (void *)thread_one_info,null);
if(ret != -1)
ret = pthread_creare(&pthread_id2, &attr_obj2, (void *)thread_one_info,null);
if(ret != -1)
//連線線程
pthread_join(thread_id1, null);
pthread_join(thread_id2, null);
return 0;
}
優先順序排程演算法
include include includetypedef struct node pcb pcb ready null,run null,finish null 定義三個佇列,就緒佇列,執行佇列和完成佇列 int num void getfirst 從就緒佇列取得第乙個節點 void outpu...
執行緒優先順序的設定
執行緒的優先順序 1 10,10為最高端別,1為最低級別,5為預設級別 thread.min priority 最小優先順序 thread.max priority 最高優先順序 thread.norm priority 預設優先順序 設定優先順序 public class jointhread2 ...
十五 優先順序排程演算法
演算法 將處理機分配給就緒佇列中優先順序最高的程序 優先順序 靜態優先順序 在程序建立時確立,確定後執行期間保持不變。確立依據有 程序的型別 程序對資源的需求 使用者申請的優先順序 優點 簡單 缺點 不能動態反映程序特點,系統排程效能差 動態優先順序 程序在開始建立時,根據某種原則確定乙個優先順序後...