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],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("\n");
}
printf("\nInputs provided:\n");
printf("Processes\tArrival Time\tBurst Time\n");
for(i=0;i<n;i++){
printf("P%d\t\t",i+1);
printf("%d\t\t%d\n",a[i],b[i]);
}
for(i=0;i<n-1;i++)
for(j=0;j<n-i-1;j++){
if(a[j]>a[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;
}
}
printf("\nOrder of processing:\n");
printf("Processes\tArrival Time\tBurst Time\n");
for(i=0;i<n;i++){
printf("P%d\t\t",p[i]);
printf("%d\t\t%d\n",a[i],b[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]);
}
//Average waiting time
sum=0;
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:
No comments:
Post a Comment