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