编写c程序模拟实现单处理机系统中的进程调度算法,实现对多个进程的调度模拟,要求使用常见的进程调度算法。
时间: 2023-12-18 08:05:44 浏览: 198
以下是使用C语言编写的一个简单的单处理机系统进程调度模拟程序,包括三种常见的进程调度算法:先来先服务(FCFS)、最短作业优先(SJF)和时间片轮转(RR)。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_PROCESSES 10 // 最大进程数
#define MAX_TIME 100 // 最大时间片数
typedef struct Process {
int id; // 进程标识符
int arrival_time; // 到达时间
int burst_time; // 执行时间
int priority; // 优先级
int remaining_time; // 剩余执行时间
int waiting_time; // 等待时间
int turnaround_time; // 周转时间
} Process;
void fcfs(Process *processes, int num_processes); // 先来先服务
void sjf(Process *processes, int num_processes); // 最短作业优先
void rr(Process *processes, int num_processes, int time_slice); // 时间片轮转
int main() {
Process processes[MAX_PROCESSES];
int num_processes, time_slice;
// 输入进程信息
printf("请输入进程数:");
scanf("%d", &num_processes);
for (int i = 0; i < num_processes; i++) {
printf("请输入第%d个进程的信息(到达时间、执行时间、优先级):", i + 1);
scanf("%d%d%d", &processes[i].arrival_time, &processes[i].burst_time, &processes[i].priority);
processes[i].id = i + 1;
processes[i].remaining_time = processes[i].burst_time;
processes[i].waiting_time = 0;
processes[i].turnaround_time = 0;
}
// 先来先服务
fcfs(processes, num_processes);
// 最短作业优先
sjf(processes, num_processes);
// 时间片轮转
printf("请输入时间片长度:");
scanf("%d", &time_slice);
rr(processes, num_processes, time_slice);
return 0;
}
// 先来先服务
void fcfs(Process *processes, int num_processes) {
printf("先来先服务算法:\n");
int current_time = 0;
float avg_waiting_time = 0, avg_turnaround_time = 0;
for (int i = 0; i < num_processes; i++) {
if (current_time < processes[i].arrival_time) {
current_time = processes[i].arrival_time;
}
current_time += processes[i].burst_time;
processes[i].turnaround_time = current_time - processes[i].arrival_time;
processes[i].waiting_time = processes[i].turnaround_time - processes[i].burst_time;
avg_waiting_time += processes[i].waiting_time;
avg_turnaround_time += processes[i].turnaround_time;
}
avg_waiting_time /= num_processes;
avg_turnaround_time /= num_processes;
printf("进程\t到达时间\t执行时间\t等待时间\t周转时间\n");
for (int i = 0; i < num_processes; i++) {
printf("%d\t%d\t\t%d\t\t%d\t\t%d\n", processes[i].id, processes[i].arrival_time, processes[i].burst_time, processes[i].waiting_time, processes[i].turnaround_time);
}
printf("平均等待时间:%f\n", avg_waiting_time);
printf("平均周转时间:%f\n", avg_turnaround_time);
}
// 最短作业优先
void sjf(Process *processes, int num_processes) {
printf("最短作业优先算法:\n");
Process *queue[MAX_PROCESSES];
int queue_size = 0, current_time = 0, completed_processes = 0;
float avg_waiting_time = 0, avg_turnaround_time = 0;
while (completed_processes < num_processes) {
// 将到达时间小于等于当前时间的进程加入队列
for (int i = 0; i < num_processes; i++) {
if (processes[i].arrival_time <= current_time && processes[i].remaining_time > 0) {
queue[queue_size++] = &processes[i];
}
}
// 选取剩余执行时间最短的进程
Process *shortest_process = queue[0];
for (int i = 1; i < queue_size; i++) {
if (queue[i]->remaining_time < shortest_process->remaining_time) {
shortest_process = queue[i];
}
}
// 执行该进程并更新队列
shortest_process->remaining_time--;
current_time++;
if (shortest_process->remaining_time == 0) {
shortest_process->turnaround_time = current_time - shortest_process->arrival_time;
shortest_process->waiting_time = shortest_process->turnaround_time - shortest_process->burst_time;
avg_waiting_time += shortest_process->waiting_time;
avg_turnaround_time += shortest_process->turnaround_time;
completed_processes++;
for (int i = 0; i < queue_size; i++) {
if (queue[i] == shortest_process) {
queue[i] = queue[queue_size - 1];
queue_size--;
break;
}
}
}
}
avg_waiting_time /= num_processes;
avg_turnaround_time /= num_processes;
printf("进程\t到达时间\t执行时间\t等待时间\t周转时间\n");
for (int i = 0; i < num_processes; i++) {
printf("%d\t%d\t\t%d\t\t%d\t\t%d\n", processes[i].id, processes[i].arrival_time, processes[i].burst_time, processes[i].waiting_time, processes[i].turnaround_time);
}
printf("平均等待时间:%f\n", avg_waiting_time);
printf("平均周转时间:%f\n", avg_turnaround_time);
}
// 时间片轮转
void rr(Process *processes, int num_processes, int time_slice) {
printf("时间片轮转算法:\n");
Process *queue[MAX_PROCESSES];
int queue_size = 0, current_time = 0, completed_processes = 0;
float avg_waiting_time = 0, avg_turnaround_time = 0;
while (completed_processes < num_processes) {
// 将到达时间小于等于当前时间的进程加入队列
for (int i = 0; i < num_processes; i++) {
if (processes[i].arrival_time <= current_time && processes[i].remaining_time > 0) {
queue[queue_size++] = &processes[i];
}
}
// 执行队首进程并更新队列
Process *current_process = queue[0];
if (current_process->remaining_time <= time_slice) {
current_time += current_process->remaining_time;
current_process->remaining_time = 0;
} else {
current_time += time_slice;
current_process->remaining_time -= time_slice;
}
// 更新等待时间和周转时间
for (int i = 1; i < queue_size; i++) {
queue[i]->waiting_time += current_process->remaining_time;
}
if (current_process->remaining_time == 0) {
current_process->turnaround_time = current_time - current_process->arrival_time;
current_process->waiting_time = current_process->turnaround_time - current_process->burst_time;
avg_waiting_time += current_process->waiting_time;
avg_turnaround_time += current_process->turnaround_time;
completed_processes++;
for (int i = 0; i < queue_size - 1; i++) {
queue[i] = queue[i + 1];
}
queue_size--;
} else {
queue[queue_size - 1] = current_process;
}
}
avg_waiting_time /= num_processes;
avg_turnaround_time /= num_processes;
printf("进程\t到达时间\t执行时间\t等待时间\t周转时间\n");
for (int i = 0; i < num_processes; i++) {
printf("%d\t%d\t\t%d\t\t%d\t\t%d\n", processes[i].id, processes[i].arrival_time, processes[i].burst_time, processes[i].waiting_time, processes[i].turnaround_time);
}
printf("平均等待时间:%f\n", avg_waiting_time);
printf("平均周转时间:%f\n", avg_turnaround_time);
}
```
程序中定义了一个结构体`Process`,表示一个进程,包含进程标识符、到达时间、执行时间、优先级、剩余执行时间、等待时间和周转时间等信息。`fcfs()`函数实现了先来先服务算法,`sjf()`函数实现了最短作业优先算法,`rr()`函数实现了时间片轮转算法。
在`main()`函数中,首先输入进程信息,然后依次调用三个算法的函数进行模拟。每个算法的实现都类似,都是按照不同的规则从进程队列中选择下一个要执行的进程,并将执行情况更新到进程的信息中,直到所有进程都执行完成为止。最后输出每个进程的各项信息和平均等待时间、平均周转时间。
阅读全文