Saturday, April 5, 2014

Priority CPU Scheduling : C++

// PriorityCPUScheduling.cpp

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

using namespace std;

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

cout<<"Enter the burst times of processes: ";
int data[10][5];  //4th column for priority and 5th column for proc id (during sorting)

for(int i =0;i<nProc;++i)
{
cin>>data[i][0];
data[i][1]=0;
data[i][2]=0;
data[i][3]=i;
}

cout<<"Enter the priorities of the processes: ";
for(int i =0;i<nProc;++i)
{
cin>>data[i][4];
}

//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][4]>data[inner+1][4])
  {
   int temp = data[inner][4];
   data[inner][4] = data[inner+1][4];
   data[inner+1][4] = temp;

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

temp = data[inner][0];
data[inner][0] = data[inner+1][0];
data[inner+1][0] = 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