Process scheduling experiment

The experiment purpose

In multiprogramming, it is often the case that several processes are in a ready state at the same time, and a strategy must be followed to determine which process takes precedence over the processor. This causes process scheduling. This experiment simulates the processor scheduling problem under the condition of single processor to deepen the understanding of process scheduling.

The experiment content,

  1. Priority method – dynamic priority
  2. Rotary method

The flow chart

The experimental requirements

  1. The value range of various random numbers generated is limited, such as the CPU time required is limited between 1 and 20.
  2. The number of processes n is not too large. Generally, 4 to 8 processes are required
  3. Use dynamic data structures
  4. Independent programming
  5. Two scheduling algorithms

The results

The experimental code

#include<iostream> #include<queue> #include<vector> using namespace std; int n; const int N = 10; struct PCB { string name; int time; int priority; int status; int runtime; int lefttime; }; struct cmp { bool operator()(PCB a, PCB b) { if (a.priority == b.priority) { return a.time > b.time; } return a.priority < b.priority; }}; // priority_queue<PCB, vector<PCB>, CMP > q1; //ready queue<PCB> q3; //finish void init_priority() { PCB t; for (int i = 1; i <= n; i++) { t.name = 'a' + i - 1; t.priority = (rand() % 20) + 50; t.time = (rand() % 20) + 1; t.status = -1; t.lefttime = t.time; t.runtime = 0; q1.push(t); } } void print_priority(priority_queue<PCB, vector<PCB>, cmp> q) { while (! q.empty()) { PCB t = q.top(); cout << t.name << '\t' << t.time << '\t' << t.priority << '\t' << t.status << '\t' << t.runtime + 1 << '\t' << '\t' << t.lefttime - 1 << endl; q.pop(); }} void print_finish1(queue<PCB> q) {cout <<" Cout < < "name" < < < < '\ t' "time" < < < < '\ t' "priority" < < < < '\ t' "state" < < < < '\ t' "running time" < < < < '\ t' "time remaining" < < endl; while (! q.empty()) { PCB t = q.front(); cout << t.name << '\t' << t.time << '\t' << t.priority << '\t' << t.status << '\t' << t.runtime << '\t' << '\t' << t.lefttime << endl; q.pop(); } } void run_priority() { while (! q1.empty()) { print_priority(q1); PCB t = q1.top(); t.priority -= 3; t.lefttime -= 1; t.runtime += 1; t.status = 0; if (t.lefttime <= 0) { t.status = 1; q3.push(t); // Insert to finish queue Q1.pop (); } else { t.status = -1; q1.pop(); q1.push(t); // Insert into ready queue}} cout << "All processes are finished!" << endl; cout << endl << endl << endl; } // queue<PCB> q2; void init_timeturn() { PCB t; for (int i = 1; i <= n; i++) { t.name = 'a' + i - 1; t.time = (rand() % 20) + 1; t.status = -1; t.lefttime = t.time; t.runtime = 0; q2.push(t); } } void print_timeturn(queue<PCB> q) { while (! q.empty()) { PCB t = q.front(); cout << t.name << '\t' << t.time << '\t' << t.status << '\t' << t.runtime + 1 << '\t' << '\t' << t.lefttime - 1 << endl;  q.pop(); }} void print_finish2(queue<PCB> q) {cout <<" Cout < < "name" < < < < '\ t' "time" < < < < '\ t' "state" < < < < '\ t' "running time" < < < < '\ t' "time remaining" < < endl; while (! q.empty()) { PCB t = q.front(); cout << t.name << '\t' << t.time << '\t' << t.status << '\t' << t.runtime << '\t' << '\t' << t.lefttime << endl; q.pop(); } } void run_timeturn() { while (! q2.empty()) { print_timeturn(q2); PCB t = q2.front(); t.runtime += 1; t.lefttime -= 1; t.status = 0; if (t.lefttime <= 0) { t.status = 1; q3.push(t); q2.pop(); } else { t.status = -1; q2.push(t); q2.pop(); }} cout << "All processes completed!" << endl; cout << endl << endl << endl; } int main() { int c; Cout << "Select scheduling algorithm: 1. priority scheduling 2. time slice rotation" << endl; cin >> c; Cout << "Input process number:" << endl; cin >> n; cout << endl; Cout << "Status bar '-1' for ready, '1' for finished" << endl << endl; if (c == 1) { init_priority(); Cout << "The ready queue process is as follows:" << endl; Cout < < "name" < < < < '\ t' "time" < < < < '\ t' "priority" < < < < '\ t' "state" < < < < '\ t' "running time" < < < < '\ t' "time remaining" < < endl; run_priority(); print_finish1(q3); } else if (c == 2) { init_timeturn(); Cout << "The ready queue process is as follows:" << endl; Cout < < "name" < < < < '\ t' "time" < < < < '\ t' "state" < < < < '\ t' "running time" < < < < '\ t' "time remaining" < < endl; run_timeturn(); print_finish2(q3); } else {cout << "input error, automatic exit! : " << endl; } return 0; }Copy the code