编制时间片轮转调度算法,可自定义时间片的大小,编制静态优先级调度算法,优先级高低可自定义的c语言代码
时间: 2024-03-20 07:41:45 浏览: 12
时间片轮转调度算法 C 代码:
```c
#include <stdio.h>
#define MAX_PROCESS 10 // 最大进程数
#define MAX_TIME_SLOT 100 // 最大时间片数
#define MAX_ARRIVAL_TIME 100 // 最大到达时间
#define MAX_PRIORITY 10 // 最大优先级
struct process {
int pid; // 进程 ID
int arrival_time; // 到达时间
int burst_time; // 执行时间
int remaining_time; // 剩余执行时间
int priority; // 优先级
};
int main() {
int n, time_slot;
printf("请输入进程数:");
scanf("%d", &n);
struct process p[MAX_PROCESS];
int i;
// 输入进程信息
for (i = 0; i < n; i++) {
printf("请输入第 %d 个进程的到达时间、执行时间和优先级:", i+1);
scanf("%d%d%d", &p[i].arrival_time, &p[i].burst_time, &p[i].priority);
p[i].pid = i + 1;
p[i].remaining_time = p[i].burst_time;
}
printf("请输入时间片大小:");
scanf("%d", &time_slot);
int current_time = 0;
int completed_process = 0; // 已完成的进程数
int time_slot_count = 0; // 时间片计数器
while (completed_process < n) {
int selected_process = -1; // 选中的进程
int highest_priority = -1; // 最高优先级
// 选择优先级最高的进程
for (i = 0; i < n; i++) {
if (p[i].arrival_time <= current_time && p[i].remaining_time > 0 &&
(highest_priority == -1 || p[i].priority < highest_priority)) {
selected_process = i;
highest_priority = p[i].priority;
}
}
if (selected_process == -1) {
// 当前时间无进程可运行,跳过
current_time++;
continue;
}
// 运行进程
p[selected_process].remaining_time--;
time_slot_count++;
if (p[selected_process].remaining_time == 0) {
// 进程已完成
completed_process++;
printf("时间 %d : 进程 %d 完成\n", current_time, p[selected_process].pid);
} else if (time_slot_count == time_slot) {
// 时间片用完,切换进程
time_slot_count = 0;
printf("时间 %d : 进程 %d 时间片用完\n", current_time, p[selected_process].pid);
}
current_time++;
}
return 0;
}
```
静态优先级调度算法 C 代码:
```c
#include <stdio.h>
#define MAX_PROCESS 10 // 最大进程数
#define MAX_ARRIVAL_TIME 100 // 最大到达时间
#define MAX_PRIORITY 10 // 最大优先级
struct process {
int pid; // 进程 ID
int arrival_time; // 到达时间
int burst_time; // 执行时间
int remaining_time; // 剩余执行时间
int priority; // 优先级
};
int main() {
int n;
printf("请输入进程数:");
scanf("%d", &n);
struct process p[MAX_PROCESS];
int i;
// 输入进程信息
for (i = 0; i < n; i++) {
printf("请输入第 %d 个进程的到达时间、执行时间和优先级:", i+1);
scanf("%d%d%d", &p[i].arrival_time, &p[i].burst_time, &p[i].priority);
p[i].pid = i + 1;
p[i].remaining_time = p[i].burst_time;
}
// 按优先级排序
for (i = 0; i < n - 1; i++) {
int j, max_priority_index = i;
for (j = i + 1; j < n; j++) {
if (p[j].priority < p[max_priority_index].priority) {
max_priority_index = j;
}
}
if (i != max_priority_index) {
struct process temp = p[i];
p[i] = p[max_priority_index];
p[max_priority_index] = temp;
}
}
int current_time = 0;
int completed_process = 0; // 已完成的进程数
while (completed_process < n) {
int selected_process = -1; // 选中的进程
// 选择优先级最高的进程
for (i = 0; i < n; i++) {
if (p[i].arrival_time <= current_time && p[i].remaining_time > 0) {
selected_process = i;
break;
}
}
if (selected_process == -1) {
// 当前时间无进程可运行,跳过
current_time++;
continue;
}
// 运行进程
p[selected_process].remaining_time--;
if (p[selected_process].remaining_time == 0) {
// 进程已完成
completed_process++;
printf("时间 %d : 进程 %d 完成\n", current_time, p[selected_process].pid);
}
current_time++;
}
return 0;
}
```