oslab4.h
【单处理器进程调度算法模拟,FCFS,RR(q=1),SPN,SRT,HRRN1】
#ifndef OSLAB4_H
#define OSLAB4_H#include
using namespace std;
class SelectionFunction
{
public:
SelectionFunction();
~SelectionFunction();
void InputProcess();
void FCFS();
void RR();
void SPN();
void SRT();
void HRRN();
private:
void OutputInformation( char * name);
int FindTheShortestProcess( int runTime);
int FindHightestRate (int runTime);
int FindShortestRemain(int runTime);
int FindNextExcuteProcess( int currentProcess, int runTime);
void SetProInformation(int currentProcess , int runTime);
void ClearProcessInformation();
//inputed processes information
struct Process
{
char ID;
unsigned int arrivedTime;
unsigned int serverTime;
};
//the calculated information of process
struct ProcessInformation
{
unsigned int finishTime;
unsigned int turnaroundTime;
double rate;
bool isExcuted;
bool isFirst;
int remainTime;
};
int length;
struct Process *process;
struct ProcessInformation *proInformation;
void put();
};
#endif
oslab4.cpp
#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"<>process[i].arrivedTime>>process[i].serverTime;
process[i].ID = 'A'+i;
if (process[i].serverTime == 0)
{
cout<<"Server time can't be zero!"<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
}
}//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)/(double)process[i].serverTime;
if (tempRate > rate)
{
rate = tempRate;
mark = i;
}
}
}
}//!proInformation[i].isExcuted
}
return mark;
}void SelectionFunction::ClearProcessInformation()
{
for (int i = 0;
i < length;
i++)
{
proInformation[i].finishTime = 0;
proInformation[i].turnaroundTime = 0;
proInformation[i].rate = 0;
proInformation[i].turnaroundTime = 0;
proInformation[i].remainTime = 0;
proInformation[i].isExcuted = false;
proInformation[i].isFirst = true;
}
}void SelectionFunction::SetProInformation(int currentProcess , int runTime)
{
proInformation[currentProcess].finishTime = runTime;
proInformation[currentProcess].isExcuted = true;
proInformation[currentProcess].turnaroundTime = proInformation[currentProcess].finishTime - process[currentProcess].arrivedTime;
proInformation[currentProcess].rate = (double)proInformation[currentProcess].turnaroundTime/(double)process[currentProcess].serverTime;
}void SelectionFunction::OutputInformation(char *name)
{
double totalTurnaroundTime = 0;
double totalRate = 0;
cout<<"=========="<
main.cpp
#include
#include"oslab4.h"using namespace std;
int main()
{
SelectionFunction *selectionFunction = new SelectionFunction;
selectionFunction->InputProcess();
selectionFunction->FCFS();
selectionFunction->RR();
selectionFunction->SPN();
selectionFunction->SRT();
selectionFunction->HRRN();
getchar();
getchar();
return 0;
}
执行结果
文章图片
文章图片
调度算法与第七版的《操作系统——精髓和设计原理》书本的P286,调度情况完全一样,只保证在输入书本的那个五个进程信息的情况下调度正确。
完整代码连结地址 os
环境,win7,x64,vs2012
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-