1乍迄、實驗?zāi)康?/h1>
通過動態(tài)優(yōu)先權(quán)算法的模擬加深對進程概念進程調(diào)度過程的理解。
2士败、實驗內(nèi)容
- 用C語言來實現(xiàn)對N個進程采用動態(tài)優(yōu)先權(quán)優(yōu)先算法的進程調(diào)度闯两。
- 每個用來標識進程的進程控制塊PCB用結(jié)構(gòu)來描述褥伴,包括以下字段:
- 進程標識數(shù) ID。
- 進程優(yōu)先數(shù) PRIORITY漾狼,并規(guī)定優(yōu)先數(shù)越大的進程重慢,其優(yōu)先權(quán)越高。
- 進程已占用的CPU時間CPUTIME逊躁。
- 進程還需占用的CPU時間ALLTIME似踱。當進程運行完畢時,ALLTIME變?yōu)?稽煤。???? 進程的阻塞時間STARTBLOCK核芽,表示當進程再運行STARTBLOCK個時間片后,將進入阻塞狀態(tài)念脯。
- 進程被阻塞的時間BLOCKTIME狞洋,表示已足賽的進程再等待BLOCKTIME個時間片后弯淘,將轉(zhuǎn)換成就緒狀態(tài)绿店。
- 進程狀態(tài)START。
- 隊列指針NEXT庐橙,用來將PCB排成隊列假勿。
- 優(yōu)先數(shù)改變的原則:
- 進程在就緒隊列中呆一個時間片,優(yōu)先數(shù)加1态鳖。
- 進程每運行一個時間片转培,優(yōu)先數(shù)減3。
- 假設(shè)在調(diào)度前浆竭,系統(tǒng)中有5個進程浸须,它們的初始狀態(tài)如下:
ID 0 1 2 3 4
PRIORITY 9 38 30 29 0
CPUTIME 0 0 0 0 0
ALLTIME 3 3 6 3 4
STARTBLOCK 2 -1 -1 -1 -1
BLOCKTIME 3 0 0 0 0
STATE READY READY READY READY READY
- 為了清楚的觀察各進程的調(diào)度過程,程序應(yīng)將每個時間片內(nèi)的情況顯示出來邦泄,參照的具體格式如下:
RUNNING PROG:i
READY-QUEUE:-〉id1-〉id2
BLOCK-QUEUE:-〉id3-〉id4
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = == = =
ID 0 1 2 3 4
PRIORITY P0 P1 P2 P3 P4
CUPTIME C0 C1 C2 C3 C4
ALLTIME A0 A1 A2 A3 A4
STARTBLOCK T0 T1 T2 T3 T4
BLOCKTIME B0 B1 B2 B3 B4
STATE S0 S1 S2 S3 S4
實驗代碼
#include<queue>
#include<iostream>
#include<iomanip>
using namespace std;
typedef int ID;
typedef int Priority;
typedef int Time;
enum State {
sready, sblocked, sruning, sstop
};
struct PCB{
ID id;
Priority priority;
Time cpu_time;
Time all_time;
Time start_block;
Time block_time;
State state;
public:
PCB(
ID id0,
Priority priority0,
Time cpu_time0,
Time all_time0,
Time start_block0,
Time block_time0,
State state0
): id(id0), priority(priority0), cpu_time(cpu_time0),
all_time(all_time0), start_block(start_block0),
block_time(block_time0), state(state0) {
}
};
struct cmp //??д?o???
{
bool operator() (PCB* a, PCB* b)
{
return a->priority < b->priority; //????
}
};
typedef priority_queue<PCB*, vector<PCB*>, cmp> mqueue;
const int width = 10;
void show_PCB(PCB* pcb) {
string state = "";
switch(pcb->state) {
case sready: state = "ready"; break;
case sblocked : state = "blocked";break;
case sruning : state = "runing";break;
case sstop: state = "stop";break;
defalut:state="unknown";break;
}
cout
<< left << setw(width) << setfill(' ') << pcb->id << "|"
<< left << setw(width) << setfill(' ') << pcb->priority << "|"
<< left << setw(width) << setfill(' ') << pcb->cpu_time << "|"
<< left << setw(width) << setfill(' ') << pcb->all_time << "|"
<< left << setw(width) << setfill(' ') << pcb->start_block << "|"
<< left << setw(width) << setfill(' ') << pcb->block_time << "|"
<< left << setw(width) << setfill(' ') << state << "|" << endl;
}
void show_queue(queue<PCB*> q){
cout
<< left << setw(7*width+7) << setfill('-') << "" << "\n"
<< left << setw(width) << setfill(' ') << "id" << "|"
<< left << setw(width) << setfill(' ') << "priority" << "|"
<< left << setw(width) << setfill(' ') << "cpuTime" << "|"
<< left << setw(width) << setfill(' ') << "allTime" << "|"
<< left << setw(width) << setfill(' ') << "startBlock" << "|"
<< left << setw(width) << setfill(' ') << "blockTime" << "|"
<< left << setw(width) << setfill(' ') << "state" << "|" << endl;
while(!q.empty()) {
PCB* t = q.front();
q.pop();
show_PCB(t);
}
cout << left << setw(7*width+7) << setfill('-') << "" << "\n";
}
void show_queue(mqueue q){
cout
<< left << setw(7*width+7) << setfill('-') << "" << "\n"
<< left << setw(width) << setfill(' ') << "id" << "|"
<< left << setw(width) << setfill(' ') << "priority" << "|"
<< left << setw(width) << setfill(' ') << "cpuTime" << "|"
<< left << setw(width) << setfill(' ') << "allTime" << "|"
<< left << setw(width) << setfill(' ') << "startBlock" << "|"
<< left << setw(width) << setfill(' ') << "blockTime" << "|"
<< left << setw(width) << setfill(' ') << "state" << "|" << endl;
while(!q.empty()) {
PCB* t = q.top();
q.pop();
show_PCB(t);
}
cout << left << setw(7*width+7) << setfill('-') << "" << "\n";
}
queue<PCB*> finished;
void run_a_time(mqueue& ready,mqueue& blocked,PCB* runing) {
mqueue t_ready, t_blocked;
runing = ready.top();
ready.pop();
runing->priority -= 3;
runing->cpu_time += 1;
runing->all_time -= 1;
runing->start_block -= 1;
if(runing->start_block == 0) {
t_blocked.push(runing);
runing->state = sruning;
} else {
if(runing->all_time > 0) {
t_ready.push(runing);
} else {
finished.push(runing);
runing->state = sstop;
}
}
mqueue t_queue = blocked;
while(!t_queue.empty()) {
PCB* t = t_queue.top();
t_queue.pop();
t->block_time -= 1;
if(t->block_time == 0) {
t_ready.push(t);
t->block_time = 0;
t->start_block = -1;
t->state = sready;
} else {
t_blocked.push(t);
t->state = sblocked;
}
}
t_queue = ready;
while(!t_queue.empty()) {
PCB* t = t_queue.top();
t_queue.pop();
t->priority += 1;
t->start_block -= 1;
if(t->start_block == 0) {
t_blocked.push(t);
t->state = sblocked;
} else {
t_ready.push(t);
t->state = sready;
}
}
ready = t_ready;
blocked = t_blocked;
}
int main() {
mqueue ready, blocked;
PCB* runing = nullptr;
PCB* pcbs[] = {
new PCB(0,9,0,3,2,3, sready),
new PCB(1,38,0,3,-1,0, sready),
new PCB(2,30,0,6,-1,0, sready),
new PCB(3,29,0,3,-1,0, sready),
new PCB(4,0,0,4,-1,0, sready)
};
int pcb_num = sizeof(pcbs)/sizeof(PCB*);
for(int i = 0; i < pcb_num; i++) {
ready.push(pcbs[i]);
}
show_queue(ready);
int clock_ = 1;
while(!ready.empty() || !blocked.empty()) {
cout << "clock = " << clock_ << endl;
run_a_time(ready, blocked, runing);
cout << "ready queue:" << endl;
show_queue(ready);
cout << "blocked queue:" << endl;
show_queue(blocked);
//system("pause");
clock_++;
}
show_queue(finished);
//delete
for(int i = 0; i < pcb_num; i++) {
delete pcbs[i];
}
}