================================================================================
多级反馈队列调度算法模拟器
================================================================================
请输入进程数量: 4
请依次输入每个进程的信息(进程ID 到达时间 执行时间):
可用设备类型:
1. Chrome浏览器
2. Word文档
3. Excel表格
4. 音乐播放器
5. 视频播放器
6. 图片编辑器
7. 终端窗口
8. 文件管理器
9. 邮件客户端
10. PDF阅读器
11. 代码编辑器
12. 系统监控器
进程 1 (PID 到达时间 执行时间): 1 0 3
进程 2 (PID 到达时间 执行时间): 2 4 2
进程 3 (PID 到达时间 执行时间): 3 6 4
进程 4 (PID 到达时间 执行时间): 4 8 1
请输入队列数量: 3
请依次输入每个队列的时间片大小:
队列 0 时间片: 1
队列 1 时间片: 2
队列 2 时间片: 4
输入信息汇总:
+-----+------------------+----------+----------+
| PID | 进程名称 | 到达时间 | 执行时间 |
+-----+------------------+----------+----------+
| 1 | Word文档 | 0 | 3 |
| 2 | Excel表格 | 4 | 2 |
| 3 | 音乐播放器 | 6 | 4 |
| 4 | 视频播放器 | 8 | 1 |
+-----+------------------+----------+----------+
队列信息:
+----------+------------+----------------+
| 队列级别 | 时间片大小 | 优先级描述 |
+----------+------------+----------------+
| 0 | 1 | 最高优先级 |
| 1 | 2 | 高优先级 |
| 2 | 4 | 最低优先级 |
+----------+------------+----------------+
模拟即将开始...
开始执行多级反馈队列调度...
[到达] 时间 0: 进程 [Word文档] 到达系统,加入队列 0 (最高优先级)
+------------------------------------------------------------------------+
| 当前时间: 0 |
+--------+----------------+----------+-----------------------------------+
| 队列ID | 优先级描述 | 时间片 | 队列中的进程 |
+--------+----------------+----------+-----------------------------------+
| 0 | 最高优先级 | 1 | Word文档 |
| 1 | 高优先级 | 2 | 空队列 |
| 2 | 最低优先级 | 4 | 空队列 |
+--------+----------------+----------+-----------------------------------+
进程状态表:
+-----+------------------+----------+----------+----------+----------+----------+
| PID | 进程名称 | 到达时间 | 执行时间 | 剩余时间 | 队列级别 | 状态 |
+-----+------------------+----------+----------+----------+----------+----------+
| 1 | Word文档 | 0 | 3 | 3 | 0 | 就绪中 |
| 2 | Excel表格 | 4 | 2 | 2 | 0 | 未到达 |
| 3 | 音乐播放器 | 6 | 4 | 4 | 0 | 未到达 |
| 4 | 视频播放器 | 8 | 1 | 1 | 0 | 未到达 |
+-----+------------------+----------+----------+----------+----------+----------+
[执行] 时间 0: 执行进程 [Word文档] (队列 0: 最高优先级), 执行 1 单位时间
[降级] 时间 1: 进程 [Word文档] 从队列 0 (最高优先级) 降级到队列 1 (高优先级)
按回车键继续...
+------------------------------------------------------------------------+
| 当前时间: 1 |
+--------+----------------+----------+-----------------------------------+
| 队列ID | 优先级描述 | 时间片 | 队列中的进程 |
+--------+----------------+----------+-----------------------------------+
| 0 | 最高优先级 | 1 | 空队列 |
| 1 | 高优先级 | 2 | Word文档 |
| 2 | 最低优先级 | 4 | 空队列 |
+--------+----------------+----------+-----------------------------------+
进程状态表:
+-----+------------------+----------+----------+----------+----------+----------+
| PID | 进程名称 | 到达时间 | 执行时间 | 剩余时间 | 队列级别 | 状态 |
+-----+------------------+----------+----------+----------+----------+----------+
| 1 | Word文档 | 0 | 3 | 2 | 1 | 就绪中 |
| 2 | Excel表格 | 4 | 2 | 2 | 0 | 未到达 |
| 3 | 音乐播放器 | 6 | 4 | 4 | 0 | 未到达 |
| 4 | 视频播放器 | 8 | 1 | 1 | 0 | 未到达 |
+-----+------------------+----------+----------+----------+----------+----------+
[执行] 时间 1: 执行进程 [Word文档] (队列 1: 高优先级), 执行 2 单位时间
[完成] 时间 3: 进程 [Word文档] 执行完成!
按回车键继续...
+------------------------------------------------------------------------+
| 当前时间: 3 |
+--------+----------------+----------+-----------------------------------+
| 队列ID | 优先级描述 | 时间片 | 队列中的进程 |
+--------+----------------+----------+-----------------------------------+
| 0 | 最高优先级 | 1 | 空队列 |
| 1 | 高优先级 | 2 | 空队列 |
| 2 | 最低优先级 | 4 | 空队列 |
+--------+----------------+----------+-----------------------------------+
进程状态表:
+-----+------------------+----------+----------+----------+----------+----------+
| PID | 进程名称 | 到达时间 | 执行时间 | 剩余时间 | 队列级别 | 状态 |
+-----+------------------+----------+----------+----------+----------+----------+
| 1 | Word文档 | 0 | 3 | 0 | 1 | 已完成 |
| 2 | Excel表格 | 4 | 2 | 2 | 0 | 未到达 |
| 3 | 音乐播放器 | 6 | 4 | 4 | 0 | 未到达 |
| 4 | 视频播放器 | 8 | 1 | 1 | 0 | 未到达 |
+-----+------------------+----------+----------+----------+----------+----------+
[空闲] 时间 3: CPU 空闲,跳到时间 6
按回车键继续...
[到达] 时间 6: 进程 [Excel表格] 到达系统,加入队列 0 (最高优先级)
[到达] 时间 6: 进程 [音乐播放器] 到达系统,加入队列 0 (最高优先级)
+------------------------------------------------------------------------+
| 当前时间: 6 |
+--------+----------------+----------+-----------------------------------+
| 队列ID | 优先级描述 | 时间片 | 队列中的进程 |
+--------+----------------+----------+-----------------------------------+
| 0 | 最高优先级 | 1 | Excel表格, 音乐播放器 |
| 1 | 高优先级 | 2 | 空队列 |
| 2 | 最低优先级 | 4 | 空队列 |
+--------+----------------+----------+-----------------------------------+
进程状态表:
+-----+------------------+----------+----------+----------+----------+----------+
| PID | 进程名称 | 到达时间 | 执行时间 | 剩余时间 | 队列级别 | 状态 |
+-----+------------------+----------+----------+----------+----------+----------+
| 1 | Word文档 | 0 | 3 | 0 | 1 | 已完成 |
| 2 | Excel表格 | 4 | 2 | 2 | 0 | 未到达 |
| 3 | 音乐播放器 | 6 | 4 | 4 | 0 | 未到达 |
| 4 | 视频播放器 | 8 | 1 | 1 | 0 | 未到达 |
+-----+------------------+----------+----------+----------+----------+----------+
[执行] 时间 6: 执行进程 [Excel表格] (队列 0: 最高优先级), 执行 1 单位时间
[降级] 时间 7: 进程 [Excel表格] 从队列 0 (最高优先级) 降级到队列 1 (高优先级)
按回车键继续...
+------------------------------------------------------------------------+
| 当前时间: 7 |
+--------+----------------+----------+-----------------------------------+
| 队列ID | 优先级描述 | 时间片 | 队列中的进程 |
+--------+----------------+----------+-----------------------------------+
| 0 | 最高优先级 | 1 | 音乐播放器 |
| 1 | 高优先级 | 2 | Excel表格 |
| 2 | 最低优先级 | 4 | 空队列 |
+--------+----------------+----------+-----------------------------------+
进程状态表:
+-----+------------------+----------+----------+----------+----------+----------+
| PID | 进程名称 | 到达时间 | 执行时间 | 剩余时间 | 队列级别 | 状态 |
+-----+------------------+----------+----------+----------+----------+----------+
| 1 | Word文档 | 0 | 3 | 0 | 1 | 已完成 |
| 2 | Excel表格 | 4 | 2 | 1 | 1 | 就绪中 |
| 3 | 音乐播放器 | 6 | 4 | 4 | 0 | 未到达 |
| 4 | 视频播放器 | 8 | 1 | 1 | 0 | 未到达 |
+-----+------------------+----------+----------+----------+----------+----------+
[执行] 时间 7: 执行进程 [音乐播放器] (队列 0: 最高优先级), 执行 1 单位时间
[降级] 时间 8: 进程 [音乐播放器] 从队列 0 (最高优先级) 降级到队列 1 (高优先级)
按回车键继续...
[到达] 时间 8: 进程 [视频播放器] 到达系统,加入队列 0 (最高优先级)
+------------------------------------------------------------------------+
| 当前时间: 8 |
+--------+----------------+----------+-----------------------------------+
| 队列ID | 优先级描述 | 时间片 | 队列中的进程 |
+--------+----------------+----------+-----------------------------------+
| 0 | 最高优先级 | 1 | 视频播放器 |
| 1 | 高优先级 | 2 | Excel表格, 音乐播放器 |
| 2 | 最低优先级 | 4 | 空队列 |
+--------+----------------+----------+-----------------------------------+
进程状态表:
+-----+------------------+----------+----------+----------+----------+----------+
| PID | 进程名称 | 到达时间 | 执行时间 | 剩余时间 | 队列级别 | 状态 |
+-----+------------------+----------+----------+----------+----------+----------+
| 1 | Word文档 | 0 | 3 | 0 | 1 | 已完成 |
| 2 | Excel表格 | 4 | 2 | 1 | 1 | 就绪中 |
| 3 | 音乐播放器 | 6 | 4 | 3 | 1 | 就绪中 |
| 4 | 视频播放器 | 8 | 1 | 1 | 0 | 未到达 |
+-----+------------------+----------+----------+----------+----------+----------+
[执行] 时间 8: 执行进程 [视频播放器] (队列 0: 最高优先级), 执行 1 单位时间
[完成] 时间 9: 进程 [视频播放器] 执行完成!
按回车键继续...
+------------------------------------------------------------------------+
| 当前时间: 9 |
+--------+----------------+----------+-----------------------------------+
| 队列ID | 优先级描述 | 时间片 | 队列中的进程 |
+--------+----------------+----------+-----------------------------------+
| 0 | 最高优先级 | 1 | 空队列 |
| 1 | 高优先级 | 2 | Excel表格, 音乐播放器 |
| 2 | 最低优先级 | 4 | 空队列 |
+--------+----------------+----------+-----------------------------------+
进程状态表:
+-----+------------------+----------+----------+----------+----------+----------+
| PID | 进程名称 | 到达时间 | 执行时间 | 剩余时间 | 队列级别 | 状态 |
+-----+------------------+----------+----------+----------+----------+----------+
| 1 | Word文档 | 0 | 3 | 0 | 1 | 已完成 |
| 2 | Excel表格 | 4 | 2 | 1 | 1 | 就绪中 |
| 3 | 音乐播放器 | 6 | 4 | 3 | 1 | 就绪中 |
| 4 | 视频播放器 | 8 | 1 | 0 | 0 | 已完成 |
+-----+------------------+----------+----------+----------+----------+----------+
[执行] 时间 9: 执行进程 [Excel表格] (队列 1: 高优先级), 执行 1 单位时间
[完成] 时间 10: 进程 [Excel表格] 执行完成!
按回车键继续...
+------------------------------------------------------------------------+
| 当前时间: 10 |
+--------+----------------+----------+-----------------------------------+
| 队列ID | 优先级描述 | 时间片 | 队列中的进程 |
+--------+----------------+----------+-----------------------------------+
| 0 | 最高优先级 | 1 | 空队列 |
| 1 | 高优先级 | 2 | 音乐播放器 |
| 2 | 最低优先级 | 4 | 空队列 |
+--------+----------------+----------+-----------------------------------+
进程状态表:
+-----+------------------+----------+----------+----------+----------+----------+
| PID | 进程名称 | 到达时间 | 执行时间 | 剩余时间 | 队列级别 | 状态 |
+-----+------------------+----------+----------+----------+----------+----------+
| 1 | Word文档 | 0 | 3 | 0 | 1 | 已完成 |
| 2 | Excel表格 | 4 | 2 | 0 | 1 | 已完成 |
| 3 | 音乐播放器 | 6 | 4 | 3 | 1 | 就绪中 |
| 4 | 视频播放器 | 8 | 1 | 0 | 0 | 已完成 |
+-----+------------------+----------+----------+----------+----------+----------+
[执行] 时间 10: 执行进程 [音乐播放器] (队列 1: 高优先级), 执行 2 单位时间
[降级] 时间 12: 进程 [音乐播放器] 从队列 1 (高优先级) 降级到队列 2 (最低优先级)
按回车键继续...
+------------------------------------------------------------------------+
| 当前时间: 12 |
+--------+----------------+----------+-----------------------------------+
| 队列ID | 优先级描述 | 时间片 | 队列中的进程 |
+--------+----------------+----------+-----------------------------------+
| 0 | 最高优先级 | 1 | 空队列 |
| 1 | 高优先级 | 2 | 空队列 |
| 2 | 最低优先级 | 4 | 音乐播放器 |
+--------+----------------+----------+-----------------------------------+
进程状态表:
+-----+------------------+----------+----------+----------+----------+----------+
| PID | 进程名称 | 到达时间 | 执行时间 | 剩余时间 | 队列级别 | 状态 |
+-----+------------------+----------+----------+----------+----------+----------+
| 1 | Word文档 | 0 | 3 | 0 | 1 | 已完成 |
| 2 | Excel表格 | 4 | 2 | 0 | 1 | 已完成 |
| 3 | 音乐播放器 | 6 | 4 | 1 | 2 | 就绪中 |
| 4 | 视频播放器 | 8 | 1 | 0 | 0 | 已完成 |
+-----+------------------+----------+----------+----------+----------+----------+
[执行] 时间 12: 执行进程 [音乐播放器] (队列 2: 最低优先级), 执行 1 单位时间
[完成] 时间 13: 进程 [音乐播放器] 执行完成!
按回车键继续...
调度结果统计:
+-----+------------------+----------+----------+----------+----------+----------+
| PID | 进程名称 | 到达时间 | 执行时间 | 完成时间 | 周转时间 | 等待时间 |
+-----+------------------+----------+----------+----------+----------+----------+
| 1 | Word文档 | 0 | 3 | 3 | 3 | 0 |
| 2 | Excel表格 | 4 | 2 | 10 | 6 | 2 |
| 3 | 音乐播放器 | 6 | 4 | 13 | 7 | 3 |
| 4 | 视频播放器 | 8 | 1 | 9 | 1 | 0 |
+-----+------------------+----------+----------+----------+----------+----------+
性能指标:
+--------------------------------+
| 平均周转时间: 4.25 |
| 平均等待时间: 1.25 |
| CPU利用率: 76.92 % |
+--------------------------------+
模拟结束,感谢使用!
--------------------------------
Process exited after 8.559 seconds with return value 0
请按任意键继续. . .
还是不对啊,到底什么情况,你看这段代码到底怎么改?#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
const char* device_names[] = {
"Chrome浏览器", "Word文档", "Excel表格", "音乐播放器",
"视频播放器", "图片编辑器", "终端窗口", "文件管理器",
"邮件客户端", "PDF阅读器", "代码编辑器", "系统监控器",
"游戏程序", "下载管理器", "压缩工具", "虚拟机"
};
typedef struct Process {
int pid;
char name[50];
int arrival_time;
int burst_time;
int remaining_time;
int waiting_time;
int turnaround_time;
int completion_time;
int queue_level;
} Process;
// 队列节点
typedef struct Node {
Process *process;
struct Node *next;
} Node;
// 队列结构
typedef struct Queue {
Node *front;
Node *rear;
int time_quantum;
int level;
char priority[20];
} Queue;
Process* createProcess(int pid, const char* name, int arrival_time, int burst_time) {
Process *p = (Process*)malloc(sizeof(Process));
p->pid = pid;
strcpy(p->name, name);
p->arrival_time = arrival_time;
p->burst_time = burst_time;
p->remaining_time = burst_time;
p->waiting_time = 0;
p->turnaround_time = 0;
p->completion_time = 0;
p->queue_level = 0;
return p;
}
Queue* createQueue(int time_quantum, int level, const char* priority) {
Queue *q = (Queue*)malloc(sizeof(Queue));
q->front = q->rear = NULL;
q->time_quantum = time_quantum;
q->level = level;
strcpy(q->priority, priority);
return q;
}
bool isEmpty(Queue *q) {
return q->front == NULL;
}
void enqueue(Queue *q, Process *p) {
Node *newNode = (Node*)malloc(sizeof(Node));
newNode->process = p;
newNode->next = NULL;
if (isEmpty(q)) {
q->front = q->rear = newNode;
} else {
q->rear->next = newNode;
q->rear = newNode;
}
}
Process* dequeue(Queue *q) {
if (isEmpty(q)) {
return NULL;
}
Node *temp = q->front;
Process *p = temp->process;
q->front = q->front->next;
if (q->front == NULL) {
q->rear = NULL;
}
free(temp);
return p;
}
int getQueueSize(Queue *q) {
int count = 0;
Node *current = q->front;
while (current != NULL) {
count++;
current = current->next;
}
return count;
}
const char* getPriorityDescription(int level, int total_queues) {
if (total_queues == 1) return "唯一队列";
if (level == 0) return "最高优先级";
if (level == total_queues - 1) return "最低优先级";
if (level == 1) return "高优先级";
if (level == total_queues - 2) return "低优先级";
float position = (float)level / (total_queues - 1);
if (position < 0.33) return "高优先级";
if (position < 0.67) return "中优先级";
return "低优先级";
}
int displayWidth(const char* str) {
int width = 0;
while (*str) {
if ((unsigned char)*str >= 0x80) {
width += 2;
str += 3;
} else {
width += 1;
str += 1;
}
}
return width;
}
void displayQueues(Queue **queues, int queue_count, int current_time) {
printf("\n");
printf("+------------------------------------------------------------------------+\n");
printf("| 当前时间: %-4d |\n", current_time);
printf("+--------+----------------+----------+-----------------------------------+\n");
printf("| 队列ID | 优先级描述 | 时间片 | 队列中的进程 |\n");
printf("+--------+----------------+----------+-----------------------------------+\n");
for (int i = 0; i < queue_count; i++) {
printf("| %-4d | %-14s | %-8d | ", i, queues[i]->priority, queues[i]->time_quantum);
if (isEmpty(queues[i])) {
printf("%-33s |\n", "空队列");
} else {
Node *current = queues[i]->front;
char display_buffer[100] = "";
int count = 0;
while (current != NULL && count < 3) {
char temp[50];
if (count > 0) {
sprintf(temp, ", %s", current->process->name);
} else {
sprintf(temp, "%s", current->process->name);
}
if (strlen(display_buffer) + strlen(temp) <= 30) {
strcat(display_buffer, temp);
count++;
current = current->next;
} else {
break;
}
}
if (current != NULL) {
int remaining = getQueueSize(queues[i]) - count;
char remaining_str[20];
sprintf(remaining_str, " ...(+%d)", remaining);
if (strlen(display_buffer) + strlen(remaining_str) <= 33) {
strcat(display_buffer, remaining_str);
}
}
printf("%-33s |\n", display_buffer);
}
}
printf("+--------+----------------+----------+-----------------------------------+\n");
}
void displayProcessStatus(Process **processes, int n) {
printf("\n进程状态表:\n");
printf("+-----+------------------+----------+----------+----------+----------+----------+\n");
printf("| PID | 进程名称 | 到达时间 | 执行时间 | 剩余时间 | 队列级别 | 状态 |\n");
printf("+-----+------------------+----------+----------+----------+----------+----------+\n");
for (int i = 0; i < n; i++) {
char status[20];
if (processes[i]->remaining_time == 0) {
strcpy(status, "已完成");
} else if (processes[i]->arrival_time > 0 && processes[i]->remaining_time == processes[i]->burst_time) {
strcpy(status, "未到达");
} else if (processes[i]->remaining_time > 0) {
strcpy(status, "就绪中");
} else {
strcpy(status, "运行中");
}
printf("| %-3d | %-16s | %-8d | %-8d | %-8d | %-8d | %-8s |\n",
processes[i]->pid,
processes[i]->name,
processes[i]->arrival_time,
processes[i]->burst_time,
processes[i]->remaining_time,
processes[i]->queue_level,
status);
}
printf("+-----+------------------+----------+----------+----------+----------+----------+\n");
}
bool isProcessInAnyQueue(Queue **queues, int queue_count, int pid) {
for (int q = 0; q < queue_count; q++) {
Node *current = queues[q]->front;
while (current != NULL) {
if (current->process->pid == pid) {
return true;
}
current = current->next;
}
}
return false;
}
// 多级反馈队列调度算法
void multilevelFeedbackQueueScheduling(Process **processes, int n, int queue_count, int *time_quantums) {
Queue **queues = (Queue**)malloc(queue_count * sizeof(Queue*));
for (int i = 0; i < queue_count; i++) {
queues[i] = createQueue(time_quantums[i], i, getPriorityDescription(i, queue_count));
}
int current_time = 0;
int completed = 0;
int total_processes = n;
printf("\n开始执行多级反馈队列调度...\n");
while (completed < total_processes) {
// 先处理进程到达 - 修复:确保所有已到达进程都加入队列
for (int i = 0; i < n; i++) {
if (processes[i]->arrival_time <= current_time &&
processes[i]->remaining_time > 0) {
// 修复:检查进程是否已经在队列中
bool in_queue = false;
for (int q = 0; q < queue_count; q++) {
Node *node = queues[q]->front;
while (node != NULL) {
if (node->process->pid == processes[i]->pid) {
in_queue = true;
break;
}
node = node->next;
}
if (in_queue) break;
}
if (!in_queue) {
processes[i]->queue_level = 0;
enqueue(queues[0], processes[i]);
printf("\n[到达] 时间 %d: 进程 [%s] 到达系统,加入队列 %d (%s)\n",
current_time, processes[i]->name, 0, queues[0]->priority);
}
}
}
// 再显示当前状态
displayQueues(queues, queue_count, current_time);
displayProcessStatus(processes, n);
bool process_executed = false;
for (int i = 0; i < queue_count; i++) {
if (!isEmpty(queues[i])) {
Process *current_process = dequeue(queues[i]);
int time_quantum = queues[i]->time_quantum;
int execution_time;
if (current_process->remaining_time <= time_quantum) {
execution_time = current_process->remaining_time;
} else {
execution_time = time_quantum;
}
printf("\n[执行] 时间 %d: 执行进程 [%s] (队列 %d: %s), 执行 %d 单位时间\n",
current_time, current_process->name, i, queues[i]->priority, execution_time);
// 在执行前先更新其他进程的等待时间
for (int j = 0; j < n; j++) {
if (processes[j] != current_process &&
processes[j]->arrival_time <= current_time &&
processes[j]->remaining_time > 0 &&
processes[j]->completion_time == 0) {
processes[j]->waiting_time += execution_time;
}
}
current_process->remaining_time -= execution_time;
current_time += execution_time;
if (current_process->remaining_time == 0) {
current_process->completion_time = current_time;
current_process->turnaround_time = current_process->completion_time -
current_process->arrival_time;
completed++;
printf("\n[完成] 时间 %d: 进程 [%s] 执行完成!\n", current_time, current_process->name);
} else {
if (i < queue_count - 1) {
current_process->queue_level = i + 1;
enqueue(queues[i + 1], current_process);
printf("\n[降级] 时间 %d: 进程 [%s] 从队列 %d (%s) 降级到队列 %d (%s)\n",
current_time, current_process->name,
i, queues[i]->priority,
i + 1, queues[i + 1]->priority);
} else {
enqueue(queues[i], current_process);
printf("\n[重排] 时间 %d: 进程 [%s] 在队列 %d (%s) 中重新排队\n",
current_time, current_process->name, i, queues[i]->priority);
}
}
process_executed = true;
break;
}
}
if (!process_executed) {
int next_arrival = current_time + 1;
for (int i = 0; i < n; i++) {
if (processes[i]->arrival_time > current_time &&
processes[i]->remaining_time > 0) {
if (next_arrival == current_time + 1 ||
processes[i]->arrival_time < next_arrival) {
next_arrival = processes[i]->arrival_time;
}
}
}
printf("\n[空闲] 时间 %d: CPU 空闲,跳到时间 %d\n", current_time, next_arrival);
current_time = next_arrival;
}
printf("\n按回车键继续...");
getchar();
}
int total_burst_time = 0;
for (int i = 0; i < n; i++) {
total_burst_time += processes[i]->burst_time;
}
printf("\n调度结果统计:\n");
printf("+-----+------------------+----------+----------+----------+----------+----------+\n");
printf("| PID | 进程名称 | 到达时间 | 执行时间 | 完成时间 | 周转时间 | 等待时间 |\n");
printf("+-----+------------------+----------+----------+----------+----------+----------+\n");
float total_turnaround = 0, total_waiting = 0;
for (int i = 0; i < n; i++) {
printf("| %-3d | %-16s | %-8d | %-8d | %-8d | %-8d | %-8d |\n",
processes[i]->pid,
processes[i]->name,
processes[i]->arrival_time,
processes[i]->burst_time,
processes[i]->completion_time,
processes[i]->turnaround_time,
processes[i]->waiting_time);
total_turnaround += processes[i]->turnaround_time;
total_waiting += processes[i]->waiting_time;
}
printf("+-----+------------------+----------+----------+----------+----------+----------+\n");
printf("\n性能指标:\n");
printf("+--------------------------------+\n");
printf("| 平均周转时间: %-12.2f |\n", total_turnaround / n);
printf("| 平均等待时间: %-12.2f |\n", total_waiting / n);
printf("| CPU利用率: %-10.2f%% |\n", (current_time > 0 ? (total_burst_time * 100.0) / current_time : 0));
printf("+--------------------------------+\n");
for (int i = 0; i < queue_count; i++) {
while (!isEmpty(queues[i])) {
dequeue(queues[i]);
}
free(queues[i]);
}
free(queues);
}
void inputProcesses(Process ***processes, int *n) {
printf("请输入进程数量: ");
scanf("%d", n);
getchar();
*processes = (Process**)malloc((*n) * sizeof(Process*));
printf("\n请依次输入每个进程的信息(进程ID 到达时间 执行时间):\n");
printf("可用设备类型: \n");
int device_count = sizeof(device_names) / sizeof(device_names[0]);
for (int i = 0; i < device_count && i < 12; i++) {
printf(" %2d. %s\n", i+1, device_names[i]);
}
for (int i = 0; i < *n; i++) {
int pid, arrival_time, burst_time;
printf("\n进程 %d (PID 到达时间 执行时间): ", i + 1);
scanf("%d %d %d", &pid, &arrival_time, &burst_time);
getchar();
int device_index = pid % (sizeof(device_names)/sizeof(device_names[0]));
(*processes)[i] = createProcess(pid, device_names[device_index], arrival_time, burst_time);
}
}
void inputQueueParameters(int *queue_count, int **time_quantums) {
printf("\n请输入队列数量: ");
scanf("%d", queue_count);
getchar();
*time_quantums = (int*)malloc((*queue_count) * sizeof(int));
printf("\n请依次输入每个队列的时间片大小:\n");
for (int i = 0; i < *queue_count; i++) {
printf("队列 %d 时间片: ", i);
scanf("%d", &(*time_quantums)[i]);
getchar();
}
}
int main() {
Process **processes = NULL;
int n = 0;
int queue_count = 0;
int *time_quantums = NULL;
printf("================================================================================\n");
printf(" 多级反馈队列调度算法模拟器\n");
printf("================================================================================\n");
inputProcesses(&processes, &n);
inputQueueParameters(&queue_count, &time_quantums);
printf("\n输入信息汇总:\n");
printf("+-----+------------------+----------+----------+\n");
printf("| PID | 进程名称 | 到达时间 | 执行时间 |\n");
printf("+-----+------------------+----------+----------+\n");
for (int i = 0; i < n; i++) {
printf("| %-3d | %-16s | %-8d | %-8d |\n",
processes[i]->pid,
processes[i]->name,
processes[i]->arrival_time,
processes[i]->burst_time);
}
printf("+-----+------------------+----------+----------+\n");
printf("\n队列信息:\n");
printf("+----------+------------+----------------+\n");
printf("| 队列级别 | 时间片大小 | 优先级描述 |\n");
printf("+----------+------------+----------------+\n");
for (int i = 0; i < queue_count; i++) {
printf("| %-8d | %-10d | %-14s |\n", i, time_quantums[i], getPriorityDescription(i, queue_count));
}
printf("+----------+------------+----------------+\n");
printf("\n模拟即将开始...\n");
multilevelFeedbackQueueScheduling(processes, n, queue_count, time_quantums);
for (int i = 0; i < n; i++) {
free(processes[i]);
}
free(processes);
free(time_quantums);
printf("\n模拟结束,感谢使用!\n");
return 0;
}
最新发布