先来先服务调度算法、短作业优先算法、时间片轮转和优先级调度算法c语言代码
时间: 2023-09-02 10:07:06 浏览: 110
先来先服务调度算法的C语言代码:
```
#include<stdio.h>
int main(){
int n,p[20],bt[20],wt[20],tat[20],i,j;
float avwt=0,avtat=0;
printf("Enter total number of processes(maximum 20):");
scanf("%d",&n);
printf("\nEnter Process Burst Time\n");
for(i=0;i<n;i++){
printf("P[%d]:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1;
}
for(i=0;i<n;i++){
for(j=0;j<n-i-1;j++){
if(bt[j]>bt[j+1]){
int temp=bt[j];
bt[j]=bt[j+1];
bt[j+1]=temp;
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;
}
}
}
wt[0]=0;
for(i=1;i<n;i++){
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
avwt+=wt[i];
}
avwt/=n;
printf("\nProcess\t Burst Time\tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++){
tat[i]=bt[i]+wt[i];
avtat+=tat[i];
printf("\nP[%d]\t\t%d\t\t%d\t\t%d",p[i],bt[i],wt[i],tat[i]);
}
avtat/=n;
printf("\n\nAverage Waiting Time:%.2f",avwt);
printf("\nAverage Turnaround Time:%.2f",avtat);
return 0;
}
```
短作业优先算法的C语言代码:
```
#include<stdio.h>
int main(){
int n,i,j,p[20],bt[20],wt[20],tat[20],temp;
float avwt=0,avtat=0;
printf("Enter total number of processes(maximum 20):");
scanf("%d",&n);
printf("\nEnter Process Burst Time\n");
for(i=0;i<n;i++){
printf("P[%d]:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1;
}
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(bt[i]>bt[j]){
temp=bt[i];
bt[i]=bt[j];
bt[j]=temp;
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}
wt[0]=0;
for(i=1;i<n;i++){
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
avwt+=wt[i];
}
avwt/=n;
printf("\nProcess\t Burst Time\tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++){
tat[i]=bt[i]+wt[i];
avtat+=tat[i];
printf("\nP[%d]\t\t%d\t\t%d\t\t%d",p[i],bt[i],wt[i],tat[i]);
}
avtat/=n;
printf("\n\nAverage Waiting Time=%.2f",avwt);
printf("\nAverage Turnaround Time=%.2f",avtat);
return 0;
}
```
时间片轮转算法的C语言代码:
```
#include<stdio.h>
int main(){
int n,i,j,p[20],bt[20],wt[20],tat[20],tq;
int avwt=0,avtat=0;
printf("Enter total number of processes(maximum 20):");
scanf("%d",&n);
printf("\nEnter Process Burst Time\n");
for(i=0;i<n;i++){
printf("P[%d]:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1;
}
printf("\nEnter Time Quantum:");
scanf("%d",&tq);
int rt[n];
for(i=0;i<n;i++)
rt[i]=bt[i];
int time=0,flag=0;
while(1){
flag=0;
for(i=0;i<n;i++){
if(rt[i]>0){
flag=1;
if(rt[i]>tq){
time+=tq;
rt[i]-=tq;
}
else{
time=time+rt[i];
wt[i]=time-bt[i];
rt[i]=0;
}
}
}
if(flag==0)
break;
}
printf("\nProcess\t Burst Time\tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++){
tat[i]=bt[i]+wt[i];
avwt+=wt[i];
avtat+=tat[i];
printf("\nP[%d]\t\t%d\t\t%d\t\t%d",p[i],bt[i],wt[i],tat[i]);
}
avwt/=n;
avtat/=n;
printf("\n\nAverage Waiting Time:%d",avwt);
printf("\nAverage Turnaround Time:%d",avtat);
return 0;
}
```
优先级调度算法的C语言代码:
```
#include<stdio.h>
int main(){
int n,i,j,p[20],bt[20],wt[20],tat[20],pr[20],temp;
float avwt=0,avtat=0;
printf("Enter total number of processes(maximum 20):");
scanf("%d",&n);
printf("\nEnter Process Burst Time and Priority\n");
for(i=0;i<n;i++){
printf("\nP[%d]\n",i+1);
printf("Burst Time:");
scanf("%d",&bt[i]);
printf("Priority:");
scanf("%d",&pr[i]);
p[i]=i+1;
}
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(pr[i]>pr[j]){
temp=pr[i];
pr[i]=pr[j];
pr[j]=temp;
temp=bt[i];
bt[i]=bt[j];
bt[j]=temp;
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}
wt[0]=0;
for(i=1;i<n;i++){
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
avwt+=wt[i];
}
avwt/=n;
printf("\nProcess\t Burst Time\t Priority\tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++){
tat[i]=bt[i]+wt[i];
avtat+=tat[i];
printf("\nP[%d]\t\t%d\t\t%d\t\t%d\t\t%d",p[i],bt[i],pr[i],wt[i],tat[i]);
}
avtat/=n;
printf("\n\nAverage Waiting Time=%.2f",avwt);
printf("\nAverage Turnaround Time=%.2f",avtat);
return 0;
}
```
阅读全文