一个c语言代码实现先来先服务调度算法、短作业优先算法、时间片轮转和优先级调度算法
时间: 2023-11-17 08:04:20 浏览: 99
先来先服务调度算法(FCFS):
```c
#include <stdio.h>
#define MAX 10
struct Process {
int pid;
int arrival_time;
int burst_time;
int waiting_time;
int turnaround_time;
};
int main() {
struct Process p[MAX];
int n, i;
float avg_waiting_time = 0, avg_turnaround_time = 0;
printf("Enter the number of processes: ");
scanf("%d", &n);
for(i=0; i<n; i++) {
printf("Enter arrival time and burst time for Process %d: ", i+1);
scanf("%d %d", &p[i].arrival_time, &p[i].burst_time);
p[i].pid = i+1;
}
// calculate waiting time and turnaround time
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;
avg_waiting_time += p[i].waiting_time;
avg_turnaround_time += p[i].turnaround_time;
}
// print result
printf("\nProcess\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time\n");
for(i=0; i<n; i++) {
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\n", p[i].pid, p[i].arrival_time, p[i].burst_time, p[i].waiting_time, p[i].turnaround_time);
}
avg_waiting_time /= n;
avg_turnaround_time /= n;
printf("Average Waiting Time: %f\n", avg_waiting_time);
printf("Average Turnaround Time: %f\n", avg_turnaround_time);
return 0;
}
```
短作业优先算法(SJF):
```c
#include <stdio.h>
#define MAX 10
struct Process {
int pid;
int arrival_time;
int burst_time;
int waiting_time;
int turnaround_time;
int is_completed;
};
int main() {
struct Process p[MAX];
int n, i, j;
float avg_waiting_time = 0, avg_turnaround_time = 0;
printf("Enter the number of processes: ");
scanf("%d", &n);
for(i=0; i<n; i++) {
printf("Enter arrival time and burst time for Process %d: ", i+1);
scanf("%d %d", &p[i].arrival_time, &p[i].burst_time);
p[i].pid = i+1;
p[i].is_completed = 0;
}
int time = 0, completed = 0;
while(completed != n) {
int shortest = -1;
for(i=0; i<n; i++) {
if(!p[i].is_completed && p[i].arrival_time <= time) {
if(shortest == -1 || p[i].burst_time < p[shortest].burst_time) {
shortest = i;
}
}
}
if(shortest != -1) {
p[shortest].waiting_time = time - p[shortest].arrival_time;
p[shortest].turnaround_time = p[shortest].waiting_time + p[shortest].burst_time;
avg_waiting_time += p[shortest].waiting_time;
avg_turnaround_time += p[shortest].turnaround_time;
p[shortest].is_completed = 1;
completed++;
time += p[shortest].burst_time;
} else {
time++;
}
}
// print result
printf("\nProcess\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time\n");
for(i=0; i<n; i++) {
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\n", p[i].pid, p[i].arrival_time, p[i].burst_time, p[i].waiting_time, p[i].turnaround_time);
}
avg_waiting_time /= n;
avg_turnaround_time /= n;
printf("Average Waiting Time: %f\n", avg_waiting_time);
printf("Average Turnaround Time: %f\n", avg_turnaround_time);
return 0;
}
```
时间片轮转算法(RR):
```c
#include <stdio.h>
#define MAX 10
struct Process {
int pid;
int arrival_time;
int burst_time;
int remaining_time;
int waiting_time;
int turnaround_time;
int is_completed;
};
int main() {
struct Process p[MAX];
int n, i, j, time_quantum;
float avg_waiting_time = 0, avg_turnaround_time = 0;
printf("Enter the number of processes: ");
scanf("%d", &n);
for(i=0; i<n; i++) {
printf("Enter arrival time and burst time for Process %d: ", i+1);
scanf("%d %d", &p[i].arrival_time, &p[i].burst_time);
p[i].pid = i+1;
p[i].remaining_time = p[i].burst_time;
p[i].is_completed = 0;
}
printf("Enter time quantum: ");
scanf("%d", &time_quantum);
int time = 0, completed = 0;
while(completed != n) {
for(i=0; i<n; i++) {
if(!p[i].is_completed && p[i].arrival_time <= time) {
int execute_time = time_quantum;
if(p[i].remaining_time < execute_time) {
execute_time = p[i].remaining_time;
}
p[i].remaining_time -= execute_time;
time += execute_time;
if(p[i].remaining_time == 0) {
p[i].waiting_time = time - p[i].arrival_time - p[i].burst_time;
if(p[i].waiting_time < 0) {
p[i].waiting_time = 0;
}
p[i].turnaround_time = p[i].waiting_time + p[i].burst_time;
avg_waiting_time += p[i].waiting_time;
avg_turnaround_time += p[i].turnaround_time;
p[i].is_completed = 1;
completed++;
}
}
}
}
// print result
printf("\nProcess\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time\n");
for(i=0; i<n; i++) {
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\n", p[i].pid, p[i].arrival_time, p[i].burst_time, p[i].waiting_time, p[i].turnaround_time);
}
avg_waiting_time /= n;
avg_turnaround_time /= n;
printf("Average Waiting Time: %f\n", avg_waiting_time);
printf("Average Turnaround Time: %f\n", avg_turnaround_time);
return 0;
}
```
优先级调度算法:
```c
#include <stdio.h>
#define MAX 10
struct Process {
int pid;
int arrival_time;
int burst_time;
int priority;
int waiting_time;
int turnaround_time;
int is_completed;
};
int main() {
struct Process p[MAX];
int n, i, j;
float avg_waiting_time = 0, avg_turnaround_time = 0;
printf("Enter the number of processes: ");
scanf("%d", &n);
for(i=0; i<n; i++) {
printf("Enter arrival time, burst time and priority for Process %d: ", i+1);
scanf("%d %d %d", &p[i].arrival_time, &p[i].burst_time, &p[i].priority);
p[i].pid = i+1;
p[i].is_completed = 0;
}
int time = 0, completed = 0;
while(completed != n) {
int highest_priority = -1, index = -1;
for(i=0; i<n; i++) {
if(!p[i].is_completed && p[i].arrival_time <= time) {
if(p[i].priority > highest_priority) {
highest_priority = p[i].priority;
index = i;
}
}
}
if(index != -1) {
p[index].waiting_time = time - p[index].arrival_time;
p[index].turnaround_time = p[index].waiting_time + p[index].burst_time;
avg_waiting_time += p[index].waiting_time;
avg_turnaround_time += p[index].turnaround_time;
p[index].is_completed = 1;
completed++;
time += p[index].burst_time;
} else {
time++;
}
}
// print result
printf("\nProcess\tArrival Time\tBurst Time\tPriority\tWaiting Time\tTurnaround Time\n");
for(i=0; i<n; i++) {
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n", p[i].pid, p[i].arrival_time, p[i].burst_time, p[i].priority, p[i].waiting_time, p[i].turnaround_time);
}
avg_waiting_time /= n;
avg_turnaround_time /= n;
printf("Average Waiting Time: %f\n", avg_waiting_time);
printf("Average Turnaround Time: %f\n", avg_turnaround_time);
return 0;
}
```
阅读全文