實驗 使用動態(tài)優(yōu)先權(quán)的進程調(diào)度算法模擬

1乍迄、實驗?zāi)康?/h1>

通過動態(tài)優(yōu)先權(quán)算法的模擬加深對進程概念進程調(diào)度過程的理解。

2士败、實驗內(nèi)容

  1. 用C語言來實現(xiàn)對N個進程采用動態(tài)優(yōu)先權(quán)優(yōu)先算法的進程調(diào)度闯两。
  2. 每個用來標識進程的進程控制塊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排成隊列假勿。
  1. 優(yōu)先數(shù)改變的原則:
  • 進程在就緒隊列中呆一個時間片,優(yōu)先數(shù)加1态鳖。
  • 進程每運行一個時間片转培,優(yōu)先數(shù)減3。
  1. 假設(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
  1. 為了清楚的觀察各進程的調(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];
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末删窒,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子顺囊,更是在濱河造成了極大的恐慌肌索,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,406評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件特碳,死亡現(xiàn)場離奇詭異诚亚,居然都是意外死亡,警方通過查閱死者的電腦和手機午乓,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評論 3 393
  • 文/潘曉璐 我一進店門站宗,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人益愈,你說我怎么就攤上這事梢灭。” “怎么了?”我有些...
    開封第一講書人閱讀 163,711評論 0 353
  • 文/不壞的土叔 我叫張陵或辖,是天一觀的道長瘾英。 經(jīng)常有香客問我,道長颂暇,這世上最難降的妖魔是什么缺谴? 我笑而不...
    開封第一講書人閱讀 58,380評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮耳鸯,結(jié)果婚禮上湿蛔,老公的妹妹穿的比我還像新娘。我一直安慰自己县爬,他們只是感情好阳啥,可當我...
    茶點故事閱讀 67,432評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著财喳,像睡著了一般察迟。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上耳高,一...
    開封第一講書人閱讀 51,301評論 1 301
  • 那天扎瓶,我揣著相機與錄音,去河邊找鬼泌枪。 笑死概荷,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的碌燕。 我是一名探鬼主播误证,決...
    沈念sama閱讀 40,145評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼修壕!你這毒婦竟也來了愈捅?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,008評論 0 276
  • 序言:老撾萬榮一對情侶失蹤叠殷,失蹤者是張志新(化名)和其女友劉穎改鲫,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體林束,經(jīng)...
    沈念sama閱讀 45,443評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡像棘,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,649評論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了壶冒。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片缕题。...
    茶點故事閱讀 39,795評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖胖腾,靈堂內(nèi)的尸體忽然破棺而出烟零,到底是詐尸還是另有隱情瘪松,我是刑警寧澤,帶...
    沈念sama閱讀 35,501評論 5 345
  • 正文 年R本政府宣布锨阿,位于F島的核電站宵睦,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏墅诡。R本人自食惡果不足惜壳嚎,卻給世界環(huán)境...
    茶點故事閱讀 41,119評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望末早。 院中可真熱鬧烟馅,春花似錦、人聲如沸然磷。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,731評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽姿搜。三九已至寡润,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間痪欲,已是汗流浹背悦穿。 一陣腳步聲響...
    開封第一講書人閱讀 32,865評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留业踢,地道東北人。 一個月前我還...
    沈念sama閱讀 47,899評論 2 370
  • 正文 我出身青樓礁扮,卻偏偏與公主長得像知举,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子太伊,可洞房花燭夜當晚...
    茶點故事閱讀 44,724評論 2 354

推薦閱讀更多精彩內(nèi)容