进程调度算法实验报告c语言
时间: 2023-08-17 21:06:03 浏览: 66
以下是进程调度算法实验报告的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_PROCESS 10
typedef struct {
int pid;
int burst_time;
int arrival_time;
int waiting_time;
int turnaround_time;
} process;
void fcfs(process *p, int n);
void sjf(process *p, int n);
void rr(process *p, int n);
int main() {
int n, i, quantum;
process p[MAX_PROCESS];
printf("Enter number of processes: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Process %d:\n", i);
printf("Enter burst time: ");
scanf("%d", &p[i].burst_time);
printf("Enter arrival time: ");
scanf("%d", &p[i].arrival_time);
p[i].pid = i;
}
fcfs(p, n);
sjf(p, n);
printf("Enter time quantum: ");
scanf("%d", &quantum);
rr(p, n, quantum);
return 0;
}
void fcfs(process *p, int n) {
int i, total_waiting_time = 0, total_turnaround_time = 0;
p[0].waiting_time = 0;
for (i = 1; i < n; i++) {
p[i].waiting_time = p[i - 1].waiting_time + p[i - 1].burst_time;
}
for (i = 0; i < n; i++) {
p[i].turnaround_time = p[i].waiting_time + p[i].burst_time;
total_waiting_time += p[i].waiting_time;
total_turnaround_time += p[i].turnaround_time;
}
printf("\nFCFS Scheduling:\n");
printf("Process\tBurst Time\tArrival Time\tWaiting Time\tTurnaround Time\n");
for (i = 0; i < n; i++) {
printf("%d\t%d\t\t%d\t\t%d\t\t%d\n", p[i].pid, p[i].burst_time, p[i].arrival_time, p[i].waiting_time, p[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 sjf(process *p, int n) {
int i, j, min, total_waiting_time = 0, total_turnaround_time = 0;
process temp;
for (i = 0; i < n - 1; i++) {
min = i;
for (j = i + 1; j < n; j++) {
if (p[j].burst_time < p[min].burst_time) {
min = j;
}
}
if (min != i) {
temp = p[i];
p[i] = p[min];
p[min] = temp;
}
}
p[0].waiting_time = 0;
for (i = 1; i < n; i++) {
p[i].waiting_time = p[i - 1].waiting_time + p[i - 1].burst_time;
}
for (i = 0; i < n; i++) {
p[i].turnaround_time = p[i].waiting_time + p[i].burst_time;
total_waiting_time += p[i].waiting_time;
total_turnaround_time += p[i].turnaround_time;
}
printf("\nSJF Scheduling:\n");
printf("Process\tBurst Time\tArrival Time\tWaiting Time\tTurnaround Time\n");
for (i = 0; i < n; i++) {
printf("%d\t%d\t\t%d\t\t%d\t\t%d\n", p[i].pid, p[i].burst_time, p[i].arrival_time, p[i].waiting_time, p[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 *p, int n, int quantum) {
int i, j, time = 0, remaining = n, total_waiting_time = 0, total_turnaround_time = 0;
process temp;
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (p[i].arrival_time > p[j].arrival_time) {
temp = p[i];
p[i] = p[j];
p[j] = temp;
}
}
}
for (i = 0; i < n; i++) {
p[i].waiting_time = 0;
}
while (remaining > 0) {
for (i = 0; i < n; i++) {
if (p[i].burst_time > 0) {
if (p[i].burst_time <= quantum) {
time += p[i].burst_time;
p[i].burst_time = 0;
p[i].turnaround_time = time - p[i].arrival_time;
p[i].waiting_time = p[i].turnaround_time - p[i].burst_time;
total_waiting_time += p[i].waiting_time;
total_turnaround_time += p[i].turnaround_time;
remaining--;
} else {
time += quantum;
p[i].burst_time -= quantum;
}
}
}
}
printf("\nRound Robin Scheduling:\n");
printf("Process\tBurst Time\tArrival Time\tWaiting Time\tTurnaround Time\n");
for (i = 0; i < n; i++) {
printf("%d\t%d\t\t%d\t\t%d\t\t%d\n", p[i].pid, p[i].burst_time, p[i].arrival_time, p[i].waiting_time, p[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)、短作业优先(SJF)和轮转调度(RR)。用户需要输入每个进程的到达时间和执行时间,然后程序将计算每个进程的等待时间和周转时间,并输出平均等待时间和平均周转时间。
在FCFS算法中,进程按照它们到达的顺序执行。在SJF算法中,进程按照它们的执行时间排序,然后按照顺序执行。在RR算法中,进程按照它们到达的顺序排序,然后每个进程都被分配一个时间量(量子),在这个时间量内执行,如果进程还没有完成,那么它重新排队,等待下一次执行。
希望这个代码对你有所帮助!
阅读全文