编写C程序模拟实现单处理机系统中的进程调度算法,实现对多个进程的调度模拟,要求采用常见进程调度算法(如先来先服务、时间片轮转和优先级调度等算法)进行模拟调度。
时间: 2023-12-18 19:05:18 浏览: 86
好的,我可以为您提供一份简单的单处理器进程调度模拟程序,包括先来先服务(FCFS)、时间片轮转(RR)和优先级调度(PS)三种算法的模拟调度。以下是程序代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_PROCESSES 5
#define TIME_QUANTUM 2
typedef struct {
int pid; // 进程ID
int arrival_time; // 到达时间
int burst_time; // 执行时间
int priority; // 优先级
int waiting_time; // 等待时间
int turnaround_time; // 周转时间
} process;
void fcfs(process* processes, int n);
void rr(process* processes, int n);
void ps(process* processes, int n);
int main() {
process processes[MAX_PROCESSES] = {
{1, 0, 5, 2},
{2, 1, 3, 1},
{3, 2, 8, 4},
{4, 3, 6, 3},
{5, 4, 4, 5}
};
int n = MAX_PROCESSES;
printf("FCFS:\n");
fcfs(processes, n);
printf("\n\nRR:\n");
rr(processes, n);
printf("\n\nPS:\n");
ps(processes, n);
return 0;
}
void fcfs(process* processes, int n) {
int i, j;
int current_time = 0;
int total_waiting_time = 0;
int total_turnaround_time = 0;
for (i = 0; i < n; i++) {
if (processes[i].arrival_time > current_time) {
current_time = processes[i].arrival_time;
}
processes[i].waiting_time = current_time - processes[i].arrival_time;
processes[i].turnaround_time = processes[i].waiting_time + processes[i].burst_time;
current_time += processes[i].burst_time;
total_waiting_time += processes[i].waiting_time;
total_turnaround_time += processes[i].turnaround_time;
}
printf("PID\tAT\tBT\tPri\tWT\tTAT\n");
for (i = 0; i < n; i++) {
printf("%d\t%d\t%d\t%d\t%d\t%d\n", processes[i].pid, processes[i].arrival_time, processes[i].burst_time, processes[i].priority, processes[i].waiting_time, processes[i].turnaround_time);
}
printf("Average waiting time: %.2f\n", (float)total_waiting_time / n);
printf("Average turnaround time: %.2f\n", (float)total_turnaround_time / n);
}
void rr(process* processes, int n) {
int i, j;
int current_time = 0;
int total_waiting_time = 0;
int total_turnaround_time = 0;
int remaining_burst_time[n];
memset(remaining_burst_time, 0, sizeof(remaining_burst_time));
for (i = 0; i < n; i++) {
remaining_burst_time[i] = processes[i].burst_time;
}
while (1) {
int done = 1;
for (i = 0; i < n; i++) {
if (remaining_burst_time[i] > 0) {
done = 0;
if (remaining_burst_time[i] > TIME_QUANTUM) {
remaining_burst_time[i] -= TIME_QUANTUM;
current_time += TIME_QUANTUM;
}
else {
current_time += remaining_burst_time[i];
processes[i].waiting_time = current_time - processes[i].arrival_time - processes[i].burst_time;
processes[i].turnaround_time = current_time - processes[i].arrival_time;
total_waiting_time += processes[i].waiting_time;
total_turnaround_time += processes[i].turnaround_time;
remaining_burst_time[i] = 0;
}
}
}
if (done == 1) break;
}
printf("PID\tAT\tBT\tPri\tWT\tTAT\n");
for (i = 0; i < n; i++) {
printf("%d\t%d\t%d\t%d\t%d\t%d\n", processes[i].pid, processes[i].arrival_time, processes[i].burst_time, processes[i].priority, processes[i].waiting_time, processes[i].turnaround_time);
}
printf("Average waiting time: %.2f\n", (float)total_waiting_time / n);
printf("Average turnaround time: %.2f\n", (float)total_turnaround_time / n);
}
void ps(process* processes, int n) {
int i, j;
int current_time = 0;
int total_waiting_time = 0;
int total_turnaround_time = 0;
int remaining_burst_time[n];
int remaining_priority[n];
memset(remaining_burst_time, 0, sizeof(remaining_burst_time));
memset(remaining_priority, 0, sizeof(remaining_priority));
for (i = 0; i < n; i++) {
remaining_burst_time[i] = processes[i].burst_time;
remaining_priority[i] = processes[i].priority;
}
while (1) {
int done = 1;
int max_priority = -1;
int max_priority_index = -1;
for (i = 0; i < n; i++) {
if (remaining_burst_time[i] > 0) {
done = 0;
if (remaining_priority[i] > max_priority) {
max_priority = remaining_priority[i];
max_priority_index = i;
}
}
}
if (done == 1) break;
remaining_burst_time[max_priority_index]--;
current_time++;
if (remaining_burst_time[max_priority_index] == 0) {
processes[max_priority_index].waiting_time = current_time - processes[max_priority_index].arrival_time - processes[max_priority_index].burst_time;
processes[max_priority_index].turnaround_time = current_time - processes[max_priority_index].arrival_time;
total_waiting_time += processes[max_priority_index].waiting_time;
total_turnaround_time += processes[max_priority_index].turnaround_time;
remaining_priority[max_priority_index] = -1;
}
else {
remaining_priority[max_priority_index]--;
}
}
printf("PID\tAT\tBT\tPri\tWT\tTAT\n");
for (i = 0; i < n; i++) {
printf("%d\t%d\t%d\t%d\t%d\t%d\n", processes[i].pid, processes[i].arrival_time, processes[i].burst_time, processes[i].priority, processes[i].waiting_time, processes[i].turnaround_time);
}
printf("Average waiting time: %.2f\n", (float)total_waiting_time / n);
printf("Average turnaround time: %.2f\n", (float)total_turnaround_time / n);
}
```
这个程序模拟了三种常见进程调度算法:先来先服务(FCFS)、时间片轮转(RR)和优先级调度(PS)。您可以将进程的信息修改或添加到程序中,并运行程序进行模拟调度。
阅读全文