資源描述:
《任務(wù)二、單處理機(jī)系統(tǒng)的進(jìn)程調(diào)度》由會員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫。
1、任務(wù)二、單處理機(jī)系統(tǒng)的進(jìn)程調(diào)度實(shí)驗(yàn)?zāi)康模?、加深對進(jìn)程概念的理解,明確進(jìn)程和程序的區(qū)別2、深入了解系統(tǒng)如何組織進(jìn)程,創(chuàng)建進(jìn)程3、進(jìn)一步認(rèn)識如何實(shí)現(xiàn)處理機(jī)調(diào)度。實(shí)驗(yàn)代碼:#include"stdio.h"#include#include#definegetpch(type)(type*)malloc(sizeof(type))#defineNULL0structpcb{/*定義進(jìn)程控制塊PCB*/charname[10];charstate;intsuper;intntime;intrtime;structpcb*link;}*rea
2、dy=NULL,*p;typedefstructpcbPCB;voidsort()/*建立對進(jìn)程進(jìn)行優(yōu)先級排列函數(shù)*/{PCB*first,*second;intinsert=0;if((ready==NULL)
3、
4、((p->super)>(ready->super)))/*優(yōu)先級最大者,插入隊(duì)首*/{p->link=ready;ready=p;}else/*進(jìn)程比較優(yōu)先級,插入適當(dāng)?shù)奈恢弥?/{first=ready;second=first->link;while(second!=NULL){if((p->super)>(second->super))/*若插入進(jìn)程
5、比當(dāng)前進(jìn)程優(yōu)先數(shù)大,*/{/*插入到當(dāng)前進(jìn)程前面*/p->link=second;first->link=p;second=NULL;insert=1;}else/*插入進(jìn)程優(yōu)先數(shù)最低,則插入到隊(duì)尾*/{first=first->link;second=second->link;}}if(insert==0)first->link=p;}}voidinput()/*建立進(jìn)程控制塊函數(shù)*/{inti,num;printf("請輸入進(jìn)程數(shù)量:");scanf("%d",&num);for(i=0;i6、p=getpch(PCB);printf("輸入進(jìn)程名:");scanf("%s",p->name);printf("輸入進(jìn)程優(yōu)先數(shù):");scanf("%d",&p->super);printf("輸入進(jìn)程運(yùn)行時間:");scanf("%d",&p->ntime);printf("");p->rtime=0;p->state='w';p->link=NULL;sort();/*調(diào)用sort函數(shù)*/}}intspace(){intl=0;PCB*pr=ready;while(pr!=NULL){l++;pr=pr->link;}return(l);}vo
7、idshow(){printf("qnametstatetsupertndtimetruntime");}voiddisp(PCB*pr)/*建立進(jìn)程顯示函數(shù),用于顯示當(dāng)前進(jìn)程*/{printf("%st",pr->name);printf("%ct",pr->state);printf("%dt",pr->super);printf("%dt",pr->ntime);printf("%dt",pr->rtime);printf("");}voidcheck()/*建立進(jìn)程查看函數(shù)*/{PCB*pr;printf("****當(dāng)前正在運(yùn)行
8、的進(jìn)程是:%s",p->name);/*顯示當(dāng)前運(yùn)行進(jìn)程*/show();disp(p);pr=ready;if(pr==NULL)printf("****當(dāng)前就緒隊(duì)列為空!");else{printf("****當(dāng)前就緒隊(duì)列狀態(tài)為:");/*顯示就緒隊(duì)列狀態(tài)*/show();while(pr!=NULL){disp(pr);pr=pr->link;}}}voiddestroy()/*建立進(jìn)程撤消函數(shù)(進(jìn)程運(yùn)行結(jié)束,撤消進(jìn)程)*/{printf("進(jìn)程[%s]已完成.",p->name);free(p);}voidrunning()/*建立進(jìn)程就緒函數(shù)(
9、進(jìn)程運(yùn)行時間到,置就緒狀態(tài)*/{(p->rtime)++;if(p->rtime==p->ntime)destroy();/*調(diào)用destroy函數(shù)*/else{(p->super)--;p->state='w';sort();/*調(diào)用sort函數(shù)*/}}voidmain()/*主函數(shù)*/{intlen,h=0;charch;input();len=space();while((len!=0)&&(ready!=NULL)){ch=getchar();h++;printf("當(dāng)前運(yùn)行次數(shù)為:%d",h);p=ready;ready