Saturday, April 5, 2014

Shortest Job First CPU Scheduling : C++

// Shortest Job First CPU Scheduling.cpp

#include "stdafx.h"
#include <iostream>

using namespace std;

void main()
{
cout<<"Enter the number of processes: ";
int nProc= 0;
cin>>nProc;

int data[10][4]; //data[][3] stores the id of processes which otherwise gets disturbed after sorting
cout<<"Enter the burst times of processes: ";
for(int i =0;i<nProc;++i)
{
cin>>data[i][0];
data[i][1] = 0;
data[i][2] = 0;
data[i][3] = i;
}

//Now sort according to the burst times of the processes: (bubble sort used)
for(int outer = 0;outer<nProc;++outer)
{
for(int inner = 0;inner<nProc-outer-1;++inner)
{
if(data[inner][0]>data[inner+1][0])
{
int temp = data[inner][0];
data[inner][0] = data[inner+1][0];
data[inner+1][0] = temp;

temp = data[inner][3];
data[inner][3] = data[inner+1][3];
data[inner+1][3] = temp;
}
}
}

//Calculation of waiting times
for(int i =1;i<nProc;++i)
{
 data[i][1]=data[i-1][1]+data[i-1][0];
}

//calculation of TAT (Turn around time)
for(int i=0;i<nProc;++i)
{
 data[i][2] = data[i][0]+data[i][1];
}

//show results
for(int i = 0;i<nProc;++i)
{
 cout<<"\nProcess ID "<<data[i][3]<<endl;
 cout<<"Burst time : "<<data[i][0]<<endl;
 cout<<"Waiting time: "<<data[i][1]<<endl;
 cout<<"TAT: "<<data[i][2]<<endl;
 cout<<endl;
}
}

No comments:

Post a Comment