Analog Processor Process Scheduling - Simple Round Robin Scheduling Algorithm

 

The principle of simple round-robin scheduling algorithm

When the CPU is idle, select the head element of the ready queue and assign a time slice. When the time slice of the process runs out, the CPU control is released, and it enters the end of the ready queue. The CPU control is given to the next element at the head of the ready queue. The principle is as follows.

Implementation flow chart

Process scheduling source code

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

/* Process data structure */ 
struct PCB
{
    int ID; // Process ID 
    double in_time; // Process entry time 
    double res_time; // Process response time 
    double l_time; // Process remaining time 
};

/* Process scheduling function                                     */ 
/* Input: process queue prs, timeslice timeslice                 */ 
/* Output: process status table                                 */     
void ProcessScheduling(queue<PCB>prs, double timeslice)
{
    cout << left;
    queue <PCB>ex_prs; // Ready queue, the processes in this queue are waiting to be executed 
    ex_prs.push(prs.front()); // The first process first enters the ready queue 
    prs.pop(); // Will The first process exits the process queue 
    double slice_start; // time slice start time 
    double slice_end = 0 ; // time slice end time 
    cout << " ------------------- ---------------Process Status Table-------------------------------- ---- " << endl;
    cout <<setw(15)<<"进程ID" << "|" << setw(15) << "到达时间" << "|" <<setw(15) << "总响应时间" << "|" << setw(15) << "时间片开始时间"<<"|" << setw(15) <<"endl;<<"|"<<"Time slice end time
    cout << "--------------------------------------------------------------------------------" << endl;
    while (!prs.empty()||!ex_prs.empty())
    {
        slice_start = slice_end; // Update the start and end time of the time 
        slice slice_end += timeslice; // Increase the time of a time slice each time 
        while (! prs.empty())
         // Before executing a process, it will be available here Processes entering the ready queue while the process occupies the CPU join the ready queue 
        {
             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;
            }
        }
        // If the ready queue is empty, continue the while loop 
        if (ex_prs.empty()) continue ;
         // If the remaining time is less than or equal to the time slice 
        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 =Place the team leader process at the tail//ex_prs.front();
            ex_prs.pop();
            ex_prs.push(tmp_pr);
        }

    }
    cout << "--------------------------------------------------------------------------------" << endl;
}
intmain ()
{
    queue <PCB> prs;
     // Total response time 
    double allres_time = 0 ;
     // Time slice 
    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 << "The time slice is: " << timeslice << endl;
    ProcessScheduling(prs, timeslice);
    cout << " Add process? (y/n): " ;
     char i;
    cin >> i;

    while (i-'y'==0)
    {
        PCB pr;
        cout << " Please enter the process ID: " ;
        cin >> pr.ID;
        cout << " Please enter the process arrival time: " ;
        cin >> pr.in_time;
        cout << " Please enter the process response time: " ;
        cin >> pr.res_time;
        cout << " Please enter the remaining time of the process: " ;
        cin >> pr.l_time;
        prs.push(pr);
        allres_time += pr.res_time;
        timeslice = allres_time / prs.size();
        cout << "The time slice is: " << timeslice << endl;
        ProcessScheduling(prs, timeslice);
        cout << " Continue adding processes? (y/n): " ;
        cin >> i;
    }
    return 0;
}

Results screenshot

Compiler

run the program

add a process

continue to add a process

Operating environment: Ubuntu

Additional related questions:

①C++ outputs a table

Solution: first use cout<<left to set the left alignment, and then use setw() to set the width of the text after that, such as:  cout << setw(12) << "Miles" , set the width of the subsequent Miles to 12 letters.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325342638&siteId=291194637