資源簡介
本實驗通過編寫C#語言的代碼實現先來先服務(FCFS),最短作業(yè)優(yōu)先(SJF)、響應比高者優(yōu)先(HRN)的調度算法。最后在界面上顯示出進程在調度過程中的情況。另附上C++的進程調度實驗,算法不相同同。

代碼片段和文件信息
#include?
#include?
#include
#include
#include?
using?namespace?std;
#define?P_NUM?5??//進程數
#define?P_TIME?50//時間片長度
#define?MIN?-9999?//最小優(yōu)先級
enum?state{
????ready???//就緒
????execute?//執(zhí)行
????block???//阻塞
????finish???//完成
};
struct?pcb{??//進程控制塊結構
????char?name[4];?//進程名
????int?priority;?//優(yōu)先級
????int?cputime;??//占用CPU時間
????int?needtime;?//還需要執(zhí)行的時間
????int?count;
????int?round;?
????state?process;?//進程狀態(tài)
????pcb?*?next;
};
pcb?*?get_process()//獲取進程信息
{
????pcb?*q;
????pcb?*t;
????pcb?*p;//進程鏈表頭指針
????int?i=0;
????cout<<“input?“< ????while?(i ????????q=(struct?pcb?*)malloc(sizeof(pcb));
????????cin>>q->name;
????????cin>>q->needtime;
????????q->cputime???=?0;
????????q->priority??=?P_TIME?-?q->needtime;//計算優(yōu)先級
?????q->round?=?0;
?????q->count?=?0;
????????q->process???=?ready;
????????q->next??????=?NULL;
????????if?(i==0)??//第一個pcb
?????{
????????????p?=?q;
????????????t?=?q;
????????}
????????else
??{
????????????t->next?=?q;
????????????t?=?q;
????????}
????????i++;
????}??//while
????return?p;
}
void??display(pcb?*p){
????cout<<“NAME“<<“????“<<“CPUTIME“<<“????“<<“NEEDTIME“
??<<“????“<<“PRIORITY“<<“????“<<“STATE“< ????while(p){
????????cout<name;
????????cout<<“????????“;
????????cout<cputime;
????????cout<<“??????????“;
????????cout<needtime;
????????cout<<“??????????“;
????????cout<priority;
????????cout<<“??????????“;
????????switch(p->process){
????????????case?ready:cout<<“ready“< ????????????case?execute:cout<<“execute“< ????????????case?block:cout<<“block“< ????????????case?finish:cout<<“finish“< ????????}
????????p=p->next;
????}
}
int?process_finish(pcb?*q){//判斷進程是否均已執(zhí)行完畢
????int?bl=1;
????while(bl&&q)
{
????????bl?=?bl&&(q->needtime==0);
????????q?=?q->next;
????}
????return?bl;
}
void?cpuexe(pcb?*q){
????pcb*?t=q;
????int?tp=?MIN;
????while(q){??//尋找優(yōu)先級最大的進程
????????if?(q->process!=finish){
????????????q->process=ready;
????????????if(q->needtime==0){
????????????????q->process=finish;
????????????}
????????}
????????if(tppriority&&q->process!=finish){
????????????tp=q->priority;
????????????t=q;
????????}
????????q=q->next;
????}
????if(t->needtime!=0){??//執(zhí)行進程t
????????t->priority-=3;
????????t->needtime--;
????????t->process=execute;
????????t->cputime++;
????}
}
void?priority_cal()//優(yōu)先級調度
{
????pcb?*?p;
????system(“cls“);//清屏clrscr();
????p?=?get_process();?//獲取進程鏈表
system(“cls“);
????int?cpu=0;
system(“cls“);??//clrscr();
while(!process_finish(p)){
cpu++;
????????cout<<“cputime:“< ????????cpuexe(p);
????????display(p);
Sleep(2000);
system(“cls“);
}
cout<<“All?processes?have?finishedpress?any?key?to?exit“< ????getch();
}
void?display_menu()//顯示菜單
{
????cout<<“CHOOSE?THE?ALGORITHM:“<
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????20480??2010-01-06?22:43??Scheduling\bin\Debug\Scheduling.exe
?????文件??????19968??2010-01-06?22:43??Scheduling\bin\Debug\Scheduling.pdb
?????文件???????5632??2005-11-11?22:25??Scheduling\bin\Debug\Scheduling.vshost.exe
?????文件??????20480??2010-01-06?22:43??Scheduling\obj\Debug\Scheduling.exe
?????文件??????19968??2010-01-06?22:43??Scheduling\obj\Debug\Scheduling.pdb
?????文件????????381??2010-01-06?22:49??Scheduling\obj\Scheduling.csproj.FileListAbsolute.txt
?????文件??????10662??2010-01-06?22:39??Scheduling\Program.cs
?????文件???????1191??2009-12-14?10:00??Scheduling\Properties\AssemblyInfo.cs
?????文件???????1954??2009-12-14?10:00??Scheduling\Scheduling.csproj
?????文件????????919??2009-12-14?10:00??Scheduling.sln
?????目錄??????????0??2009-12-14?10:00??Scheduling\obj\Debug\TempPE
?????目錄??????????0??2009-12-14?10:14??Scheduling\bin\Debug
?????目錄??????????0??2010-01-06?22:43??Scheduling\obj\Debug
?????目錄??????????0??2009-12-14?10:00??Scheduling\bin
?????目錄??????????0??2009-12-14?10:06??Scheduling\obj
?????目錄??????????0??2009-12-14?10:00??Scheduling\Properties
?????目錄??????????0??2010-01-06?22:39??Scheduling
?????文件???????5234??2009-12-14?11:46??schudule.cpp
-----------?---------??----------?-----??----
???????????????106869????????????????????18
- 上一篇:C語言 馬踏棋盤 完整代碼及實驗報告
- 下一篇:三維布爾運算算法
評論
共有 條評論