#define MAX_PROCESS_NUM 10 typedef enum{ READY, RUNNING, BLOCKED } ProcessState; typedef struct{ int pid; ProcessState state; int priority; int remain_time_slice; } PCB; PCB processes[MAX_PROCESS_NUM]; int process_num = 0; #define TIME_SLICE 3 void time_slice_scheduling(){ int i; do{ for(i = 0; i < process_num; i++){ if(processes[i].state == RUNNING){ processes[i].remain_time_slice--; if(processes[i].remain_time_slice == 0){ processes[i].state = READY; } } if(processes[i].state == READY){ processes[i].state = RUNNING; processes[i].remain_time_slice = TIME_SLICE; } } }while(1); } void priority_scheduling(){ int i, j, max_priority, max_index; do{ max_priority = -1; max_index = -1; for(i = 0; i < process_num; i++){ if(processes[i].state == READY && processes[i].priority > max_priority){ max_priority = processes[i].priority; max_index = i; } } if(max_index != -1){ processes[max_index].state = RUNNING; for(j = 0; j < process_num; j++){ if(j != max_index && processes[j].state != BLOCKED){ processes[j].state = READY; } } } }while(1); } #include <stdio.h> int main(){ // 创建进程并初始化 processes[0].pid = 0; processes[0].state = RUNNING; processes[0].priority = 2; processes[0].remain_time_slice = TIME_SLICE; processes[1].pid = 1; processes[1].state = READY; processes[1].priority = 1; processes[1].remain_time_slice = 0; processes[2].pid = 2; processes[2].state = READY; processes[2].priority = 3; processes[2].remain_time_slice = 0; process_num = 3; // 调用时间片轮转调度算法 time_slice_scheduling(); // 调用静态优先级调度算法 priority_scheduling(); return 0; }代码运行错误,请修改
时间: 2024-03-06 15:51:39 浏览: 58
这段代码存在两个死循环,分别在 time_slice_scheduling() 和 priority_scheduling() 函数中。这会导致程序无法正常退出,需要在循环中添加退出条件。另外,在 priority_scheduling() 函数中,当没有就绪进程时,应该等待新的进程到来,而不是一直循环。修改后的代码如下:
```
#include <stdio.h>
#define MAX_PROCESS_NUM 10
#define TIME_SLICE 3
typedef enum { READY, RUNNING, BLOCKED } ProcessState;
typedef struct {
int pid;
ProcessState state;
int priority;
int remain_time_slice;
} PCB;
PCB processes[MAX_PROCESS_NUM];
int process_num = 0;
void time_slice_scheduling() {
int i;
do {
int all_blocked = 1;
for (i = 0; i < process_num; i++) {
if (processes[i].state == RUNNING) {
processes[i].remain_time_slice--;
if (processes[i].remain_time_slice == 0) {
processes[i].state = READY;
} else {
all_blocked = 0;
}
}
if (processes[i].state == READY) {
processes[i].state = RUNNING;
processes[i].remain_time_slice = TIME_SLICE;
all_blocked = 0;
}
}
if (all_blocked) {
break;
}
} while (1);
}
void priority_scheduling() {
int i, j, max_priority, max_index;
do {
int all_blocked = 1;
max_priority = -1;
max_index = -1;
for (i = 0; i < process_num; i++) {
if (processes[i].state == READY && processes[i].priority > max_priority) {
max_priority = processes[i].priority;
max_index = i;
all_blocked = 0;
}
}
if (!all_blocked) {
processes[max_index].state = RUNNING;
for (j = 0; j < process_num; j++) {
if (j != max_index && processes[j].state != BLOCKED) {
processes[j].state = READY;
}
}
} else {
break;
}
} while (1);
}
int main() {
// 创建进程并初始化
processes[0].pid = 0;
processes[0].state = RUNNING;
processes[0].priority = 2;
processes[0].remain_time_slice = TIME_SLICE;
processes[1].pid = 1;
processes[1].state = READY;
processes[1].priority = 1;
processes[1].remain_time_slice = 0;
processes[2].pid = 2;
processes[2].state = READY;
processes[2].priority = 3;
processes[2].remain_time_slice = 0;
process_num = 3;
// 调用时间片轮转调度算法
time_slice_scheduling();
// 调用静态优先级调度算法
priority_scheduling();
return 0;
}
```
阅读全文