进程调度实验
实验目的
多道程序设计中,经常是若干个进程同时处于就绪状态,必须依照某种策略来决定那个进程优先占有处理机。因而引起进程调度。本实验模拟在单处理机情况下的处理机调度问题,加深对进程调度的理解。
实验内容
- 优先权法-动态优先权
- 轮转法
流程图
实验要求
- 产生的各种随机数的取值范围加以限制,如所需的CPU时间限制在1~20之间。
- 进程数n不要太大通常取4~8个
- 使用动态数据结构
- 独立编程
- 两种调度算法
运行结果
实验代码
#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 << "完成队列过程如下: " << endl;
cout << "名字" << '\t' << "时间" << '\t' << " 优先级" << '\t' << "状态" << '\t' << "运行时间" << '\t' << "剩余时间" << 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); //插入到finish队列
q1.pop();
}
else {
t.status = -1;
q1.pop();
q1.push(t); //插入到就绪队列
}
}
cout << "所有进程均已执行完毕!" << 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 << "完成队列过程如下: " << endl;
cout << "名字" << '\t' << "时间" << '\t' << "状态" << '\t' << "运行时间" << '\t' << "剩余时间" << 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 << "所有进程均已执行完毕!" << endl;
cout << endl << endl << endl;
}
int main() {
int c;
cout << "选择调度算法 : 1.优先级调度 2.时间片轮转 " << endl;
cin >> c;
cout << "输入进程个数 : " << endl;
cin >> n;
cout << endl;
cout << "状态栏 ‘-1’代表就绪, ‘1’代表完成" << endl << endl;
if (c == 1) {
init_priority();
cout << "就绪队列过程如下: " << endl;
cout << "名字" << '\t' << "时间" << '\t' << "优先级" << '\t' << "状态" << '\t' << "运行时间" << '\t' << "剩余时间" << endl;
run_priority();
print_finish1(q3);
}
else if (c == 2) {
init_timeturn();
cout << "就绪队列过程如下: " << endl;
cout << "名字" << '\t' << "时间" << '\t' << "状态" << '\t' << "运行时间" << '\t' << "剩余时间" << endl;
run_timeturn();
print_finish2(q3);
}
else {
cout << "输入错误 ,自动退出 ! : " << endl;
}
return 0;
}