操作系统课程设计进程调度算法模拟.doc
99计算机(2)班 戴虎 99031045 操作系统课程设计 -进程调度算法模拟一、 实验内容:模拟五种进程调度算法并计算各参数值二、 实验原理:(1) 先来先服务调度:最先到达的进程先进行调度(2) 短作业优先调度:从已到达的进程中选出最短的进程优先调度(3) 时间片轮转调度:每个进程依次的按时间片轮流的方式执行(4) 静态优先权调度:赋予优先权,在已到达的进程中选出优先权最高的优先调度(5) 高响应比优先调度:优先权随等待时间而增加,从而使等待时间较长且服务时间较短的进程优先调度响应比=1+等待时间/服务时间三、 实验设计说明:本程序采用C+ Builder 5编程和设计界面考虑到调度算法较多,采用了多页面的界面(如下图),加入的进程限制五个,在文本框中模拟到达一进程后,输入到达时间和服务时间,点击“加入新进程”即可,输入完成之后,点击运行即可得到表中各值。想从新输入可点击“清除”按钮。关闭时直接点击“×”即可。该程序还不是很完善,有一些小问题,尚待解决,还得向老师请教。 四、 实验结果:(只打印两个结果示范)运行后操作面板初始化如下图: 短作业优先结果如图:时间片轮转结果如图:(书上的例题)五、 实验源程序:/-#ifndef Unit1H#define Unit1H/-#include <Classes.hpp>#include <Controls.hpp>#include <StdCtrls.hpp>#include <Forms.hpp>#include <ComCtrls.hpp>#include <Grids.hpp>#include <ExtCtrls.hpp>/-class TForm1 : public TForm_published:/ IDE-managed Components 共13页 第13页TPageControl *PageControl1;TTabSheet *TabSheet1;TTabSheet *TabSheet2;TTabSheet *TabSheet3;TTabSheet *TabSheet4;TabSheet *TabSheet5; TStringGrid *sgr;TButton *Button1;TButton *Button2;TButton *Button3;TStringGrid *sgr1;TButton *Button4;TButton *Button5;TButton *Button6;TStringGrid *sgr2;TButton *Button7;TButton *Button8;TButton *Button9;TStringGrid *sgr3;TButton *Button10;TButton *Button11;TButton *Button12;TStringGrid *sgr4;TButton *Button13;TButton *Button14;TButton *Button15;TLabel *Label1;TEdit *Edit1;TLabel *Label2;TEdit *Edit2;TEdit *Edit3;TEdit *Edit4;TLabel *Label3;TLabel *Label4;TEdit *Edit5;TLabel *Label5;TLabel *Label6;TEdit *Edit6;TLabel *Label7;TEdit *Edit7;TLabel *Label8;TLabel *Label9;TLabel *Label10;TEdit *Edit8;TEdit *Edit9;TEdit *Edit10;TLabel *Label11;TLabel *Label12;TEdit *Edit11;TEdit *Edit12;void _fastcall FormCreate(TObject *Sender);void _fastcall Button3Click(TObject *Sender);void _fastcall Button1Click(TObject *Sender);void _fastcall Button2Click(TObject *Sender);void _fastcall Button6Click(TObject *Sender);void _fastcall Button5Click(TObject *Sender);void _fastcall Button4Click(TObject *Sender);void _fastcall Button9Click(TObject *Sender);void _fastcall Button7Click(TObject *Sender);void _fastcall Button8Click(TObject *Sender);void _fastcall Button12Click(TObject *Sender);void _fastcall Button10Click(TObject *Sender);void _fastcall Button11Click(TObject *Sender);void _fastcall Button15Click(TObject *Sender);void _fastcall Button13Click(TObject *Sender);void _fastcall Button14Click(TObject *Sender);private:/ User declarationspublic:/ User declarations _fastcall TForm1(TComponent* Owner);/-extern PACKAGE TForm1 *Form1;/-#endif */-#include <vcl.h>#pragma hdrstop#include "Unit1.h"/-#pragma package(smart_init)#pragma resource "*.dfm"TForm1 *Form1;float super6; /优先权float come6; /到达时间float serve6; /服务时间float begin6; /开始执行时间float end6; /完成时间float all6; /周转时间float dall6; /带权周转时间float max(float,float,float);/-_fastcall TForm1:TForm1(TComponent* Owner) /初始化表格 : TForm(Owner)/-void _fastcall TForm1:FormCreate(TObject *Sender)sgr->Cells00="进程名"sgr1->Cells00="进程名"sgr2->Cells00="进程名"sgr3->Cells00="进程名"sgr4->Cells00="进程名"sgr3->Cells01="优先权"sgr->Cells01="到达时间"sgr1->Cells01="到达时间"sgr2->Cells01="到达时间"sgr3->Cells02="到达时间"sgr4->Cells01="到达时间"sgr->Cells02="服务时间"sgr1->Cells02="服务时间"sgr2->Cells02="服务时间"sgr3->Cells03="服务时间"sgr4->Cells02="服务时间"sgr->Cells03="开始执行时间"sgr1->Cells03="开始执行时间"sgr3->Cells04="开始执行时间"sgr4->Cells03="开始执行时间"sgr4->Cells04="等待时间"sgr4->Cells05="响应比"sgr->Cells04="完成时间"sgr1->Cells04="完成时间"sgr2->Cells03="完成时间"sgr3->Cells05="完成时间"sgr4->Cells06="完成时间"sgr->Cells05="周转时间"sgr1->Cells05="周转时间"sgr2->Cells04="周转时间"sgr3->Cells06="周转时间"sgr4->Cells07="周转时间"sgr->Cells06="带权周转时间"sgr1->Cells06="带权周转时间"sgr2->Cells05="带权周转时间"sgr3->Cells07="带权周转时间"sgr4->Cells08="带权周转时间"sgr->Cells07="显示调度结果"sgr1->Cells07="显示调度结果"sgr3->Cells08="显示调度结果"sgr4->Cells09="显示调度结果"/-/先来先服务调度算法void _fastcall TForm1:Button3Click(TObject *Sender) /输入初值,5个static int i=1;sgr->Cellsi0=i; /进程编号sgr->Cellsi1=Edit1->Text; /输入到达时间comei=StrToFloat(Edit1->Text); /赋到达时间给表格sgr->Cellsi2=Edit2->Text; /输入服务时间servei=StrToFloat(Edit2->Text); / 赋服务时间给表格i+;if(i>5) i=1;/-float max(float x,float y,float m) /定义函数 return (x+y)>m?(x+y):m; /-/“运行”按键函数void _fastcall TForm1:Button1Click(TObject *Sender) int i=1;begin1=come1;sgr->Cells13=begin1; /初始值for(i=2;i<=5;i+) begini=max(servei-1,begini-1,comei); /调用函数 sgr->Cellsi3=begini; /开始执行时间for(i=1;i<=5;i+) /计算各个值 endi=begini+servei; sgr->Cellsi4=endi; alli=endi-comei; sgr->Cellsi5=alli; dalli=alli/servei; sgr->Cellsi6=dalli; sgr->Cellsi7=i; /-/“清除”按键函数void _fastcall TForm1:Button2Click(TObject *Sender) int i=0,j=0;for(i=0;i<=7;i+) for(j=1;j<=5;j+) sgr->Cellsji="" /-/短作业优先调度函数/“加入新进程”输入初值void _fastcall TForm1:Button6Click(TObject *Sender) static int i=1;sgr1->Cellsi0=i; /进程编号sgr1->Cellsi1=Edit3->Text; comei=StrToFloat(Edit3->Text);sgr1->Cellsi2=Edit4->Text;servei=StrToFloat(Edit4->Text);i+;if(i>5) i=1;/-/“清除”按键函数void _fastcall TForm1:Button5Click(TObject *Sender) int i=0,j=0; for(i=0;i<=7;i+) for(j=1;j<=5;j+) sgr1->Cellsji="" /-/“运行”按钮函数void _fastcall TForm1:Button4Click(TObject *Sender) int i,j,k,run=1,index=1,flag6=1,1,1,1,1,1,t4; /flag用于设置访问值“1”为未访问 sgr1->Cells17="1" begin1=come1; sgr1->Cells13=FloatToStr(begin1); end1=begin1+serve1; sgr1->Cells14=FloatToStr(end1); sgr1->Cells15=FloatToStr(end1-come1); sgr1->Cells16=FloatToStr(end1-come1)/serve1); /初始化 for(j=1;j<=4;j+) index+; /index自动指向下一个进程 if (index>5) /index值的复位 for(k=2;k<=5;k+) if (flagk=1) index=k; /index指向第一个未执行的进程 break; for(i=2;i<=5;i+) /满足三个条件的进程才可调度,1、已到达2、未调度过3、服务时间最小 if (comei<=endrun)&&(serveindex>servei)&&(flagi=1) index=i; else if (comei>endrun) /如果一进程完成未有进程到达,则下一个进程到来就直接调度 break; flagindex=0; /处理index,index为当前运行的进程编号,run为上一个进程的编号 beginindex=max(serverun,beginrun,comeindex); /开始执行时间 sgr1->Cellsindex3=FloatToStr(beginindex); run=index; endindex=beginindex+serveindex; /计算完成时间 sgr1->Cellsindex4=FloatToStr(endindex); allindex=endindex-comeindex; /计算周转时间 sgr1->Cellsindex5=FloatToStr(allindex); sgr1->Cellsindex6=FloatToStr(allindex/serveindex); /赋带权周转时间 tj=index; / tj为调度序列 for(i=1;i<=4;i+) sgr1->Cellsi+17=FloatToStr(ti); /显示调度结果 /-/时间片轮转调度算法void _fastcall TForm1:Button9Click(TObject *Sender)static int i=1;sgr2->Cellsi0=i; /进程编号sgr2->Cellsi1=Edit5->Text;comei=StrToFloat(Edit5->Text);sgr2->Cellsi2=Edit6->Text;servei=StrToFloat(Edit6->Text);i+;if(i>5) i=1;/-/“运行”按钮函数void _fastcall TForm1:Button7Click(TObject *Sender) int i=1,j=1,flag6=1,1,1,1,1,1; float t=0,q,a6; / q为时间片大小 q=StrToFloat(Edit7->Text); /赋值给q for(i=1;i<=5;i+) ai=servei; for(j=1;j<=100;j+) /设总时间片不超过100 for(i=1;i<=5;i+) if(flagi=1) servei-=q; /计算服务时间 if(servei>0) t+=q; /t为当前执行的总时间 else if(servei<=0) t+=(q+servei); /t为当前执行的总时间 endi=t; /进程调度完毕送完成时间 flagi=0; /一旦进程被调度后置flag值为0 for(i=1;i<=5;i+) /显示结果 sgr2->Cellsi3=FloatToStr(endi); alli=endi-comei; sgr2->Cellsi4=FloatToStr(alli); sgr2->Cellsi5=FloatToStr(alli/ai); /-/“清除”按键函数void _fastcall TForm1:Button8Click(TObject *Sender)int i=0,j=0; for(i=0;i<=6;i+) for(j=1;j<=5;j+) sgr2->Cellsji="" /-/优先权调度算法 ( 原理类似短作业优先)/“加入新进程”输入初值void _fastcall TForm1:Button12Click(TObject *Sender)static int i=1;sgr3->Cellsi0=i; /进程编号sgr3->Cellsi1=Edit8->Text;superi=StrToFloat(Edit8->Text);sgr3->Cellsi2=Edit9->Text;comei=StrToFloat(Edit9->Text);sgr3->Cellsi3=Edit10->Text;servei=StrToFloat(Edit10->Text);i+;if(i>5) i=1;/-/“运行”按钮函数void _fastcall TForm1:Button10Click(TObject *Sender) int i,j,k,run=1,index=1,flag6=1,1,1,1,1,1,t4; /run为上一个调度的进程号 sgr3->Cells18="1" begin1=come1; sgr3->Cells14=FloatToStr(begin1); end1=begin1+serve1; sgr3->Cells15=FloatToStr(end1); sgr3->Cells16=FloatToStr(end1-come1); sgr3->Cells17=FloatToStr(end1-come1)/serve1); /初始化for(j=1;j<=4;j+) index+; while(flagindex=0) index+; /index指向下一个待运行的进程 if (index>5) /index值的复位 for(k=2;k<=5;k+) if (flagk=1) index=k; break; for(i=2;i<=5;i+) if (comei<=endrun)&&(superindex>superi)&&(flagi=1) index=i; /满足条件的进程号送给index else if (comei>endrun) break; flagindex=0; /处理index beginindex=max(serverun,beginrun,comeindex); sgr3->Cellsindex4=FloatToStr(beginindex); run=index; endindex=beginindex+serveindex; sgr3->Cellsindex5=FloatToStr(endindex); allindex=endindex-comeindex; sgr3->Cellsindex6=FloatToStr(allindex); sgr3->Cellsindex7=FloatToStr(allindex/serveindex); tj=index; for(i=1;i<=4;i+) sgr3->Cellsi+18=FloatToStr(ti);/-/“清除”按键函数void _fastcall TForm1:Button11Click(TObject *Sender)int i=0,j=0; for(i=0;i<=9;i+) for(j=1;j<=5;j+) sgr3->Cellsji=""/-/高响应比优先调度算法 (类似优先权调度算法,响应比为动态的)/“清除”按键函数void _fastcall TForm1:Button15Click(TObject *Sender)static int i=1;sgr4->Cellsi0=i; /进程编号sgr4->Cellsi1=Edit11->Text;comei=StrToFloat(Edit11->Text);sgr4->Cellsi2=Edit12->Text;servei=StrToFloat(Edit12->Text);i+;if(i>5) i=1;/-/“运行”按钮函数void _fastcall TForm1:Button13Click(TObject *Sender)int i,j,k,run=1,index=1,flag6=1,1,1,1,1,1,t4;float xy6; /xy为响应比数组 sgr4->Cells19="1" begin1=come1; sgr4->Cells13=FloatToStr(begin1); sgr4->Cells14="0" xy1=1+0; sgr4->Cells15=FloatToStr(xy1); end1=begin1+serve1; sgr4->Cells16=FloatToStr(end1); sgr4->Cells17=FloatToStr(end1-come1); sgr4->Cells18=FloatToStr(end1-come1)/serve1); /初始化 for(j=1;j<=4;j+) index+; if (index>5) for(k=2;k<=5;k+) if (flagk=1) index=k; break; for(i=2;i<=5;i+) if (comei<endrun)&& (endrun-comeindex)/serveindex<(endrun-comeindex)/servei) &&(flagi=1) /动态计算响应比并比较 index=i; else if (comei>endrun) break; flagindex=0; /处理index beginindex=max(serverun,beginrun,comeindex); sgr4->Cellsindex3=FloatToStr(beginindex); sgr4->Cellsindex4=FloatToStr(endrun-comeindex); xyindex=1+(endrun-comeindex)/serveindex; sgr4->Cellsindex5=FloatToStr(xyindex); run=index; endindex=beginindex+serveindex; sgr4->Cellsindex6=FloatToStr(endindex); allindex=endindex-comeindex; sgr4->Cellsindex7=FloatToStr(allindex); sgr4->Cellsindex8=FloatToStr(allindex/servei