模擬處理機進程調度---簡單循環輪轉調度算法


簡單循環輪轉調度算法原理

CPU空閑時,選取就緒隊列隊首元素,賦予時間片。當該進程時間片用完時,則釋放CPU控制權,進入就緒隊列的隊尾,CPU控制權給下一個處於就緒隊列首元素,原理如下圖。

實現流程圖

進程控制塊PCB 的結構如下:

模擬實現

①時間片定義為:總相應時間/進程數;

②在屏幕上輸出以下進程狀態表(表中每一行代表一個進程對一個時間片的占用):

③可以通過鍵盤命令動態地增加作業(即增加一個 PCB 數 3 據結構項),增加進程后,進程狀態表內容可更新查看。

算法代碼

#include "stdafx.h"
#include<queue>
#include<math.h>
#include<vector>
#include<iostream>
#include <iomanip>
using namespace std;

/*進程的數據結構*/
struct PCB
{
    int ID;//進程ID
    double in_time;//進程的進入時間
    double res_time;//進程的響應時間
    double l_time;//進程的剩余時間
};

/*進程調度函數                                    */
/*輸入:進程隊列prs,時間片timeslice                */
/*輸出:進程狀態表                                */    
void ProcessScheduling(queue<PCB>prs,double timeslice)
{
    cout << left;
    queue<PCB>ex_prs;//就緒隊列,該隊列中的進程是等待執行的
    ex_prs.push(prs.front());//第一個進程首先進入就緒隊列
    prs.pop();//將第一個進程退出進程隊列
    double slice_start;//時間片開始時間
    double slice_end = 0;//時間片結束時間
    cout << "----------------------------------進程狀態表------------------------------------" << endl;
    cout <<setw(15)<<"進程ID" << "|" << setw(15) << "到達時間" << "|" <<setw(15) << "總響應時間" << "|" << setw(15) << "時間片開始時間"<<"|" << setw(15) <<"時間片結束時間"<<"|"<< endl;
    cout << "--------------------------------------------------------------------------------" << endl;
    while (!prs.empty()||!ex_prs.empty())
    {
        slice_start = slice_end;//更新時間片開始與結束時間
        slice_end += timeslice;//每次增加一個時間片的時間
        while (!prs.empty())
        //每次執行一個進程前,將能在此進程占用cpu期間進入就緒隊列的進程加入就緒隊列
        {
            if (prs.front().in_time > slice_end)break;
            ex_prs.push(prs.front());
            prs.pop();
            if (ex_prs.size() == 1)
            {
                slice_start = slice_end;
                slice_end += timeslice;
            }
        }
        //如果就緒隊列為空則繼續while循環
        if (ex_prs.empty())continue;
        //如果剩余時間小於或等於時間片
        if (ex_prs.front().l_time <= timeslice)
        {
            ex_prs.front().l_time = 0;//將進程的剩余時間置為0(可有可無)
            cout << setw(15) << ex_prs.front().ID << "|" << setw(15) << ex_prs.front().in_time << "|" << setw(15) << ex_prs.front().res_time << "|" << setw(15) << slice_start<<"|"<< setw(15) << slice_end<<"|"<< endl;
            ex_prs.pop();
        }
        else
        {
            ex_prs.front().l_time -= timeslice;//將進程的剩余時間減少一個時間片
            cout << setw(15) << ex_prs.front().ID << "|" << setw(15) << ex_prs.front().in_time << "|" << setw(15) << ex_prs.front().res_time << "|" << setw(15) << slice_start << "|" << setw(15) << slice_end <<"|"<< endl;
            //將隊首進程置於隊尾
            PCB tmp_pr = ex_prs.front();
            ex_prs.pop();
            ex_prs.push(tmp_pr);
        }

    }
    cout << "--------------------------------------------------------------------------------" << endl;
}
int main()
{
    queue<PCB>prs;
    //總響應時間
    double allres_time = 0;
    //時間片
    double timeslice = 0;
    PCB    pr1, pr2, pr3;

    pr1 = { 1,0,10,10 };
    pr2 = { 2,1,5,5 };
    pr3 = { 3,2,5,5 };
    allres_time += pr1.res_time + pr2.res_time + pr3.res_time;
    timeslice = allres_time / 3;
    prs.push(pr1);
    prs.push(pr2);
    prs.push(pr3);
    cout << "時間片為:" << timeslice << endl;
    ProcessScheduling(prs, timeslice);
    cout << "添加進程?(y/n):";
    char i;
    cin >> i;

    while (i-'y'==0)
    {
        PCB pr;
        cout << "請輸入進程ID:";
        cin >> pr.ID;
        cout << "請輸入進程到達時間:";
        cin >> pr.in_time;
        cout << "請輸入進程響應時間:";
        cin >> pr.res_time;
        cout << "請輸入進程剩余時間:";
        cin >> pr.l_time;
        prs.push(pr);
        allres_time += pr.res_time;
        timeslice = allres_time / prs.size();
        cout << "時間片為:" << timeslice << endl;
        ProcessScheduling(prs, timeslice);
        cout << "繼續添加進程?(y/n):";
        cin >> i;
    }
    return 0;
}

運行結果截圖

編譯程序

運行程序

添加一個進程

繼續添加一個進程

運行環境:Ubuntu

相關問題補充:

①C++輸出一個表格

解決方法:首先用cout<<left設置左對齊,然后用setw()設置之后的文本寬度,如: cout << setw(12) << "Miles" ,設置其后的Miles寬為12個字母。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM