用vc++编写多级反馈队列代码
时间: 2024-05-11 10:20:24 浏览: 14
以下是用VC++编写的多级反馈队列代码:
```
#include<iostream>
using namespace std;
const int MAX = 100; // 队列最大长度
const int LEVEL = 3; // 多级反馈队列的级数
struct PCB {
int id; // 进程ID
int arrivalTime; // 到达时间
int needTime; // 运行所需时间
int priority; // 优先级
int level; // 所在级数
int runTime; // 已运行时间
};
class Queue {
private:
PCB queue[MAX]; // 队列数组
int front, rear; // 队头和队尾指针
public:
Queue() { front = rear = 0; } // 构造函数
bool isEmpty() { return front == rear; } // 判断队列是否为空
void enQueue(PCB p); // 入队
PCB deQueue(); // 出队
PCB getFront(); // 获取队头元素
};
void Queue::enQueue(PCB p) {
if ((rear + 1) % MAX == front) { // 队列已满
cout << "The queue is full!" << endl;
return;
}
queue[rear] = p;
rear = (rear + 1) % MAX;
}
PCB Queue::deQueue() {
if (isEmpty()) { // 队列为空
cout << "The queue is empty!" << endl;
PCB p;
p.id = -1;
return p;
}
PCB p = queue[front];
front = (front + 1) % MAX;
return p;
}
PCB Queue::getFront() {
if (isEmpty()) { // 队列为空
cout << "The queue is empty!" << endl;
PCB p;
p.id = -1;
return p;
}
return queue[front];
}
int main() {
Queue q[LEVEL]; // LEVEL级队列数组
int timeSlice[LEVEL] = { 4, 8, 16 }; // 每级队列的时间片
int currentTime = 0; // 当前时间
// 输入进程信息
int n;
cout << "Please input the number of processes: ";
cin >> n;
PCB p[MAX];
cout << "Please input the information of each process(ID, arrival time, need time, priority): " << endl;
for (int i = 0; i < n; i++) {
cin >> p[i].id >> p[i].arrivalTime >> p[i].needTime >> p[i].priority;
p[i].runTime = 0;
p[i].level = 0;
}
// 处理进程
int finished = 0; // 已完成进程数
while (finished < n) { // 还有进程未处理完
// 将到达时间在当前时间之前的进程加入第0级队列中
for (int i = 0; i < n; i++) {
if (p[i].arrivalTime <= currentTime && p[i].runTime == 0) {
q[0].enQueue(p[i]);
}
}
// 处理第0级队列中的进程
int i = 0;
while (!q[i].isEmpty()) {
PCB current = q[i].getFront();
q[i].deQueue();
// 运行时间小于时间片,进程运行结束
if (current.needTime - current.runTime <= timeSlice[i]) {
currentTime += current.needTime - current.runTime;
current.runTime = current.needTime;
cout << "Process " << current.id << " finished at time " << currentTime << endl;
finished++;
}
// 运行时间大于时间片,进程移动到下一级队列末尾
else {
currentTime += timeSlice[i];
current.runTime += timeSlice[i];
current.level = (i + 1) % LEVEL;
q[current.level].enQueue(current);
}
}
// 队列为空,当前时间自增1
if (q[i].isEmpty()) {
currentTime++;
}
}
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)