#include"oslab4.h"
SelectionFunction::SelectionFunction()
{
process = NULL;
proInformation = NULL;
length = 0;
}
SelectionFunction::~SelectionFunction()
{
delete [] process;
process = NULL;
delete [] proInformation;
proInformation = NULL;
}
void SelectionFunction::InputProcess()
{
cout<<"Input the numbers of process:";
cin>>length;
if(length<1)
{
cout<<"The numbers of process can't less 1"<<endl;
return ;
}
//dynamic new an array to store process information
process = new struct Process[length];
if(process == NULL)
{
cout<<"New function error! Quit!"<<endl;
return ;
}
proInformation = new struct ProcessInformation[length];
if (proInformation == NULL)
{
cout<<"New function error! Quit!"<<endl;
return ;
}
ClearProcessInformation();
cout<<"Two nonnagetive umbers for process arrive time and sever time:"<<endl;
for (int i = 0; i < length; i++)
{
cin>>process[i].arrivedTime>>process[i].serverTime;
process[i].ID = 'A'+i;
if (process[i].serverTime == 0)
{
cout<<"Server time can't be zero!"<<endl;
return ;
}
}
}
void SelectionFunction::FCFS()
{
int runTime = 0;
for (int i = 0; i < length; i++)
{
if (process[i].arrivedTime>runTime)
{
runTime = process[i].arrivedTime;
}
runTime = runTime + process[i].serverTime;
/*
proInformation[i].finishTime = runTime;
proInformation[i].turnaroundTime = runTime - process[i].arrivedTime; //or = process[i].finishTime - process[i].arrivedTime
proInformation[i].rate = (double)proInformation[i].turnaroundTime/(double)process[i].serverTime;
*/
SetProInformation(i,runTime);
}
OutputInformation("FCFS");
ClearProcessInformation();
}
//q = 1
void SelectionFunction::RR()
{
int runTime = 0;
int excutedProcessNumbers = 0;
int currentProcess = 0;
for (int i = 0; i < length; i++)
{
proInformation[i].remainTime = process[i].serverTime;
}
while (excutedProcessNumbers < length)
{
currentProcess = FindNextExcuteProcess(currentProcess, runTime);
if (currentProcess < 0)
{
++runTime;
}
else
{
--proInformation[currentProcess].remainTime;
++runTime;
if(proInformation[currentProcess].remainTime ==0 )
{
/*
proInformation[currentProcess].finishTime = runTime;
proInformation[currentProcess].turnaroundTime = runTime - process[currentProcess].arrivedTime;
proInformation[currentProcess].rate = (double)proInformation[currentProcess].turnaroundTime/(double)process[currentProcess].serverTime;
proInformation[currentProcess].isExcuted = true;
*/
SetProInformation(currentProcess,runTime);
excutedProcessNumbers++;
}
}
}//end while
OutputInformation("RR(q=1)");
ClearProcessInformation();
}
int SelectionFunction::FindNextExcuteProcess( int currentProcess, int runTime)
{
if (process[0].arrivedTime > runTime)
{
return -1;
}
int tMark = currentProcess;
int mark = --currentProcess;
if(mark<0)
mark+= length;
int excuteTime = 0;
bool isFind = false;
bool isFirstOne = true;
while (excuteTime < length)
{
if(proInformation[mark].isExcuted)
{ }
else
{
if (process[mark].arrivedTime<=runTime)
{
if (isFirstOne)
{
isFirstOne = !isFirstOne;
isFind = true;
currentProcess = mark;
}
else
{
if (tMark == mark)
{ }
else if(( process[mark].arrivedTime<runTime)&&
(process[currentProcess].arrivedTime==runTime))
{
currentProcess = mark;
}
}
}
}//else
--mark;
if(mark<0)
mark+=length;
++excuteTime;
}
if(isFind)
return currentProcess;
return -1;
}
//the shortest process next
void SelectionFunction::SPN()
{
int runTime = 0;
int excutedProcessNumbers = 0;
int currentProcess = 0;
while(excutedProcessNumbers<length)
{
currentProcess = FindTheShortestProcess( runTime);
//at this time ,there isn't any process needs dealt
if(currentProcess<0)
{
runTime++;
}
else
{
runTime = runTime + process[currentProcess].serverTime;
/*
proInformation[currentProcess].isExcuted = true;
proInformation[currentProcess].finishTime = runTime;
proInformation[currentProcess].turnaroundTime = runTime - process[currentProcess].arrivedTime;
proInformation[currentProcess].rate = (double)proInformation[currentProcess].turnaroundTime/(double)process[currentProcess].serverTime;
*/
SetProInformation(currentProcess,runTime);
++excutedProcessNumbers;
}
}
OutputInformation("SPN");
ClearProcessInformation();
}
//
int SelectionFunction::FindTheShortestProcess( int runTime)
{
int mark = -1;
bool isFirstOne = true;
for (int i = 0; i < length; i++)
{
if (!proInformation[i].isExcuted)
{
if (process[i].arrivedTime<=runTime)
{
if(isFirstOne)
{
mark = i;
isFirstOne = !isFirstOne;
}
else
{
if(process[i].serverTime<process[mark].serverTime)
mark = i;
}
}
}//end if(!proInformation[i].isExcuted)
}
return mark;
}
//shortest running next
void SelectionFunction::SRT()
{
int runTime = 0;
int excutedProcessNumbers = 0;
int currentProcess = 0;
for (int i = 0; i < length; i++)
{
proInformation[i].remainTime = process[i].serverTime;
}
while(excutedProcessNumbers < length)
{
currentProcess = FindShortestRemain(runTime);
if(currentProcess < 0)
{
++runTime;
}
else
{
++runTime;
--proInformation[currentProcess].remainTime;
if(proInformation[currentProcess].remainTime == 0)
{
/*
proInformation[currentProcess].finishTime = runTime;
proInformation[currentProcess].turnaroundTime = runTime - process[currentProcess].arrivedTime;
proInformation[currentProcess].rate = (double)proInformation[currentProcess].turnaroundTime/(double)process[currentProcess].serverTime;
proInformation[currentProcess].isExcuted = true;
*/
SetProInformation(currentProcess,runTime);
excutedProcessNumbers++;
}
}
}//end while
OutputInformation("SRT");
ClearProcessInformation();
}
int SelectionFunction::FindShortestRemain(int runTime)
{
bool isFirstOne = true;
int shortestRemainTime = 0;
int mark = -1;
for (int i = 0; i < length; i++)
{
if (!proInformation[i].isExcuted)
{
if(process[i].arrivedTime<=runTime)
{
if(isFirstOne)
{
shortestRemainTime = proInformation[i].remainTime;
mark = i;
isFirstOne = !isFirstOne;
}
else
{
int tempTime = proInformation[i].remainTime;
if(shortestRemainTime > tempTime)
{
shortestRemainTime = tempTime;
mark = i;
}
}
}//end process[i].arrivedTime>=runTime
}
}//end for
return mark;
}
//Highest Response_ratio Next
void SelectionFunction::HRRN( )
{
int currentProcess = 0;
int runTime = 0;
int excutedProcessNumbers = 0;
while (excutedProcessNumbers < length)
{
currentProcess = FindHightestRate(runTime);
if(currentProcess < 0)
{
++runTime;
}
else
{
runTime = runTime + process[currentProcess].serverTime;
SetProInformation(currentProcess, runTime);
++excutedProcessNumbers;
}
}
OutputInformation("HRRN");
ClearProcessInformation();
}
//find the hightest rate process
int SelectionFunction::FindHightestRate(int runTime)
{
int mark = -1;
bool isFirstOne = true;
double rate=0;
for (int i = 0; i < length; i++)
{
if(!proInformation[i].isExcuted)
{
if(process[i].arrivedTime <= runTime)
{
if(isFirstOne)
{
mark = i;
rate = (double)(runTime + process[i].serverTime - process[i].arrivedTime)/(double)process[i].serverTime;
isFirstOne = !isFirstOne;
}
else
{
double tempRate = (double)(runTime + process[i].serverTime - process[i].arrivedTime)