Experiment 1 design and implementation of process scheduling algorithm dynamic priority plus time slice rotation method for process scheduling

Experiment 1 design and implementation of process scheduling algorithm dynamic priority plus time slice rotation method for process scheduling
1. Content: design a simple process scheduling algorithm to simulate the process scheduling process in OS;
2. Requirements:
① The number of processes shall not be less than 5;
② There are three process scheduling algorithms:
The dynamic priority number plus time slice rotation method realizes process scheduling, and the priority number of each time slice is reduced by 3 (required);
Multi level queue rotation (optional);
High response ratio is preferred (optional);
Short process priority (optional);
③ Programming in C language, C + + or Java;
④ Show the process scheduling process when the program is running.
3. Steps:
① Design PCB and its data structure:
Number of process IDs: ID
Process PRIORITY: PRIORITY (the higher the PRIORITY, the higher the PRIORITY)
Time slice occupied by the process: CPUTIME, the value is increased by 1 every time the scheduling is obtained;
The process also needs to occupy a time slice: ALLTIME. This value will be reduced by 1 for each scheduling. Once the operation is completed, ALLTIME will be 0)
Process queue pointer: NEXT, used to queue PCB s
Process status: STATE (generally ready, can not be used)
② Design process ready queue and data structure;
③ Design the process scheduling algorithm and draw the program flow chart;
④ Design input data and output format;
Structure format: currently running process: 0
Current ready queue: 2, 1, 3, 4
⑤ Program on the computer and verify the results.
4. Prompt: process scheduling information can be generated statically during initialization or dynamically arrived during program operation;
It is assumed that there are 5 processes in the system before scheduling, and their initial states are as follows:
ID 0 1 2 3 4
PRIORITY 9 38 30 29 0 can we consider using arrays or linked lists
CPUTIME 0 0 0 0 0
ALLTIME 3 2 6 3 4
STATE ready ready ready ready ready
① Scheduling operation in time slice;
② Each time when ALLTIME is not 0 and the process with the largest PRIORITY runs a time slice;
③ After the above process runs, its priority number is reduced by 3, and then its CPUTIME and ALLTIME are modified. Repeat ② and ③
④ Until ALLTIME of all processes becomes 0.
5. Write the experiment report
① Experimental topics;
② Test requirements;
③ Data structure and description used in the program;
④ Idea description of scheduling algorithm;
⑤ Algorithm flow chart;
○ 6 procedure list;
○ 7 analysis of test data and experimental results: output the scheduling sequence, calculate and compare the average turnaround time of processes under different algorithms.

The above are the reporting requirements.
*
This article is the original article of the blogger and cannot be reproduced without the permission of the blogger.
Copyright is owned by Chen Bochao and published on BLOG for the first time on November 30, 2021
The original articles on this BLOG shall not be used for commercial purposes without my permission. Please indicate the source after reprinting with permission, otherwise you reserve the right to investigate legal responsibility.
*
Dynamic priority number plus time slice rotation method to realize process scheduling:
Code in header file PCB.h:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class pcb
{
public:
    int ID;                                 //Number of process IDs
    int PRIORITY;                           //Number of process priorities
    int CPUTIME;                            //The process has consumed time slices
    int ALLTIME;                            //The process still needs time slice
public:
    int setPRIORITY(int i)                  //Set priority series function
    {
        return PRIORITY -= i;
    }

    int setCPUTIME(int i)                   //Set elapsed time slice function
    {
        return CPUTIME += i;
    }

    int setALLTIME(int i)                   //Set the time slice function
    {
        return ALLTIME -= i;
    }
};

Source file process scheduling dynamic priority and time wheel. cpp Code:

#include "PCB.h"

bool compare(pcb a, pcb b)                              //A custom comparison function is used to help sort the sort function
{
    return a.PRIORITY > b.PRIORITY;
}

int main()
{
    double time = 0;
    double current_time = 0;
    vector<pcb> a = {                                   //Process scheduling information initialization static generation array container
                    {0, 9, 0, 3,},{1, 38, 0, 2,},{2, 30, 0, 6,},{3, 29, 0, 3,},{4, 0, 0, 4,},
                    };
    while (a.size() > 0)
    {
        sort(a.begin(), a.end(), compare);              //Call sort function
        if (a[0].ALLTIME != 0)
        {
            current_time++;
            cout << endl;
            cout << "Running process:";
            cout << a[0].ID << endl;
            a[0].setPRIORITY(3);                        //Priority minus 3
            a[0].setCPUTIME(1);                         //The time slice occupied by the process is increased by 1
            a[0].setALLTIME(1);                         //Process time slice minus 1
            cout << "Current ready queue ID: ";
            for (int i = 1; i < a.size(); i++)          //Output ready queue
            {
                cout << a[i].ID << " ";
            }
            if (a[0].ALLTIME == 0)                      //Judge whether the operation is completed. If it is completed, exit the container
            {
                time += current_time;
                a.erase(a.begin());
            }
            cout << endl;
        }
    }
    cout << "Average turnaround time:" << time / a.capacity() << endl;
    return 0;
}

The operation screenshot is as follows:

Code words are not easy. Poor students, bosses will reward a penny for support. Thank you.
Predecessors plant trees and posterity enjoy the cool. If any younger and younger students see it, they also hope to praise and support it. It's really not easy for seniors to do this

Tags: C++ Algorithm Operating System

Posted on Tue, 30 Nov 2021 05:42:07 -0500 by Codewarrior123