進程調度:時間片輪轉調度演算法

來源:互聯網
上載者:User

一、實驗目的

(1) 加深對進程的理解

(2) 理解進程式控制制塊的結構

(3) 理解進程啟動並執行並發性

(4) 掌握時間片輪轉法進程調度演算法

二、實驗原理

(1)建立進程式控制制塊

(2)設計兩個鏈隊列,分別表示就緒隊列和完成隊列

(3)使用者輸入進程標識符,進程到達時間,進程所需的時間,申請空間存放進程,PCB資訊。

(4)每一個時間片結束輸出各進程的進程標識符,CPU已耗用時間

,進程所需時間,達到時間,周轉時間,以及狀態(運行完成或者就緒)



三、實驗步驟、資料記錄及處理


1.演算法流程


本程式中用到抽象資料類型的定義。


struct _PCBNode{char name[20];       //進程名稱int arrive_time;     //到達時間int need_time;      //服務時間int cputime;         //CPU已經執行的時間int accomplish_Time; //完成時間int turn_over_time;  //周轉時間bool tag;            //是否完成};  class _PCB{private:queue<struct _PCBNode> _PNode;queue<struct _PCBNode> _CNode;int TIME;}



主程式的流程以及各程式模組之間的層次(調用)關係。


實現概要設計中定義的主要函數,對主要函數寫出核心演算法(要求注釋);並儘可能畫程

序流程圖) 本程式寫著的就緒隊列中放著客戶外界輸入未到達的進程,所以在進行時間片輪轉時要判斷目前時間和到達時間,到達時間大於目前時間時才能CPU的調度

void Pop()//模仿時間輪轉調度{struct _PCBNode node;unsigned int n = _PNode.size();unsigned int  i = 0;for(; i< n; ++i)// for迴圈找隊列中到達時間大於目前時間時的進程{node = _PNode.front();_PNode.pop();if(node.arrive_time <= TIME){break;}_PNode.push(node);}TIME = TIME+Time_Pice;//類比進程運行中if(i == n)//沒找到到達時間大於目前時間時的進程{Display_PCB();return ;}node.cputime +=Time_Pice; //進程佔有CPU啟動並執行時間if(node.cputime >= node.need_time)//完成運行,進入完成隊列{node.accomplish_Time = TIME;node.tag = true;node.turn_over_time  = node.accomplish_Time -node.arrive_time ;_CNode.push(node);  cout<<node.name<<"   run over"<<endl;}else{_PNode.push(node);}Display_PCB(); }

2.運行結果分析

時間片被設定為 2,列印TIME時間時就緒隊列和運行完成隊列的進程狀態

四、總結與體會

  通過做本次實驗,我類比了CPU進程調度中的時間片輪轉調度演算法。時間片輪狀調度演算法可以實現進程共用CPU。在實驗中,我發現時間片不能太大,否則會導致大部分的進程在一個時間片中就能運行完成,不能實現進程對CPU資源的共用。

附錄:來源程式

#include<iostream>#include<queue>#include<string>using namespace std;#define Time_Pice 2struct _PCBNode{char name[20];       //進程名稱int arrive_time;     //到達時間int need_time;      //服務時間int cputime;         //CPU已經執行的時間int accomplish_Time; //完成時間int turn_over_time;  //周轉時間bool tag;            //是否完成};  class _PCB{public:void Push(struct _PCBNode& node){_PNode.push(node);}void init_time(){TIME = 0;}bool empty(){return _PNode.empty();}void Pop(){struct _PCBNode node;unsigned int n = _PNode.size();unsigned int  i = 0;for(; i< n; ++i){node = _PNode.front();_PNode.pop();if(node.arrive_time <= TIME){break;}_PNode.push(node);}TIME = TIME+Time_Pice;if(i == n){Display_PCB();return ;}node.cputime +=Time_Pice;if(node.cputime >= node.need_time){node.accomplish_Time = TIME;node.tag = true;node.turn_over_time  = node.accomplish_Time -node.arrive_time ;_CNode.push(node);  cout<<node.name<<"   run over"<<endl;}else{_PNode.push(node);}Display_PCB(); }    void Display_PCB(){cout<<"  TIME   "<<TIME<<endl;int n = _PNode.size();if(n != 0){cout<<"ready......."<<endl;cout<<"name   arrive_time  need_time    cputime    tag"<<endl;for(int i = 0;i<n; ++i){struct _PCBNode node = _PNode.front();_PNode.pop();if(node.arrive_time <= TIME){cout<<node.name<<"             "<<node.arrive_time<<"           "<<node.need_time<<"          "<<node.cputime <<"        "<<node.tag <<endl;}_PNode.push(node);}cout<<endl;}n = _CNode.size();if(n != 0){cout<<"run over"<<endl;cout<<"name   arrive_time  need_time   accomplish_Time     turn_over_time     tag"<<endl;for(int i = 0;i<n; ++i){struct _PCBNode node = _CNode.front();_CNode.pop();cout<<node.name<<"             "<<node.arrive_time<<"           "<<node.need_time<<"     "<<node.accomplish_Time<<"                     "<< node.turn_over_time<<"                 "<<node.tag <<endl;_CNode.push(node);}cout<<endl;}cout<<"**************************************************"<<endl;}private:queue<struct _PCBNode> _PNode;queue<struct _PCBNode> _CNode;int TIME;};void main(){_PCB pcb;pcb.init_time();int PCB_Numble;char PCBname[20];int arrive_time;int need_time;cout<<"input PCB_Numble :   ";cin>>PCB_Numble;cout<<"input PCBname arrive_time need_time example A 0 5\n";for(int i = 0; i<PCB_Numble; ++i){cin>>PCBname>>arrive_time>>need_time;struct _PCBNode node;strcpy(node.name ,PCBname);node.arrive_time = arrive_time;node.need_time = need_time;node.accomplish_Time  = 0;node.cputime = 0;node.tag = 0;pcb.Push(node);}pcb.Display_PCB();while(!pcb.empty()){pcb.Pop();}} 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.