進(jìn)程調(diào)度算法實(shí)驗(yàn)報(bào)告_第1頁(yè)
進(jìn)程調(diào)度算法實(shí)驗(yàn)報(bào)告_第2頁(yè)
進(jìn)程調(diào)度算法實(shí)驗(yàn)報(bào)告_第3頁(yè)
進(jìn)程調(diào)度算法實(shí)驗(yàn)報(bào)告_第4頁(yè)
進(jìn)程調(diào)度算法實(shí)驗(yàn)報(bào)告_第5頁(yè)
已閱讀5頁(yè),還剩10頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、操作系統(tǒng)實(shí)驗(yàn)報(bào)告(二)實(shí)驗(yàn)題目:進(jìn)程調(diào)度算法實(shí)驗(yàn)環(huán)境:C+實(shí)驗(yàn)?zāi)康模壕幊棠M實(shí)現(xiàn)幾種常見(jiàn)的進(jìn)程調(diào)度算法,通過(guò)對(duì)幾組進(jìn)程分別使用不同的調(diào)度算法,計(jì)算進(jìn)程的平均周轉(zhuǎn)時(shí)間和平均帶權(quán)周轉(zhuǎn)時(shí)間,比較各種算法的性能優(yōu)劣。實(shí)驗(yàn)內(nèi)容:編程實(shí)現(xiàn)如下算法:1.先來(lái)先服務(wù)算法;2.短進(jìn)程優(yōu)先算法;3.時(shí)間片輪轉(zhuǎn)調(diào)度算法。設(shè)計(jì)分析:程序流程圖:1.先來(lái)先服務(wù)算法初始化PCB,輸入進(jìn)程信息開始各進(jìn)程按先來(lái)先到的順序進(jìn)入就緒隊(duì)列就緒隊(duì)列?結(jié)束運(yùn)行運(yùn)行進(jìn)程所需CPU時(shí)間取消該進(jìn)程2.短進(jìn)程優(yōu)先算法3.時(shí)間片輪轉(zhuǎn)調(diào)度算法實(shí)驗(yàn)代碼:1. 先來(lái)先服務(wù)算法#include <iostream.h>#define n 2

2、0typedef struct  int id;          /進(jìn)程名 int atime;         /進(jìn)程到達(dá)時(shí)間 int runtime;       /進(jìn)程運(yùn)行時(shí)間fcs;void main() int amount,i,j,diao,huan;    fcs fn; 

3、cout<<"請(qǐng)輸入進(jìn)程個(gè)數(shù):"<<endl; cin>>amount; for(i=0;i<amount;i+)   cout<<"請(qǐng)輸入進(jìn)程名,進(jìn)程到達(dá)時(shí)間,進(jìn)程運(yùn)行時(shí)間:"<<endl;  cin>>fi.id;  cin>>fi.atime;  cin>>fi.runtime;   for(i=0;i<amount;i+) 

4、0;       /按進(jìn)程到達(dá)時(shí)間的先后排序                                /如果兩個(gè)進(jìn)程同時(shí)到達(dá),按在屏幕先輸入的先運(yùn)行  for(j=0;j<amount-i-1;j+)

5、0;   if(fj.atime>fj+1.atime)   diao=fj.atime;    fj.atime=fj+1.atime;    fj+1.atime=diao;    huan=fj.id;    fj.id=fj+1.id;    fj+1.id=huan;       for(i=0;i<amount;i+)  

6、cout<<"進(jìn)程:"<<fi.id<<"從"<<fi.atime<<"開始"<<","<<"在"   <<fi.atime+fi.runtime<<"之前結(jié)束。"<<endl;  fi+1.atime=fi.atime+fi.runtime; 2. 短進(jìn)程優(yōu)先算法#include<stdio.h>#def

7、ine n 5#define num 5#define max 65535typedef struct pro int PRO_ID; int arrive_time;int sum_time;int flag;Pro;/整數(shù)排序 int bubble(int temp) int i,j,tem=0; for(i=1;i<num;i+) int lastX=1;for(j=0;j<num-i;j+) if(tempj>tempj+1) tem=tempj; tempj=tempj+1; tempj+1=tem; lastX=0;if(lastX=1) break;return

8、 temp0; /進(jìn)程排序 Pro bubble(Pro p) int i,j;Pro temp=0;Pro snum;for(i=0;i<num;i+) si=pi; for(i=1;i<num;i+)int lastX=1;for(j=0;j<num-i;j+)if(sj.sum_time>sj+1.sum_time) temp=sj; sj=sj+1; sj+1=temp; lastX=0;if(lastX=1) break;return s0; void SPF(int p)if(n>0) int i,j,k,l,tc=0;Pro seqn;Pro tem

9、p_seqn;printf("短進(jìn)程優(yōu)先調(diào)度算法SPFn");printf("請(qǐng)依次輸入5個(gè)進(jìn)程的進(jìn)程號(hào)、到達(dá)時(shí)間和執(zhí)行時(shí)間n");printf("成員變量用逗號(hào)隔開;進(jìn)程間用回車隔開n"); for(i=0;i<n;i+) scanf("%d,%d,%d",&seqi.PRO_ID,&seqi.arrive_time,&seqi.sum_time);printf("調(diào)度順序是:n");/初始化tcint tempnum;for(i=0;i<num;i+) t

10、empi=seqi.arrive_time;tc=bubble(temp);/tc是斷點(diǎn)啊 /flag 表示對(duì)應(yīng)i的pro的隊(duì)列情況/-1表示未進(jìn)入過(guò)隊(duì)列,0表示在隊(duì)列中,1表示被清除了for(i=0;i<n;i+)seqi.flag=-1; for(i=0;i<n;i+) for(j=0;j<n;j+) if(seqj.flag!=1&&seqj.arrive_time<=tc) seqj.flag=0; for(j=0;j<n;j+) temp_seqj=seqj; if(seqj.flag!=0) temp_seqj.sum_time=max

11、; l=bubble(temp_seq).PRO_ID;for(j=0;j<n;j+)if(l=seqj.PRO_ID)k=j; tc=tc+bubble(temp_seq).sum_time; seqk.flag=1; printf("%d",l);printf("n");void main()SPF(n);3. 時(shí)間片輪轉(zhuǎn)調(diào)度算法頭文件RR.h#include<iostream>#include<stdio.h>#include<string.h>#include<stdlib.h>#includ

12、e<ctype.h>#define MaxNum 100typedef struct pcb /定義進(jìn)程控制塊char NameMaxNum; /進(jìn)程名int arrivetime; /到達(dá)時(shí)間int runtime; /運(yùn)行時(shí)間int wholetime; /固定運(yùn)行時(shí)間int FinishTime; /完成時(shí)間double WeightTime; /周轉(zhuǎn)時(shí)間double WeightWholeTime; /帶權(quán)周轉(zhuǎn)時(shí)間char state; /運(yùn)行后的狀態(tài)struct pcb *next;PCB;/全局變量int N; /實(shí)際進(jìn)程數(shù)double SumWT; /周轉(zhuǎn)時(shí)間之和do

13、uble SumWWT; /帶權(quán)周轉(zhuǎn)時(shí)間之和double AverageWT; /平均周轉(zhuǎn)時(shí)間double AverageWWT; /平均帶權(quán)周轉(zhuǎn)時(shí)間typedef struct /定義隊(duì)列,封裝頭結(jié)點(diǎn),指針?lè)謩e指向隊(duì)頭和隊(duì)尾PCB *front,*rear;queue;queue *init() /進(jìn)程隊(duì)列置空queue *head;head=(queue*)malloc(sizeof(queue);head->front=NULL;head->rear=NULL;return head;int empty(queue *head) /檢驗(yàn)隊(duì)列是否為空return (head-&

14、gt;front?0:1);queue *append(queue *head,char cMaxNum,int a,int r,char s) /進(jìn)程隊(duì)列入隊(duì),往后插入PCB *p;p=(PCB *)malloc(sizeof(PCB);strcpy(p->Name,c);p->arrivetime=a;p->runtime=r;p->wholetime=r;p->state=s;/p->FinishTime=0;/p->WeightTime=0;/p->WeightWholeTime=0;p->next=NULL;if(empty(he

15、ad)head->front=head->rear=p;elsehead->rear->next=p;head->rear=p;return head;queue *creat(queue *head) /創(chuàng)建進(jìn)程隊(duì)列char cMaxNum;char s='R'int a,r,i;printf("請(qǐng)輸入共有幾個(gè)進(jìn)程:n");scanf("%d",&N);for(i=1;i<=N;i+)printf("請(qǐng)輸入第%d 個(gè)進(jìn)程的進(jìn)程名:n",i);getchar();gets(c

16、);printf("請(qǐng)輸入第%d 個(gè)進(jìn)程的到達(dá)時(shí)間:n",i);scanf("%d",&a);printf("請(qǐng)輸入第%d 個(gè)進(jìn)程的服務(wù)時(shí)間:n",i);scanf("%d",&r);head=append(head,c,a,r,s);return head;void print(queue *head) /輸入創(chuàng)建的進(jìn)程隊(duì)列PCB *p;p=head->front;if(!p)printf("時(shí)間片輪轉(zhuǎn)調(diào)度隊(duì)列為空!n");while(p)printf("Nam

17、e=%s arrivetime=%d runtime=%d state=%c",p->Name,p->arrivetime,p->runtime,p->state);printf("n");p=p->next;/*時(shí)間片輪轉(zhuǎn)法調(diào)度算法的實(shí)現(xiàn)*/void RR(queue *head,int q)int t=head->front->arrivetime, lt=head->rear->arrivetime;if(head->front->runtime<q)t=t+head->front

18、->runtime;elset=t+q;/*進(jìn)程隊(duì)列為不空才可調(diào)度*/while(!empty(head)PCB *p1,*p2; printf("n時(shí)刻 進(jìn)程 運(yùn)行后的狀態(tài)n");/*第一種情況:當(dāng)前運(yùn)行的時(shí)間小于最后一個(gè)進(jìn)程到達(dá)時(shí)間做一下操作*/while(t<lt)p1=head->front;printf("%2d %s",t,p1->Name);p1->runtime=p1->runtime-q;/1.運(yùn)行時(shí)間小于0,刪除隊(duì)首if(p1->runtime<=0)p1->state='

19、C'printf(" %cn",p1->state);p1->FinishTime=t;p1->WeightTime=p1->FinishTime-p1->arrivetime;p1->WeightWholeTime=p1->WeightTime/p1->wholetime; SumWT+=p1->WeightTime;SumWWT+=p1->WeightWholeTime;printf("時(shí)刻%2d進(jìn)程%s運(yùn)行結(jié)束,進(jìn)程%s周轉(zhuǎn)時(shí)間=%5.2f,帶權(quán)周轉(zhuǎn)時(shí)間=%5.2fn",t,p1-

20、>Name,p1->Name,p1->WeightTime,p1->WeightWholeTime);head->front=p1->next;free(p1);/2.運(yùn)行時(shí)間大于0,向后找位置插入elseprintf(" %cn",p1->state);p2=p1->next;while(p2->next && p2->arrivetime != t)p2=p2->next;/此時(shí)無(wú)新進(jìn)入隊(duì)列的進(jìn)程,有兩種情況:1.不用找位置往后插入,隊(duì)首不變,不做操作/2.找位置往后插入if(p2->

21、;arrivetime != t)PCB *p3=p1,*p4;while(p3->next && p3->arrivetime<t)p4=p3;p3=p3->next;if(p3->arrivetime>t)if(p4!=p1) /p1插在p4后,頭為p1->nexthead->front=p1->next;p1->next=p4->next;p4->next=p1;else /不做操作p4=p3=p2=NULL;elsep4=p3=p2=NULL;/此時(shí)有新進(jìn)入隊(duì)列的進(jìn)程時(shí):p1插在新進(jìn)入隊(duì)列的進(jìn)程p2

22、后,隊(duì)首為p1->nextelsehead->front=p1->next;p1->next=p2->next;p2->next=p1;/時(shí)刻變化 if(head->front->runtime<q)t=t+head->front->runtime;elset=t+q;/*第一種情況結(jié)束*/*第二種情況:當(dāng)期運(yùn)行的時(shí)間大于最后一個(gè)進(jìn)程到達(dá)的時(shí)間做以下操作*/while(t>=lt)p1=head->front;printf("%2d %s",t,p1->Name);p1->runtim

23、e=p1->runtime-q;/1.運(yùn)行時(shí)間小于0,刪除隊(duì)首if(p1->runtime<=0)p1->state='C'printf(" %cn",p1->state); p1->FinishTime=t;p1->WeightTime=p1->FinishTime-p1->arrivetime;p1->WeightWholeTime=p1->WeightTime/p1->wholetime; SumWT+=p1->WeightTime;SumWWT+=p1->Weight

24、WholeTime;printf("時(shí)刻%2d進(jìn)程%s運(yùn)行結(jié)束,進(jìn)程%s周轉(zhuǎn)時(shí)間=%5.2f,帶權(quán)周轉(zhuǎn)時(shí)間=%5.2fn",t,p1->Name,p1->Name,p1->WeightTime,p1->WeightWholeTime);/printf("時(shí)刻%2d進(jìn)程%s運(yùn)行結(jié)束",t,p1->pname);head->front=p1->next;free(p1);/2.運(yùn)行時(shí)間大于0,直接插在隊(duì)尾elseprintf(" %cn",p1->state);/若原隊(duì)列只有一個(gè)進(jìn)程,不必往隊(duì)尾插 if(!p1->next)head->front=p1; /若原隊(duì)列有多個(gè)進(jìn)程elsehead->front=p1->next; head->rear->next=p1;head->rear=p1;p1-

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論