Thursday, 28 November 2013

Shortest job first scheduling (C++ program)

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,i,j,k,bt[10],pid[10],temp,temp1,wt[10],tat[10],twt=0,ttat=0;
//reading process ID and burst time
cout<<"Enter no. of process:";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter burst time for process "<<i+1<<":";
cin>>bt[i];
pid[i]=i;
}
// sorting jobs according to burst time (ascending )
for(i=0;i<n;i++)
{
for(k=i+1;k<n;k++)
{
if( bt[i] > bt[k] )
{
//swapping process id
temp=pid[i];
pid[i]=pid[k];
pid[k]=temp;
//swapping burst time
temp=bt[i];
bt[i]=bt[k];
bt[k]=temp;
}
}
}
// caculating waiting time and turn around time for each process
wt[0]=0;
for(i=0;i<n;i++)
{
wt[i+1]=wt[i]+bt[i];
tat[i]=wt[i]+bt[i];
}
//calculating total turn around time and total waiting time
for(i=0;i<n;i++)
{
twt=twt+wt[i];
ttat=ttat+tat[i];
}
float awt=(float)twt/n;
float atat=(float)ttat/n;
// displaying output
cout<<"Process ID          :";
for(i=0;i<n;i++)
cout<<pid[i]<<"\t";
cout<<"\nBurst Time        :";
for(i=0;i<n;i++)
cout<<bt[i]<<"\t";
cout<<"\nWaiting time      :";
for(i=0;i<n;i++)
cout<<wt[i]<<"\t";
cout<<"\nTurn around time  :";
for(i=0;i<n;i++)
cout<<tat[i]<<"\t";

cout<<"\n";

cout<<"Avg waiting time:"<<awt<<endl;
cout<<"Avg turn around time"<<atat<<endl;
getch();
}

No comments:

Post a Comment