Sunday 9 August 2015

Implementation of Priority Scheduling algorithm

Note: Please remove getch() and #include<conio.h> from the code if you are executing the code in Linux Operating System.


#include<conio.h>
#include<stdio.h>
#define SIZE 100
void main(){
int swap;
int a[SIZE],b[SIZE],p[SIZE],w[SIZE],pr[SIZE],i,j,k,n,timer=0;
float sum=0;
printf("Enter number of processes: ");
scanf("%d",&n);

for(i=0;i<n;i++){
p[i]=i+1;
printf("Process P%d:\n",i+1);
printf("Arrival Time: ");
scanf("%d",&a[i]);
printf("Burst Time: ");
scanf("%d",&b[i]);
printf("Priority: ");
scanf("%d",&pr[i]);
printf("\n");
}

printf("\nInputs provided\n");
printf("Processes\tArrival Time\tBurst Time\tPriority\n");
for(i=0;i<n;i++){

printf("P%d\t\t",i+1);
printf("%d\t\t%d\t\t%d\n",a[i],b[i],pr[i]);

}

for(i=0;i<n-1;i++)
 for(j=0;j<n-i-1;j++){
 if(pr[j]<pr[j+1]){
  swap=a[j];
  a[j]=a[j+1];
  a[j+1]=swap; 
  swap=b[j];
  b[j]=b[j+1];
  b[j+1]=swap; 
  swap=p[j];
  p[j]=p[j+1];
  p[j+1]=swap; 
   
  swap=pr[j];
  pr[j]=pr[j+1];
  pr[j+1]=swap; 
    
 } 
 
}


printf("\nOrder Of Processing\n");
printf("Processes\tArrival Time\tBurst Time\tPriority\n");
for(i=0;i<n;i++){

printf("P%d\t\t",p[i]);
printf("%d\t\t%d\t\t%d\n",a[i],b[i],pr[i]);

}
printf("\n\n");
timer=a[0];
w[0]=0;
for(i=1;i<n;i++){
timer+=b[i-1];
w[i]=timer-a[i];

}
printf("\n\n");
for(i=0;i<n;i++){
 printf("Waiting time for process P%d : %d \n",p[i],w[i]);
}

for(i=0;i<n;i++){
 sum+=w[i];
}
printf("Average Waiting time : %f \n",sum/n);




printf("\t\tGantt Chart\n");

for(i=0;i<n;i++){
 
printf(" P%d ",p[i]);
for(j=1;j<=b[i];j++)
printf("%c",22); 
}

getch();
}

output sample:
C program for implementation of priority scheduling algorithm

No comments:

Post a Comment