#include<iostream>
#include<vector>
#include <ctime>
#include <queue>
using namespace std;
int n;
int method;
class random //取随机数
{
public:
static inline int getRandom(const int min, const int max, int match)
{
srand(time(0) + match);
return rand() % (max - min - 1) + min;
}
};
class PCB
{
int id;
int time;
string state;
int round_time;
int priority;
public:
int get_priority()
{
return priority;
}
int get_time()
{
return time;
}
int get_id()
{
return id;
}
int get_round_time()
{
return round_time;
}
string get_state()
{
return this->state;
}
void set_id(int n)
{
this->id = n;
}
void set_round_time(int n)
{
this->round_time = n;
}
void set_priority(int n)
{
this->priority = n;
}
void set_time(int random)
{
this->time = random;
}
void set_state(string random)
{
this->state = random;
}
};
bool operator > (PCB p1, PCB p2) {
return p1.get_priority() > p2.get_priority();
}
bool operator < (PCB p1, PCB p2) {
return p1.get_priority() < p2.get_priority();
}
void show(priority_queue<PCB, vector<PCB>, less<PCB> >ready_queue, queue<PCB>running,queue<PCB>finish)
{
queue<PCB>ready;
while(!ready_queue.empty())
{
ready.push(ready_queue.top());
ready_queue.pop();
}
cout << "就绪队列为:";
while (!ready.empty())
{
cout << ready.front().get_id() << " ";
ready.pop();
}
cout << endl;
cout << "运行队列为:";
while (!running.empty())
{
cout << running.front().get_id() << " ";
running.pop();
}
cout << endl;
cout << "完成队列为:";
while (!finish.empty())
{
cout << finish.front().get_id() << " ";
finish.pop();
}
cout << endl;
}
void show(queue<PCB>ready, queue<PCB>running, queue<PCB>finish)
{
cout << "就绪队列为:";
while (!ready.empty())
{
cout << ready.front().get_id() << " ";
ready.pop();
}
cout << endl;
cout << "运行队列为:";
while (!running.empty())
{
cout << running.front().get_id() << " ";
running.pop();
}
cout << endl;
cout << "完成队列为:";
while (!finish.empty())
{
cout << finish.front().get_id() << " ";
finish.pop();
}
cout << endl;
}
int main()
{
cout << "请输入进程数目" << endl;
cin >> n;
cout << endl;
cout << "请选择调度方法(1为优先权法,2为轮转法)" << endl;
cin >> method;
cout << endl;
if (method == 1)
{
priority_queue<PCB, vector<PCB>, less<PCB> >ready_queue;
queue<PCB>running;
queue<PCB>finish;
for (int i = 1; i <= n; ++i)
{
PCB pcb;
pcb.set_id(i);
pcb.set_state("Ready");
pcb.set_time(random::getRandom(1, 20, i));
pcb.set_priority(random::getRandom(70, 120, i));
cout << "初始化进程 " << pcb.get_id() << '\t'
<< " 状态为: " << pcb.get_state()
<< " 所需时间片数为: " << pcb.get_time()
<< " 优先权为: " << pcb.get_priority() << endl;
ready_queue.push(pcb);
}
while (!ready_queue.empty())
{
PCB temp = ready_queue.top();
running.push(temp);
ready_queue.pop();
show(ready_queue, running, finish);
temp.set_priority(temp.get_priority() - 3);
temp.set_time(temp.get_time() - 1);
if (temp.get_time() == 0)
{
finish.push(temp);
running.pop();
show(ready_queue, running, finish);
}
else
{
running.pop();
ready_queue.push(temp);
}
}
}
else
{
queue<PCB>ready;
queue<PCB>running;
queue<PCB>finish;
for (int i = 0; i < n; i++)
{
PCB pcb;
pcb.set_id(i+1);
pcb.set_time(random::getRandom(10, 20, i));
pcb.set_round_time(random::getRandom(1, 5, i));
cout << "初始化进程 " << pcb.get_id() << '\t'
<< " 状态为: " << pcb.get_state()
<< " 所需时间片数为: " << pcb.get_time()
<< " 轮转时间片为: " << pcb.get_round_time() << endl;
ready.push(pcb);
}
while (!ready.empty())
{
PCB temp = ready.front();
ready.pop();
int temp_round_time = temp.get_round_time();
if (temp_round_time >= temp.get_time())
{
finish.push(temp);
show(ready, running, finish);
continue;
}
else
{
running.push(temp);
temp.set_time(temp.get_time() - temp_round_time);
ready.push(temp);
running.pop();
show(ready, running, finish);
}
}
}
return 0;
}
操作系统进程控制实验报告代码
于 2022-04-13 16:27:31 首次发布