




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、操作系統(tǒng)實驗報告(二)實驗題目:進程調(diào)度算法實驗環(huán)境:C+實驗?zāi)康模壕幊棠M實現(xiàn)幾種常見的進程調(diào)度算法,通過對幾組進程分別使用不同的調(diào)度算法,計算進程的平均周轉(zhuǎn)時間和平均帶權(quán)周轉(zhuǎn)時間,比較各種算法的性能優(yōu)劣。實驗內(nèi)容:編程實現(xiàn)如下算法:1.先來先服務(wù)算法;2.短進程優(yōu)先算法;3.時間片輪轉(zhuǎn)調(diào)度算法。設(shè)計分析:程序流程圖:1.先來先服務(wù)算法初始化PCB,輸入進程信息開始各進程按先來先到的順序進入就緒隊列就緒隊列?結(jié)束運行運行進程所需CPU時間取消該進程2.短進程優(yōu)先算法3.時間片輪轉(zhuǎn)調(diào)度算法實驗代碼:1. 先來先服務(wù)算法#include <iostream.h>#define n 2
2、0typedef struct int id; /進程名 int atime; /進程到達時間 int runtime; /進程運行時間fcs;void main() int amount,i,j,diao,huan; fcs fn;
3、cout<<"請輸入進程個數(shù):"<<endl; cin>>amount; for(i=0;i<amount;i+) cout<<"請輸入進程名,進程到達時間,進程運行時間:"<<endl; cin>>fi.id; cin>>fi.atime; cin>>fi.runtime; for(i=0;i<amount;i+)
4、0; /按進程到達時間的先后排序 /如果兩個進程同時到達,按在屏幕先輸入的先運行 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<<"進程:"<<fi.id<<"從"<<fi.atime<<"開始"<<","<<"在" <<fi.atime+fi.runtime<<"之前結(jié)束。"<<endl; fi+1.atime=fi.atime+fi.runtime; 2. 短進程優(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; /進程排序 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("短進程優(yōu)先調(diào)度算法SPFn");printf("請依次輸入5個進程的進程號、到達時間和執(zhí)行時間n");printf("成員變量用逗號隔開;進程間用回車隔開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是斷點啊 /flag 表示對應(yīng)i的pro的隊列情況/-1表示未進入過隊列,0表示在隊列中,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. 時間片輪轉(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 /定義進程控制塊char NameMaxNum; /進程名int arrivetime; /到達時間int runtime; /運行時間int wholetime; /固定運行時間int FinishTime; /完成時間double WeightTime; /周轉(zhuǎn)時間double WeightWholeTime; /帶權(quán)周轉(zhuǎn)時間char state; /運行后的狀態(tài)struct pcb *next;PCB;/全局變量int N; /實際進程數(shù)double SumWT; /周轉(zhuǎn)時間之和do
13、uble SumWWT; /帶權(quán)周轉(zhuǎn)時間之和double AverageWT; /平均周轉(zhuǎn)時間double AverageWWT; /平均帶權(quán)周轉(zhuǎn)時間typedef struct /定義隊列,封裝頭結(jié)點,指針分別指向隊頭和隊尾PCB *front,*rear;queue;queue *init() /進程隊列置空queue *head;head=(queue*)malloc(sizeof(queue);head->front=NULL;head->rear=NULL;return head;int empty(queue *head) /檢驗隊列是否為空return (head-&
14、gt;front?0:1);queue *append(queue *head,char cMaxNum,int a,int r,char s) /進程隊列入隊,往后插入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)建進程隊列char cMaxNum;char s='R'int a,r,i;printf("請輸入共有幾個進程:n");scanf("%d",&N);for(i=1;i<=N;i+)printf("請輸入第%d 個進程的進程名:n",i);getchar();gets(c
16、);printf("請輸入第%d 個進程的到達時間:n",i);scanf("%d",&a);printf("請輸入第%d 個進程的服務(wù)時間:n",i);scanf("%d",&r);head=append(head,c,a,r,s);return head;void print(queue *head) /輸入創(chuàng)建的進程隊列PCB *p;p=head->front;if(!p)printf("時間片輪轉(zhuǎn)調(diào)度隊列為空!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;/*時間片輪轉(zhuǎn)法調(diào)度算法的實現(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;/*進程隊列為不空才可調(diào)度*/while(!empty(head)PCB *p1,*p2; printf("n時刻 進程 運行后的狀態(tài)n");/*第一種情況:當(dāng)前運行的時間小于最后一個進程到達時間做一下操作*/while(t<lt)p1=head->front;printf("%2d %s",t,p1->Name);p1->runtime=p1->runtime-q;/1.運行時間小于0,刪除隊首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("時刻%2d進程%s運行結(jié)束,進程%s周轉(zhuǎn)時間=%5.2f,帶權(quán)周轉(zhuǎn)時間=%5.2fn",t,p1-
20、>Name,p1->Name,p1->WeightTime,p1->WeightWholeTime);head->front=p1->next;free(p1);/2.運行時間大于0,向后找位置插入elseprintf(" %cn",p1->state);p2=p1->next;while(p2->next && p2->arrivetime != t)p2=p2->next;/此時無新進入隊列的進程,有兩種情況:1.不用找位置往后插入,隊首不變,不做操作/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;/此時有新進入隊列的進程時:p1插在新進入隊列的進程p2
22、后,隊首為p1->nextelsehead->front=p1->next;p1->next=p2->next;p2->next=p1;/時刻變化 if(head->front->runtime<q)t=t+head->front->runtime;elset=t+q;/*第一種情況結(jié)束*/*第二種情況:當(dāng)期運行的時間大于最后一個進程到達的時間做以下操作*/while(t>=lt)p1=head->front;printf("%2d %s",t,p1->Name);p1->runtim
23、e=p1->runtime-q;/1.運行時間小于0,刪除隊首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("時刻%2d進程%s運行結(jié)束,進程%s周轉(zhuǎn)時間=%5.2f,帶權(quán)周轉(zhuǎn)時間=%5.2fn",t,p1->Name,p1->Name,p1->WeightTime,p1->WeightWholeTime);/printf("時刻%2d進程%s運行結(jié)束",t,p1->pname);head->front=p1->next;free(p1);/2.運行時間大于0,直接插在隊尾elseprintf(" %cn",p1->state);/若原隊列只有一個進程,不必往隊尾插 if(!p1->next)head->front=p1; /若原隊列有多個進程elsehead->front=p1->next; head->rear->next=p1;head->rear=p1;p1-
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 國際貿(mào)易代理基礎(chǔ)知識考核試卷
- 珠寶首飾表面處理技術(shù)考核試卷
- 玻璃制品耐候性測試與優(yōu)化考核試卷
- 稻谷種植農(nóng)業(yè)氣象服務(wù)需求與供給考核試卷
- 新材料新技術(shù)引領(lǐng)可持續(xù)發(fā)展的新方向考核試卷
- 果蔬汁飲料的企業(yè)文化與品牌建設(shè)考核試卷
- 紡織企業(yè)成本分析與控制考核試卷
- 勞務(wù)派遣企業(yè)招聘渠道分析與優(yōu)化考核試卷
- 濟南大學(xué)《模特經(jīng)紀(jì)管理》2023-2024學(xué)年第二學(xué)期期末試卷
- 江西服裝學(xué)院《嬰幼兒護理與急救》2023-2024學(xué)年第二學(xué)期期末試卷
- 水資源論證工作大綱
- 中考物理命題培訓(xùn)講座
- 生產(chǎn)安全事故風(fēng)險評估報告(參考模板)
- 125萬噸硫鐵礦斜坡道施工組織設(shè)計
- 畢業(yè)設(shè)計10層框架—剪力墻結(jié)構(gòu)體系設(shè)計計算書
- 東南大學(xué)論文模板v1.1
- 神經(jīng)系統(tǒng)體格檢查-PPT課件
- 賽英公司FOD監(jiān)測雷達系統(tǒng)
- 固體制劑車間主要過程控制點
- 膿毒癥的診療規(guī)范 中醫(yī)
- 關(guān)于基礎(chǔ)底板“跳倉法”施工熱工計算
評論
0/150
提交評論