目录
二、优先级调度(Priority Scheduling, PS)
一、轮转调度(Round Robin, RR)
轮转法的基本原理
在轮转(RR)法中,系统根据FCFS策略,将所有的就绪进程排成一个就绪队列,并可设置每隔一定时间间隔(如 30ms)即产生一次中断,激活系统中的进程调度程序,完成一次调度,将CPU分配给队首进程,令其执行。当该进程的时间片耗尽或运行完毕时,系统再次将 CPU分配给新的队首进程(或新到达的紧迫进程)。由此,可保证就绪队列中的所有进程在一个确定的时间段内,都能够获得一次 CPU 执行。
进程切换时机
在RR调度算法中,应在何时进行进程的切换,可分为两种情况:①若一个时间片尚未用完,正在运行的进程便已经完成,就立即激活调度程序,将它从就绪队列中删除,再调度就绪队列中队首的进程运行,并启动一个新的时间片。②在一个时间片用完时,计时器中断处理程序被激活。如果进程尚未运行完毕,调度程序将把它送往就绪队列的末尾
本次实验的题目
代码示例
通过上面描述想必你应该明白了轮转调度的算法,但可能不知道如何实现,那下面就来点代码对照着思路学习吧!
我们先创建一个process进程类
const int Time_slice = 2;
const int Process = 3;
class PCB {
public:
string name; //进程名称
int process_priority; //进程优先级
double cputime; //使用cpu时间
double needtime; //还需要cpu时间
int state; //进程状态
int cnt; //已经运行的轮数
int round; //被分配时间片的数量
int index; //下一个指针
int count; //进程数量
bool is_finish = false;
public:
enum State {
F, R, W //完成,运行,等待
};
PCB set_pcb_time(string name, double needtime) {
PCB p;
p.name = name;
p.needtime = needtime;
p.cputime = 0;
p.state=State::W;
p.cnt = 0;
p.round = Time_slice;
return p;
}
PCB set_pcb_pri(string name, double needtime) {
PCB p;
p.name = name;
p.needtime = needtime;
p.cputime = 0;
p.state = State::W;
p.cnt = 0;
p.round = Time_slice;
p.process_priority = 50 - needtime;
return p;
}
void time_slice();
void Priority_algorithm();
void addprocess_time(string name,double time);
void addprocess_priority(string name, double time);
void show_time();
void show_pri();
};
deque<PCB> q; //就绪队列
deque<PCB> ans; //完成队列
轮转调度实现
//轮转调度
void PCB::time_slice() {
double time;
string name;
cout << "input # to end" << endl;
while (1) {
cout << "input name and needtime" << endl;
cin >> name ;
if (name == "#")break;
cin >> time;
addprocess_time(name,time);
}
//进行调度算法
while (true) {
auto it = q.front();
it.state = State::R;
show_time();
if (ans.size() == count)break;
q.pop_front();
if (it.needtime > Time_slice) {
it.needtime -= Time_slice;
it.cputime += Time_slice;
it.state = State::W;
}
else {
it.cputime += it.needtime;
it.needtime = 0;
it.state = State::F;
if (!it.is_finish)ans.push_back(it);
it.is_finish = true;
}
it.cnt++;
q.push_back(it);
}
}
void PCB::addprocess_time(string name,double time) {
PCB p;
p = set_pcb_time(name, time);
q.push_back(p);
count++;
}
是不是比较简单啊,看完之后自己试试吧!!
二、优先级调度(Priority Scheduling, PS)
核心思想
根据进程的 优先级(Priority) 分配CPU,优先级高的进程优先执行。可分为:
-
非抢占式:进程运行到结束或主动让出CPU。
-
抢占式:高优先级进程到达时立即抢占当前进程。
我们这里演示的是非抢占式的优先级调度。优先级调度顾名思义就是优先级高的先进行调度(优先处理),当我们每一次进行完调度之后都重新按照优先级进行排序,优先处理优先级高的!!
代码示例
bool cmp(const PCB& a, const PCB& b) {
if (a.state != PCB::F && b.state == PCB::F) return true;
if (a.state == PCB::F && b.state != PCB::F) return false;
if (a.state != PCB::F && b.state != PCB::F)
return a.process_priority > b.process_priority;
return false;
}
//优先级调度
void PCB::Priority_algorithm() {
double time;
string name;
cout << "input # to end" << endl;
while (1) {
cout << "input name and needtime" << endl;
cin >> name;
if (name == "#")break;
cin >> time;
addprocess_priority(name, time);
}
//优先级调度算法
while (true) {
sort(q.begin(), q.end(), cmp);
auto it = q.front();
it.state = State::R;
show_pri();
if (ans.size() == count)break;
q.pop_front();
if (it.needtime > Time_slice) {
it.needtime -= Time_slice;
it.cputime += Time_slice;
it.state = State::W;
it.process_priority -= Process;
}
else {
it.cputime += it.needtime;
it.process_priority -= Process;
it.needtime = 0;
it.state = State::F;
if (!it.is_finish) {
ans.push_back(it);
it.is_finish = true;
}
}
it.cnt++;
q.push_back(it);
}
}
void PCB::addprocess_priority(string name, double time) {
PCB p;
p = set_pcb_pri(name, time);
q.push_back(p);
count++;
}
void PCB::show_pri() {
cout << "name cputime needtime count pri state" << endl;
for (auto i = q.begin(); i != q.end(); i++) {
cout << i->name << " " << i->cputime << " " << i->needtime << " " << i->cnt << " " << i->process_priority << " ";
if (i == q.begin() && i->state != State::F) {
cout << "R" << endl;
continue;
}
if (i->state == State::F)cout << "F" << endl;
if (i->state == State::W)cout << "W" << endl;
}
cout << "就绪队列:";
for (auto i = q.begin() + 1; i != q.end(); i++) {
if (i->state != State::F)
cout << i->name << ",";
}
cout << endl;
cout << "完成队列:";
for (auto i = ans.begin(); i != ans.end(); i++) {
cout << i->name << ",";
}
cout << endl;
cout << endl;
}
三、全部代码
#include <iostream>
#include <string>
#include <queue>
#include <algorithm>
using namespace std;
const int Time_slice = 2;
const int Process = 3;
class PCB {
public:
string name; //进程名称
int process_priority; //进程优先级
double cputime; //使用cpu时间
double needtime; //还需要cpu时间
int state; //进程状态
int cnt; //已经运行的轮数
int round; //被分配时间片的数量
int index; //下一个指针
int count; //进程数量
bool is_finish = false;
public:
enum State {
F, R, W //完成,运行,等待
};
PCB set_pcb_time(string name, double needtime) {
PCB p;
p.name = name;
p.needtime = needtime;
p.cputime = 0;
p.state=State::W;
p.cnt = 0;
p.round = Time_slice;
return p;
}
PCB set_pcb_pri(string name, double needtime) {
PCB p;
p.name = name;
p.needtime = needtime;
p.cputime = 0;
p.state = State::W;
p.cnt = 0;
p.round = Time_slice;
p.process_priority = 50 - needtime;
return p;
}
void time_slice();
void Priority_algorithm();
void addprocess_time(string name,double time);
void addprocess_priority(string name, double time);
void show_time();
void show_pri();
};
deque<PCB> q; //就绪队列
deque<PCB> ans; //完成队列
//轮转调度
void PCB::time_slice() {
double time;
string name;
cout << "input # to end" << endl;
while (1) {
cout << "input name and needtime" << endl;
cin >> name ;
if (name == "#")break;
cin >> time;
addprocess_time(name,time);
}
//进行调度算法
while (true) {
auto it = q.front();
it.state = State::R;
show_time();
if (ans.size() == count)break;
q.pop_front();
if (it.needtime > Time_slice) {
it.needtime -= Time_slice;
it.cputime += Time_slice;
it.state = State::W;
}
else {
it.cputime += it.needtime;
it.needtime = 0;
it.state = State::F;
if (!it.is_finish)ans.push_back(it);
it.is_finish = true;
}
it.cnt++;
q.push_back(it);
}
}
bool cmp(const PCB& a, const PCB& b) {
if (a.state != PCB::F && b.state == PCB::F) return true;
if (a.state == PCB::F && b.state != PCB::F) return false;
if (a.state != PCB::F && b.state != PCB::F)
return a.process_priority > b.process_priority;
return false;
}
//优先级调度
void PCB::Priority_algorithm() {
double time;
string name;
cout << "input # to end" << endl;
while (1) {
cout << "input name and needtime" << endl;
cin >> name;
if (name == "#")break;
cin >> time;
addprocess_priority(name, time);
}
//优先级调度算法
while (true) {
sort(q.begin(), q.end(), cmp);
auto it = q.front();
it.state = State::R;
show_pri();
if (ans.size() == count)break;
q.pop_front();
if (it.needtime > Time_slice) {
it.needtime -= Time_slice;
it.cputime += Time_slice;
it.state = State::W;
it.process_priority -= Process;
}
else {
it.cputime += it.needtime;
it.process_priority -= Process;
it.needtime = 0;
it.state = State::F;
if (!it.is_finish) {
ans.push_back(it);
it.is_finish = true;
}
}
it.cnt++;
q.push_back(it);
}
}
void PCB::addprocess_priority(string name, double time) {
PCB p;
p = set_pcb_pri(name, time);
q.push_back(p);
count++;
}
void PCB::addprocess_time(string name,double time) {
PCB p;
p = set_pcb_time(name, time);
q.push_back(p);
count++;
}
void PCB::show_time() {
cout << "name cputime needtime count round state" << endl;
for (auto i = q.begin(); i != q.end(); i++) {
cout << i->name << " " << i->cputime << " " <<i->needtime<< " " << i->cnt << " " << i->round << " ";
if (i == q.begin()&&i->state!=State::F) {
cout << "R" << endl;
continue;
}
if (i->state == State::F)cout << "F" << endl;
if (i->state == State::W)cout << "W" << endl;
}
cout << "就绪队列:";
for (auto i = q.begin() + 1; i != q.end(); i++) {
if(i->state!=State::F)
cout << i->name << ",";
}
cout << endl;
cout << "完成队列:";
for (auto i = ans.begin(); i != ans.end(); i++) {
cout << i->name << ",";
}
cout << endl;
cout << endl;
}
void PCB::show_pri() {
cout << "name cputime needtime count pri state" << endl;
for (auto i = q.begin(); i != q.end(); i++) {
cout << i->name << " " << i->cputime << " " << i->needtime << " " << i->cnt << " " << i->process_priority << " ";
if (i == q.begin() && i->state != State::F) {
cout << "R" << endl;
continue;
}
if (i->state == State::F)cout << "F" << endl;
if (i->state == State::W)cout << "W" << endl;
}
cout << "就绪队列:";
for (auto i = q.begin() + 1; i != q.end(); i++) {
if (i->state != State::F)
cout << i->name << ",";
}
cout << endl;
cout << "完成队列:";
for (auto i = ans.begin(); i != ans.end(); i++) {
cout << i->name << ",";
}
cout << endl;
cout << endl;
}
PCB pcb;
int main() {
bool t = true;
int n;
while (t) {
cout << " -------------------------------------------" << endl;
cout << "--------------1.时间片流转法-----------------" << endl;
cout << "--------------2.优先数算法-------------------" << endl;
cout << "--------------------------------------------" << endl;
cout << "请输入进程算法选项" << endl;
cin >> n;
switch(n){
case 1:pcb.time_slice();
break;
case 2:pcb.Priority_algorithm();
break;
default:
t = false;
break;
}
}
return 0;
}