All Programs are Written and Compiled in Dev C++. So, it may generate some error in case of other compilers and may need some modifications in program. Download Dev C++

Friday, 8 August 2014

Shortest Job First (Non-PreEmptive)

/*Shortest Job First Scheduling Progrmam (Non-PreEmptive) */
#include<iostream>
using namespace std;
int main()
{
    int i,j,n,total=0,temp,ptemp;
    int wtime[10],btime[10],process[10];
    float avg=0;
    cout<<"\nEnter number of Processes: ";
    cin>>n;
    for(i=0;i<n;i++)
    {
    process[i]=i;
        cout<<"Enter P"<<i+1<<" Burst Time:";
        cin>>btime[i];
    }
    for(i=0;i<n-1;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(btime[i]>btime[j])
            {
                temp=btime[i];
                btime[i]=btime[j];
                btime[j]=temp;
                ptemp=process[i];
                process[i]=process[j];
                process[j]=ptemp;
            }
        }
    }
        wtime[0]=0;
    for(i=1;i<n;i++)
    {
        wtime[i]=wtime[i-1]+btime[i-1];
        total=total+wtime[i];
    }
    avg=(float)total/n;
    cout<<"\nP_ID\tB_TIME\t W_TIME\n";
    for(i=0;i<n;i++)
        cout<<process[i]+1<<"\t"<<btime[i]<<"\t"<<wtime[i]<<endl;
    cout<<"\nTotal Waiting Time: "<<total;
cout<<"\nAverage Waiting Time: "<<avg;
    return 0;
}

No comments:

Post a Comment