用C语言设计一个按先来先服务,算法时间片轮转法,优先数调度算法实现处理器调度的程序。
时间: 2024-03-20 16:39:02 浏览: 86
好的,以下是用C语言实现三种调度算法的程序:
先来先服务(FCFS):
```c
#include <stdio.h>
struct Process {
int id;
int arrival_time;
int burst_time;
int waiting_time;
int turnaround_time;
};
int main() {
int n; // 进程数量
printf("Enter the number of processes: ");
scanf("%d", &n);
struct Process processes[n];
for (int i = 0; i < n; i++) {
printf("Enter arrival time and burst time for process %d: ", i + 1);
scanf("%d %d", &processes[i].arrival_time, &processes[i].burst_time);
processes[i].id = i + 1;
}
// 按照到达时间排序
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (processes[j].arrival_time > processes[j + 1].arrival_time) {
struct Process temp = processes[j];
processes[j] = processes[j + 1];
processes[j + 1] = temp;
}
}
}
// 计算等待时间和周转时间
int current_time = 0;
for (int i = 0; i < n; i++) {
processes[i].waiting_time = current_time - processes[i].arrival_time;
if (processes[i].waiting_time < 0) {
processes[i].waiting_time = 0;
}
processes[i].turnaround_time = processes[i].waiting_time + processes[i].burst_time;
current_time += processes[i].burst_time;
}
// 输出结果
printf("Process\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (int i = 0; i < n; 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);
}
return 0;
}
```
时间片轮转(RR):
```c
#include <stdio.h>
#include <stdlib.h>
struct Process {
int id;
int arrival_time;
int burst_time;
int waiting_time;
int turnaround_time;
int remaining_time;
};
int main() {
int n; // 进程数量
printf("Enter the number of processes: ");
scanf("%d", &n);
int time_quantum; // 时间片大小
printf("Enter the time quantum: ");
scanf("%d", &time_quantum);
struct Process *processes = malloc(n * sizeof(struct Process));
for (int i = 0; i < n; i++) {
printf("Enter arrival time and burst time for process %d: ", i + 1);
scanf("%d %d", &processes[i].arrival_time, &processes[i].burst_time);
processes[i].id = i + 1;
processes[i].remaining_time = processes[i].burst_time;
}
int completed = 0; // 已完成进程数量
int current_time = 0; // 当前时间
while (completed < n) {
// 执行每个进程一次或一个时间片
for (int i = 0; i < n; i++) {
if (processes[i].arrival_time <= current_time && processes[i].remaining_time > 0) {
if (processes[i].remaining_time <= time_quantum) {
current_time += processes[i].remaining_time;
processes[i].remaining_time = 0;
processes[i].waiting_time = current_time - processes[i].arrival_time - processes[i].burst_time;
if (processes[i].waiting_time < 0) {
processes[i].waiting_time = 0;
}
processes[i].turnaround_time = processes[i].waiting_time + processes[i].burst_time;
completed++;
} else {
current_time += time_quantum;
processes[i].remaining_time -= time_quantum;
}
}
}
}
// 输出结果
printf("Process\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (int i = 0; i < n; 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);
}
free(processes);
return 0;
}
```
优先数调度:
```c
#include <stdio.h>
struct Process {
int id;
int arrival_time;
int burst_time;
int priority;
int waiting_time;
int turnaround_time;
};
int main() {
int n; // 进程数量
printf("Enter the number of processes: ");
scanf("%d", &n);
struct Process processes[n];
for (int i = 0; i < n; i++) {
printf("Enter arrival time, burst time, and priority for process %d: ", i + 1);
scanf("%d %d %d", &processes[i].arrival_time, &processes[i].burst_time, &processes[i].priority);
processes[i].id = i + 1;
}
// 按照到达时间排序
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (processes[j].arrival_time > processes[j + 1].arrival_time) {
struct Process temp = processes[j];
processes[j] = processes[j + 1];
processes[j + 1] = temp;
}
}
}
// 按照优先数排序
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (processes[j].priority > processes[j + 1].priority && processes[j].arrival_time <= processes[j + 1].arrival_time) {
struct Process temp = processes[j];
processes[j] = processes[j + 1];
processes[j + 1] = temp;
}
}
}
// 计算等待时间和周转时间
int current_time = 0;
for (int i = 0; i < n; i++) {
processes[i].waiting_time = current_time - processes[i].arrival_time;
if (processes[i].waiting_time < 0) {
processes[i].waiting_time = 0;
}
processes[i].turnaround_time = processes[i].waiting_time + processes[i].burst_time;
current_time += processes[i].burst_time;
}
// 输出结果
printf("Process\tArrival Time\tBurst Time\tPriority\tWaiting Time\tTurnaround Time\n");
for (int i = 0; i < n; i++) {
printf("%d\t%d\t\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].priority, processes[i].waiting_time, processes[i].turnaround_time);
}
return 0;
}
```
阅读全文