用一个C语言模拟代码实现PSA(优先级)和RR(时间片轮转)操作系统
时间: 2023-10-29 18:04:21 浏览: 103
时间片轮转算法和优先级调度算法-C语言模拟实现-收藏.doc
好的,我可以为您提供一下代码实现。
首先,我们来看一下PSA(优先级)操作系统的代码实现:
```c
#include<stdio.h>
struct process{
int pid;
int burst_time;
int priority;
int waiting_time;
int turnaround_time;
};
void sort_by_priority(struct process p[], int n){
struct process temp;
int i, j;
for(i=0; i<n-1; i++){
for(j=0; j<n-i-1; j++){
if(p[j].priority > p[j+1].priority){
temp = p[j];
p[j] = p[j+1];
p[j+1] = temp;
}
}
}
}
void calculate_waiting_time(struct process p[], int n){
int i;
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;
}
}
void calculate_turnaround_time(struct process p[], int n){
int i;
for(i=0; i<n; i++){
p[i].turnaround_time = p[i].waiting_time + p[i].burst_time;
}
}
void calculate_avg_time(struct process p[], int n){
int i, total_wt=0, total_tat=0;
calculate_waiting_time(p, n);
calculate_turnaround_time(p, n);
printf("\nProcess\tBurst time\tPriority\tWaiting time\tTurnaround time\n");
for(i=0; i<n; i++){
total_wt += p[i].waiting_time;
total_tat += p[i].turnaround_time;
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\n", p[i].pid, p[i].burst_time, p[i].priority, p[i].waiting_time, p[i].turnaround_time);
}
printf("Average waiting time = %f\n", (float)total_wt/n);
printf("Average turnaround time = %f\n", (float)total_tat/n);
}
int main(){
int i, n;
struct process p[10];
printf("Enter the number of processes: ");
scanf("%d", &n);
printf("Enter the burst time and priority of each process:\n");
for(i=0; i<n; i++){
p[i].pid = i+1;
printf("Process %d: ", i+1);
scanf("%d %d", &p[i].burst_time, &p[i].priority);
}
sort_by_priority(p, n);
calculate_avg_time(p, n);
return 0;
}
```
接下来,是RR(时间片轮转)操作系统的代码实现:
```c
#include<stdio.h>
struct process{
int pid;
int burst_time;
int remaining_time;
int waiting_time;
int turnaround_time;
};
void calculate_waiting_time(struct process p[], int n, int tq){
int i, t=0, remaining=n;
while(remaining!=0){
for(i=0; i<n; i++){
if(p[i].remaining_time > tq){
t += tq;
p[i].remaining_time -= tq;
}
else if(p[i].remaining_time > 0){
t += p[i].remaining_time;
p[i].waiting_time = t - p[i].burst_time;
p[i].remaining_time = 0;
remaining--;
}
}
}
}
void calculate_turnaround_time(struct process p[], int n){
int i;
for(i=0; i<n; i++){
p[i].turnaround_time = p[i].waiting_time + p[i].burst_time;
}
}
void calculate_avg_time(struct process p[], int n, int tq){
int i, total_wt=0, total_tat=0;
calculate_waiting_time(p, n, tq);
calculate_turnaround_time(p, n);
printf("\nProcess\tBurst time\tWaiting time\tTurnaround time\n");
for(i=0; i<n; i++){
total_wt += p[i].waiting_time;
total_tat += p[i].turnaround_time;
printf("%d\t\t%d\t\t%d\t\t%d\n", p[i].pid, p[i].burst_time, p[i].waiting_time, p[i].turnaround_time);
}
printf("Average waiting time = %f\n", (float)total_wt/n);
printf("Average turnaround time = %f\n", (float)total_tat/n);
}
int main(){
int i, n, tq;
struct process p[10];
printf("Enter the number of processes: ");
scanf("%d", &n);
printf("Enter the burst time of each process:\n");
for(i=0; i<n; i++){
p[i].pid = i+1;
printf("Process %d: ", i+1);
scanf("%d", &p[i].burst_time);
p[i].remaining_time = p[i].burst_time;
}
printf("Enter time quantum: ");
scanf("%d", &tq);
calculate_avg_time(p, n, tq);
return 0;
}
```
希望能对您有所帮助!
阅读全文