代码:
#include<iostream>
#include<string>
#include<fstream>
#include<vector>
#include<algorithm>
#include<cstdlib>
#include<ctime>
using namespace std;
//初始化进程池
//初始化就绪队列
//RR算法
struct PCB {
string name;
int atime; //到达时间
int rtime; //估计运行时间
char status; //进程状态 R是就绪,W是等待,C是结束
PCB* next; //指针
PCB(){
this->name = "";
this->atime = -1;
this->rtime = -1;
this->status = NULL;
this->next = NULL;
}
PCB(string name, int atime, int rtime, char status, PCB* next=NULL) {
this->name = name;
this->atime = atime;
this->rtime = rtime;
this->status = status;
this->next = next;
}
//复制构造
PCB(const PCB& pcb) {
this->name = pcb.name;
this->atime = pcb.atime;
this->rtime = pcb.rtime;
this->status = pcb.status;
}
void show() {
cout << name << " " << atime << " " << rtime << " " << status << endl;
}
};
bool cmp(PCB& a, PCB& b) {
return a.atime < b.atime;
}
//打印队列所有进程的情况
void show_status(PCB* Q_head) {
if (!Q_head)return;
PCB* node = Q_head;
node->show();
while (node->next != Q_head) {
node = node->next;
node->show();
}
}
//初始化进程池
void initPool(vector<PCB>& PCBPool, string fileName) {
ifstream inFile(fileName);
if (!inFile.is_open()) {
cout << "文件打开失败!
";
return;
}
string name;
int atime; //到达时间
int rtime; //估计运行时间
char status; //进程状态
//读入文件数据
while (inFile >> name >> atime >> rtime >> status) {
PCB pcb(name, atime, rtime, status);
PCBPool.push_back(pcb);
}
//按照到达时间进行升序排序
sort(PCBPool.begin(), PCBPool.end(),cmp);
cout << "初始化成功!
";
inFile.close();
return;
}
//头结点移到尾部
void headToTail(PCB*& Q_head, PCB*& Q_tail) {
if (Q_head) {
Q_tail = Q_head;
Q_head = Q_tail->next = Q_head->next;
}
}
//删除头结点
void delHead(PCB*& Q_head, PCB*& Q_tail) {
if (Q_head) {
PCB* node = Q_head;
if (Q_head == Q_tail) {
Q_head = Q_tail = NULL;
}
else {
Q_head = Q_tail->next = Q_head->next;
}
delete node;
}
}
//扫描并添加新进程
void scanAndAdd(PCB*& Q_head,PCB*& Q_tail, vector<PCB>& PCBPool,int time) {
while (PCBPool.size()&&PCBPool[0].atime == time) {
PCB * node = new PCB(PCBPool[0]);
//从进程池中删除该进程
PCBPool.erase(PCBPool.begin());
//如果队列为空
if (Q_head == NULL) {
node->status = 'R';
Q_head = Q_tail = node;
Q_tail->next = Q_head;
}
else{
node->status = 'W';
//插入队尾
Q_tail->next = node;
//尾指针后移
Q_tail = Q_tail->next;
//最后一个结点指向头结点
Q_tail->next = Q_head;
}
}
}
//时间片轮转算法
void RR(PCB*& Q_head, PCB*& Q_tail, vector<PCB>& PCBPool) {
//时间一直流动,在每一个时间步里,如果就绪队列不为空,则先处理就绪队列的进程再扫描添加新进程,再把没完成的放到队尾;如果为空,则直接扫描添加
int time = 0;
while (!Q_head == NULL || !PCBPool.empty()) {
cout << "===========================
";
cout << "时间:" << time << endl;
//添加进程
scanAndAdd(Q_head, Q_tail, PCBPool, time);
bool notRun = true;
while (notRun&&Q_head) {
if (Q_head->status == 'R') {
cout << "当前正在运行的进程是:" << Q_head->name << endl;
Q_head->rtime--;
notRun = false; //运行过了
//运行完
if (Q_head->rtime == 0) {
Q_head->status = 'C';
}
//还没运行完
else {
Q_head->status = 'W';
}
show_status(Q_head);
}
else if (Q_head->status == 'W') {
headToTail(Q_head, Q_tail);
Q_head->status = 'R';
}
else if (Q_head->status == 'C') {
delHead(Q_head, Q_tail);
if (Q_head) {
Q_head->status = 'R';
}
}
}
time++;
}
}
//生成随机数据
void createData(string fileName, int n) {
ofstream outFile(fileName);
if (!outFile.is_open()) {
cout << "文件打开失败!" << endl;
return;
}
srand((int)time(0));
string name;
int atime; //到达时间
int rtime; //估计运行时间
char status='W'; //进程状态
for (int i = 0; i < n; i++) {
name = "P" + to_string(i);
atime = rand() % n;
rtime = rand() % 10 + 1;
outFile << name << " " << atime << " " << rtime << " "<<status << endl;
}
outFile.close();
}
int main() {
createData("test.txt", 4);
vector<PCB> pool;
initPool(pool,"test.txt");
PCB* Q_head = NULL;
PCB* Q_tail = NULL;
RR(Q_head, Q_tail, pool);
system("pause");
}