作業系統實驗程序調演算法模擬
一、實驗目的
程序排程是處理機管理的核心內容。本實驗要求用高階語言編寫模擬程序排程程式,以便加深理解有關程序控制快、程序佇列等概念,並體會和了解優先數演算法和時間片輪轉演算法的具體實施辦法。
二、實驗內容和要求
- 設計程序控制塊PCB的結構,通常應包括:程序名、程序優先數(或輪轉時間片數)、程序已佔用的CPU時間、程序到完成還需要的時間、程序的狀態、當前佇列指標等。
- 編寫兩種排程演算法程式:
1) 優先數排程演算法程式;
2) 迴圈輪轉排程演算法程式。
- 將程式原始碼和執行截圖寫入實驗報告並提交。
三、實驗步驟
- 程序控制模組結構:
Name:程序標示符
Prio/Round:程序優先數
Cputime:程序累計佔用
Needtime:程序到完成還需時間片數
State:程序狀態
Next:鏈指標
- 程序就緒態和等待態為連結串列結構:
Run:當前執行程序指標
Ready:就緒佇列頭指標
Tall:就緒隊尾指標
Finish:完成佇列頭指標
- 程式模組結構:
Insert1:優先數演算法中,將尚未完成的pcb按優先數順序插入到就緒佇列中
Insert2:在輪轉法中,將執行了一個時間片單位(2),但尚未完成的程序的pcb插到就緒佇列的隊尾
Firstin:排程就行佇列的第一個程序投入執行
Print:顯示每執行一次後所有程序的狀態及相關資訊
Create:建立新程序,並將他的pcb插入就緒佇列
Prisch:按優先數演算法排程程序
Roundsch:按時間片輪轉法排程程序
優先數演算法中,程序優先數的初值為50needtime
- 實驗程式碼
#include<iostream>
#include<string>
using namespace std;
typedef struct node
{
char name[20]; //程序名
int prio; //程序優先順序
int round; //分配cpu的時間片
int cputime; //cpu執行時間
int needtime; //程序執行所需時間
char state; //程序狀態
int count; //記錄執行次數
struct node *next; //連結串列指標
}PCB;
int num;
PCB *ready = NULL; //就緒佇列
PCB *run = NULL; //執行佇列
PCB *finish = NULL; //完成佇列
//取得第一個就緒節點
void GetFirst()
{
run = ready;
if(ready!=NULL)
{
run->state='R';
ready=ready->next;
run->next=NULL;
}
}
//優先順序輸出佇列
void Output1()
{
PCB *p;
p=ready;
while(p != NULL)
{
cout<<p->name<<"\t"<<p->prio<<"\t"<<p->cputime<<"\t"<<p->needtime<<"\t"<<
p->state<<"\t"<<p->count<<endl;
p=p->next;
}
p=finish;
while(p!=NULL)
{
cout<<p->name<<"\t"<<p->prio<<"\t"<<p->cputime<<"\t"<<p->needtime<<"\t"<<
p->state<<"\t"<<p->count<<endl;
p=p->next;
}
p=run;
while(p!=NULL)
{
cout<<p->name<<"\t"<<p->prio<<"\t"<<p->cputime<<"\t"<<p->needtime<<"\t"<<
p->state<<"\t"<<p->count<<endl;
p=p->next;
}
}
//輪轉法輸出佇列
void Output2()
{
PCB *p;
p=ready;
while(p!=NULL)
{
cout<<p->name<<"\t"<<p->round<<"\t"<<p->cputime<<"\t"<<p->needtime<<"\t"<<
p->state<<"\t"<<p->count<<endl;
p=p->next;
}
p=finish;
while(p!=NULL)
{
cout<<p->name<<"\t"<<p->round<<"\t"<<p->cputime<<"\t"<<p->needtime<<"\t"<<
p->state<<"\t"<<p->count<<endl;
p=p->next;
}
p=run;
while(p!=NULL)
{
cout<<p->name<<"\t"<<p->round<<"\t"<<p->cputime<<"\t"<<p->needtime<<"\t"<<
p->state<<"\t"<<p->count<<endl;
p=p->next;
}
}
void InsertPrio(PCB *in)
{
PCB *fst,*nxt;
fst=nxt=ready;
if(ready==NULL)
{
in->next=ready;
ready=in;
}
else
{
if(in->prio>=fst->prio)
{
in->next=ready;
ready =in;
}
else
{
while(fst->next!=NULL)
{
nxt=fst;
fst=fst->next;
}
if(fst->next == NULL)
{
in->next=fst->next;
fst->next=in;
}
else
{
nxt=in;
in->next=fst;
}
}
}
}
void InsertTime(PCB *in)
{
PCB *fst;
fst =ready;
if(ready==NULL)
{
in->next = ready;
ready = in;
}
else
{
while(fst->next!=NULL)
{
fst=fst->next;
}
in->next=fst->next;
fst->next=in;
}
}
void InsertFinish(PCB *in)
{
PCB *fst;
fst=finish;
if(finish==NULL)
{
in->next=finish;
finish=in;
}
else
{
while(fst->next!=NULL)
{
fst=fst->next;
}
in->next=finish;
finish=in;
}
}
void PrioCreate()
{
PCB *tmp;
int i;
cout<<"Enter the name and needtime :"<<endl;
for(i=0;i<num;i++)
{
if((tmp=(PCB*)malloc(sizeof(PCB)))==NULL)
{
cerr<<"malloc"<<endl;
exit(1);
}
cin>>tmp->name;
getchar();
cin>>tmp->needtime;
tmp->cputime=0;
tmp->state='W';
tmp->prio=50-tmp->needtime;
tmp->round=0;
tmp->count=0;
InsertPrio(tmp);
}
cout<<"程序名\t優先順序\tcpu時間\t需要時間程序狀態計數器"<<endl;
}
void TimeCreate()
{
PCB *tmp;
int i;
cout<<"輸入程序名字和程序時間片所需時間:"<<endl;
for(i=0;i<num;i++)
{
if((tmp=(PCB*)malloc(sizeof(PCB)))==NULL)
{
cerr<<"malloc"<<endl;
exit(1);
}
cin>>tmp->name;
getchar();
cin>>tmp->needtime;
tmp->cputime=0;
tmp->state='W';
tmp->prio=0;
tmp->round=2;
tmp->count=0;
InsertTime(tmp);
}
cout<<"程序名\t優先順序\tcpu時間\t需要時間程序狀態計數器"<<endl;
}
void Priority()
{
int flag=1;
GetFirst();
while(run!=NULL)
{
Output1();
while(flag)
{
run->prio=3;
run->cputime++;
run->needtime--;
if(run->needtime==0)
{
run->state='F';
run->count++;
InsertFinish(run);
flag=0;
}
else
{
run->state='W';
run->count++;
InsertTime(run);
flag=0;
}
}
flag=1;
GetFirst();
}
}
void RoundRun()
{
int flag=1;
GetFirst();
while(run!=NULL)
{
Output2();
while(flag)
{
run->count++;
run->cputime++;
run->needtime--;
if(run->needtime==0)
{
run->state='F';
InsertFinish(run);
flag=0;
}
else if(run->count==run->round)
{
run->state='W';
run->count=0;
InsertTime(run);
flag=0;
}
}
flag=1;
GetFirst();
}
}
int main(void)
{
int n;
cout<<"輸入程序個個數: "<<endl;
cin>>num;
getchar();
cout<<"----------程序排程演算法模擬-----------"<<endl;
cout<<" 1.優先順序排程演算法"<<endl;
cout<<" 2.迴圈輪轉排程演算法"<<endl;
cout<<"-------------------------------------"<<endl;
cout<<"輸入序號: "<<endl;
cin>>n;
switch(n)
{
case 1:
cout<<"優先順序排程 :"<<endl;
PrioCreate();
Priority();
Output1();
break;
case 2:
cout<<"迴圈輪轉演算法: "<<endl;
TimeCreate();
RoundRun();
Output2();
break;
case 0:
exit(1);
break;
default:
cout<<"Enter error!"<<endl;
break;
}
cout<<endl;
return 0;
}
四、實驗結果
五、實驗總結
通過本次實驗,我學到了程序排程演算法,瞭解到了程序排程是cpu管理的核心,不同的排程演算法會使程序的執行時間不同,選擇使用合適的演算法是提供執行速度的一個關鍵,同時本次實驗也讓我久違的使用了c++程式設計,算是一次有意義的複習吧。