#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义进程控制块PCB结构
typedef struct PCB {
char name[10]; // 进程名称
int cputime; // 已使用CPU时间
int needtime; // 尚需CPU时间
int priority; // 优先级
char state; // 进程状态:R=执行态,W=就绪态,F=完成态
struct PCB *next;
} PCB;
PCB *head = NULL; // 就绪队列头指针
PCB *tail = NULL; // 就绪队列尾指针
PCB *finish_head = NULL; // 完成队列头指针
PCB *current_run = NULL; // 当前运行进程指针
// 创建新的PCB节点
PCB* createPCB(const char* name, int cputime, int needtime, int priority, char state) {
PCB *p = (PCB*)malloc(sizeof(PCB));
strncpy(p->name, name, 9);
p->name[9] = '\0';
p->cputime = cputime;
p->needtime = needtime;
p->priority = priority;
p->state = state;
p->next = NULL;
return p;
}
// 按优先级插入就绪队列(从高到低排序)
void insertByPriority(PCB *p) {
PCB *current = head;
PCB *prev = NULL;
while (current && current->priority >= p->priority) {
prev = current;
current = current->next;
}
if (prev == NULL) {
p->next = head;
head = p;
} else {
prev->next = p;
p->next = current;
}
if (current == NULL) tail = p;
}
// 将进程移动到完成队列
void finishProcess(PCB *p) {
p->state = 'F';
p->next = NULL;
if (finish_head == NULL) {
finish_head = p;
} else {
PCB *q = finish_head;
while (q->next != NULL) q = q->next;
q->next = p;
}
}
// 打印所有进程状态(按R→W→F顺序)
void printAllProcesses(int time) {
printf("\n时间片 %d:\n", time);
printf("进程名\t已用CPU时间\t剩余需时间\t优先级\t\t状态\n");
if (current_run != NULL) {
printf("%s\t%d\t\t%d\t\t%d\t\t%c\n",
current_run->name, current_run->cputime,
current_run->needtime, current_run->priority, 'R');
}
PCB *p = head;
while (p != NULL) {
printf("%s\t%d\t\t%d\t\t%d\t\t%c\n",
p->name, p->cputime, p->needtime, p->priority, 'W');
p = p->next;
}
p = finish_head;
while (p != NULL) {
printf("%s\t%d\t\t%d\t\t%d\t\t%c\n",
p->name, p->cputime, p->needtime, p->priority, 'F');
p = p->next;
}
}
// 优先级调度算法(每次执行一个时间片)
void priorityScheduling() {
int current_time = 0;
while (head != NULL || current_run != NULL) {
if (current_run == NULL && head != NULL) {
current_run = head;
head = head->next;
if (head == NULL) tail = NULL;
current_run->next = NULL;
}
if (current_run == NULL) break;
current_run->cputime++;
current_run->needtime--;
current_run->priority--;
current_run->state = 'R';
printAllProcesses(current_time);
if (current_run->needtime == 0) {
finishProcess(current_run);
free(current_run);
current_run = NULL;
} else {
current_run->state = 'W';
insertByPriority(current_run);
current_run = NULL;
}
current_time++;
}
}
// 时间片轮转调度算法(时间片=2)
void roundRobin() {
int current_time = 0;
while (head != NULL) {
PCB *p = head;
head = head->next;
if (head == NULL) tail = NULL;
p->state = 'R';
int run_time = (p->needtime >= 2) ? 2 : p->needtime;
p->cputime += run_time;
p->needtime -= run_time;
current_time += run_time;
printAllProcesses(current_time - run_time);
if (p->needtime == 0) {
finishProcess(p);
free(p);
} else {
p->state = 'W';
if (tail == NULL) {
head = tail = p;
} else {
tail->next = p;
tail = p;
}
tail->next = NULL;
}
}
}
int main() {
int n, need, i; // 提前声明循环变量i
char name[10];
printf("请输入进程数量: ");
scanf("%d", &n);
PCB **processes = (PCB**)malloc(n * sizeof(PCB*));
for (i = 0; i < n; ++i) {
printf("请输入进程名称和所需时间(例如:P1 5): ");
scanf("%s %d", name, &need);
int initial_priority = 50 - need;
processes[i] = createPCB(name, 0, need, initial_priority, 'W');
}
int choice;
printf("\n请选择调度算法:1为优先级调度,2为时间片轮转调度: ");
scanf("%d", &choice);
head = tail = NULL;
if (choice == 1) {
for (i = 0; i < n; ++i) {
insertByPriority(processes[i]);
}
} else if (choice == 2) {
for (i = 0; i < n; ++i) {
if (head == NULL) {
head = tail = processes[i];
} else {
tail->next = processes[i];
tail = processes[i];
}
processes[i]->next = NULL;
}
} else {
printf("无效选择!\n");
free(processes);
return 0;
}
if (choice == 1) {
priorityScheduling();
} else if (choice == 2) {
roundRobin();
}
printf("\n最终完成的进程:\n");
PCB *p = finish_head;
while (p != NULL) {
printf("%s\t已用CPU时间: %d\t剩余需时间: %d\t优先级: %d\n",
p->name, p->cputime, p->needtime, p->priority);
p = p->next;
}
free(processes);
while (finish_head != NULL) {
PCB *temp = finish_head;
finish_head = finish_head->next;
free(temp);
}
return 0;
}