处理机调度c语言
时间: 2023-07-02 08:11:22 浏览: 154
单处理机调度_C语言_
处理机调度是操作系统中的一个重要功能,其作用是决定在何时执行哪个进程。C语言可以用来实现处理机调度算法。以下是一个简单的处理机调度程序实现,使用了Round Robin算法。
```
#include <stdio.h>
#include <stdlib.h>
#define MAX 1000
struct Process {
int pid;
int burst_time;
int remaining_time;
int arrival_time;
};
int main() {
int n, quantum_time;
struct Process p[MAX];
int waiting_time[MAX], turnaround_time[MAX];
int total_waiting_time = 0, total_turnaround_time = 0, total_burst_time = 0;
int i, j, time = 0;
printf("Enter the number of processes: ");
scanf("%d", &n);
printf("Enter time quantum: ");
scanf("%d", &quantum_time);
for(i = 0; i < n; i++) {
p[i].pid = i+1;
printf("Enter burst time for process %d: ", i+1);
scanf("%d", &p[i].burst_time);
p[i].remaining_time = p[i].burst_time;
printf("Enter arrival time for process %d: ", i+1);
scanf("%d", &p[i].arrival_time);
total_burst_time += p[i].burst_time;
}
while(total_burst_time > 0) {
for(i = 0; i < n; i++) {
if(p[i].remaining_time == 0) continue;
if(p[i].remaining_time > quantum_time) {
p[i].remaining_time -= quantum_time;
total_burst_time -= quantum_time;
time += quantum_time;
} else {
total_burst_time -= p[i].remaining_time;
time += p[i].remaining_time;
p[i].remaining_time = 0;
turnaround_time[i] = time - p[i].arrival_time;
waiting_time[i] = turnaround_time[i] - p[i].burst_time;
total_waiting_time += waiting_time[i];
total_turnaround_time += turnaround_time[i];
}
}
}
printf("\nPID\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, waiting_time[i], turnaround_time[i]);
}
printf("\nAverage waiting time: %.2f", (float)total_waiting_time/n);
printf("\nAverage turnaround time: %.2f", (float)total_turnaround_time/n);
return 0;
}
```
在此程序中,用户输入了进程的数量和时间片大小,然后输入每个进程的到达时间和执行时间。程序根据Round Robin算法执行,并计算出每个进程的等待时间和周转时间。最后,程序打印出每个进程的执行数据和平均等待时间和周转时间。
阅读全文