在VMware中实现时间片轮转调度算法和静态优先级调度算法,代码实现
时间: 2024-03-13 16:45:35 浏览: 70
时间片轮转法和优先级调度算法
5星 · 资源好评率100%
时间片轮转调度算法的实现代码如下:
```c
#include<stdio.h>
#include<stdlib.h>
typedef struct process{
int pid;
int burst_time;
int remaining_time;
}Process;
void round_robin(Process *p, int n, int time_quantum){
int time = 0;
int completed = 0;
int *turnaround_time, *waiting_time;
float average_turnaround_time = 0, average_waiting_time = 0;
turnaround_time = (int*)malloc(n*sizeof(int));
waiting_time = (int*)malloc(n*sizeof(int));
for(int i=0; i<n; i++){
p[i].remaining_time = p[i].burst_time;
}
while(completed != n){
for(int i=0; i<n; i++){
if(p[i].remaining_time > 0){
if(p[i].remaining_time <= time_quantum){
time += p[i].remaining_time;
turnaround_time[i] = time - p[i].burst_time;
waiting_time[i] = turnaround_time[i] - p[i].burst_time;
p[i].remaining_time = 0;
completed++;
}
else{
time += time_quantum;
p[i].remaining_time -= time_quantum;
}
}
}
}
printf("PID\tBurst Time\tTurnaround Time\tWaiting Time\n");
for(int i=0; i<n; i++){
printf("%d\t%d\t\t%d\t\t%d\n", p[i].pid, p[i].burst_time, turnaround_time[i], waiting_time[i]);
average_turnaround_time += turnaround_time[i];
average_waiting_time += waiting_time[i];
}
average_turnaround_time /= n;
average_waiting_time /= n;
printf("Average Turnaround Time: %f\n", average_turnaround_time);
printf("Average Waiting Time: %f\n", average_waiting_time);
free(turnaround_time);
free(waiting_time);
}
int main(){
int n, time_quantum;
Process *p;
printf("Enter the number of processes: ");
scanf("%d", &n);
p = (Process*)malloc(n*sizeof(Process));
for(int i=0; i<n; i++){
printf("Enter burst time for process %d: ", i+1);
scanf("%d", &p[i].burst_time);
p[i].pid = i+1;
}
printf("Enter time quantum: ");
scanf("%d", &time_quantum);
round_robin(p, n, time_quantum);
free(p);
return 0;
}
```
静态优先级调度算法的实现代码如下:
```c
#include<stdio.h>
#include<stdlib.h>
typedef struct process{
int pid;
int burst_time;
int priority;
}Process;
void priority_scheduling(Process *p, int n){
int *turnaround_time, *waiting_time;
float average_turnaround_time = 0, average_waiting_time = 0;
turnaround_time = (int*)malloc(n*sizeof(int));
waiting_time = (int*)malloc(n*sizeof(int));
for(int i=0; i<n; i++){
for(int j=i+1; j<n; j++){
if(p[i].priority < p[j].priority){
Process temp = p[i];
p[i] = p[j];
p[j] = temp;
}
}
}
int time = 0;
for(int i=0; i<n; i++){
waiting_time[i] = time;
time += p[i].burst_time;
turnaround_time[i] = time;
}
printf("PID\tBurst Time\tPriority\tTurnaround Time\tWaiting Time\n");
for(int 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].priority, turnaround_time[i], waiting_time[i]);
average_turnaround_time += turnaround_time[i];
average_waiting_time += waiting_time[i];
}
average_turnaround_time /= n;
average_waiting_time /= n;
printf("Average Turnaround Time: %f\n", average_turnaround_time);
printf("Average Waiting Time: %f\n", average_waiting_time);
free(turnaround_time);
free(waiting_time);
}
int main(){
int n;
Process *p;
printf("Enter the number of processes: ");
scanf("%d", &n);
p = (Process*)malloc(n*sizeof(Process));
for(int i=0; i<n; i++){
printf("Enter burst time for process %d: ", i+1);
scanf("%d", &p[i].burst_time);
printf("Enter priority for process %d: ", i+1);
scanf("%d", &p[i].priority);
p[i].pid = i+1;
}
priority_scheduling(p, n);
free(p);
return 0;
}
```
阅读全文