資源描述:
《處理機(jī)的調(diào)度》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫(kù)。
1、操作系統(tǒng)實(shí)驗(yàn)報(bào)告學(xué)號(hào)540907010126:姓名:劉巖巖班級(jí):計(jì)科09-1處理機(jī)的調(diào)度一、實(shí)驗(yàn)?zāi)康模?)通過(guò)編寫(xiě)程序?qū)崿F(xiàn)進(jìn)程或作業(yè)先來(lái)先服務(wù)、高優(yōu)先權(quán)、按時(shí)間片輪轉(zhuǎn)調(diào)度算法,進(jìn)一步掌握進(jìn)程調(diào)度的概念和算法,加深對(duì)處理機(jī)分配的理解。(2)了解Linux中進(jìn)程(線程)的調(diào)度機(jī)制。(3)學(xué)習(xí)使用Linux中進(jìn)程(線程)調(diào)度算法,掌握相應(yīng)的與調(diào)度有關(guān)的函數(shù)。二、實(shí)驗(yàn)內(nèi)容與步驟:程序說(shuō)明:1)先來(lái)先服務(wù)算法:如果早就緒的進(jìn)程排在就緒隊(duì)列的前面,遲就緒的進(jìn)程排在就緒隊(duì)列的后面,那么先來(lái)先服務(wù)(FCFS:firstcomefirstservice)總是把當(dāng)前處于就緒隊(duì)列之首的那個(gè)進(jìn)
2、程調(diào)度到運(yùn)行狀態(tài)。2)2)輪轉(zhuǎn)法就是按一定時(shí)間片(記為q)輪番運(yùn)行各個(gè)進(jìn)程。如果q是一個(gè)定值,則輪轉(zhuǎn)法是一種對(duì)各進(jìn)程機(jī)會(huì)均等的調(diào)度方法。3)優(yōu)先級(jí)調(diào)度的基本思想是,把當(dāng)前處于就緒隊(duì)列中優(yōu)先級(jí)最高的進(jìn)程投入運(yùn)行,而不管各進(jìn)程的下一個(gè)CPU周期的長(zhǎng)短和其他因素。三、具體步驟:分析問(wèn)題,提出解決問(wèn)題的算法編制程序程序調(diào)試記錄實(shí)驗(yàn)結(jié)果,以及思考是否能夠改善算法四、程序及運(yùn)行結(jié)果:#include#include#defineP_NUM5#defineP_TIME50enumstate{ready,execute,block,finish};
3、structpcbb{charname[4];intpriority;//數(shù)越大優(yōu)先級(jí)越高intcputime;//已占用CPU的時(shí)間intneedtime;//執(zhí)行時(shí)間intcount;enumstateprocess;structpcbb*next;};typedefstructpcbbpcb;voiddisplay_menu(){printf("CHOOSETHEALGORITHM:");printf("1PRIORITY");printf("2ROUNDROBIN");printf("3EXIT");}pcb*get_process(){pcb*
4、q;pcb*p;//頭指針pcb*t;//尾指針inti=0;printf("inputnameandtime");while(iname);scanf("%d",&q->needtime);q->cputime=0;q->priority=P_TIME-q->needtime;q->process=ready;q->next=NULL;if(i==0){p=q;t=q;}else{t->next=q;t=q;}i++;}returnp;}voidfree_proce
5、ss(pcb*p){pcb*q;while(p!=NULL){q=p;p=p->next;free(q);}}voiddisplay(pcb*p){printf("namecputimeneedtimeprioritystate");while(p){printf("%s",p->name);printf("");printf("%d",p->cputime);printf("");printf("%d",p->needtime);printf("");printf("%d",p->priority);printf("");switch(p->process){ca
6、seready:printf("ready");break;caseexecute:printf("execute");break;caseblock:printf("block");break;casefinish:printf("finish");break;}p=p->next;}}intprocess_finish(pcb*q){intb1=1;while(b1&&q){b1=b1&&q->needtime==0;q=q->next;}returnb1;}voidcpuexe(pcb*q){pcb*t=q;inttp=0;while(q){//讓
7、t指向優(yōu)先級(jí)最高的進(jìn)程if(q->process!=finish){q->process=ready;if(q->needtime==0){q->process=finish;}}if(tppriority&&q->process!=finish){tp=q->priority;t=q;}q=q->next;}if(t->needtime!=0){t->priority-=3;//每執(zhí)行一次優(yōu)先級(jí)降低三個(gè)單位t->needtime--;t->process=execute;t->cputime++;}}voidpriority_c