Implementation of priority scheduling in C
Program:
#include<stdio.h>
void swap(int *a,int *b){
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int main(){
int pno[10],pp[10],wt[10],bt[10],tt[10];
int n,i,j,w1=0,t1=0;
float aw=0.0,at=0.0;
printf("Enter number of processes :\n");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("Enter burst and priority of process-%d: \n",i+1);
scanf("%d%d",&bt[i],&pp[i]);
pno[i]=i+1;
}
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(pp[i] < pp[j]){
swap(&pp[i],&pp[j]);
swap(&bt[i],&bt[j]);
swap(&pno[i],&pno[j]);
}
}
}
wt[0]=0;
tt[0]=bt[0];
for(i=0;i<n;i++){
wt[i+1] = bt[i] + wt [i];
tt[i+1] = tt[i] + bt [i+1];
w1+=wt[i];
t1+=tt[i];
}
aw = (float)w1/n;
at = (float)t1/n;
printf("\nP_no\tBt\tWt\tTt\n");
for(i=0;i<n;i++){
printf("%d\t%d\t%d\t%d\n",pno[i],bt[i],wt[i],tt[i]);
}
printf("Averge waiting time = %f \n",aw);
printf("Averge Turn around time = %f \n",at);
return 0;
}
Output:
Enter number of processes :
4
Enter burst and priority of process-1:
10 4
Enter burst and priority of process-2:
2 2
Enter burst and priority of process-3:
4 1
Enter burst and priority of process-4:
7 3
P_no Bt Wt Tt
3 4 0 4
2 2 4 6
4 7 6 13
1 10 13 23
Average waiting time = 5.750000
Average Turn around time = 11.500000
#include<stdio.h>
void swap(int *a,int *b){
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int main(){
int pno[10],pp[10],wt[10],bt[10],tt[10];
int n,i,j,w1=0,t1=0;
float aw=0.0,at=0.0;
printf("Enter number of processes :\n");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("Enter burst and priority of process-%d: \n",i+1);
scanf("%d%d",&bt[i],&pp[i]);
pno[i]=i+1;
}
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(pp[i] < pp[j]){
swap(&pp[i],&pp[j]);
swap(&bt[i],&bt[j]);
swap(&pno[i],&pno[j]);
}
}
}
wt[0]=0;
tt[0]=bt[0];
for(i=0;i<n;i++){
wt[i+1] = bt[i] + wt [i];
tt[i+1] = tt[i] + bt [i+1];
w1+=wt[i];
t1+=tt[i];
}
aw = (float)w1/n;
at = (float)t1/n;
printf("\nP_no\tBt\tWt\tTt\n");
for(i=0;i<n;i++){
printf("%d\t%d\t%d\t%d\n",pno[i],bt[i],wt[i],tt[i]);
}
printf("Averge waiting time = %f \n",aw);
printf("Averge Turn around time = %f \n",at);
return 0;
}
Output:
Enter number of processes :
4
Enter burst and priority of process-1:
10 4
Enter burst and priority of process-2:
2 2
Enter burst and priority of process-3:
4 1
Enter burst and priority of process-4:
7 3
P_no Bt Wt Tt
3 4 0 4
2 2 4 6
4 7 6 13
1 10 13 23
Average waiting time = 5.750000
Average Turn around time = 11.500000
Comments
Post a Comment