快期末了在复习操作系统,顺便把实验课上写的算法重新写一遍
先来先服务算法非常简单,当作业来了,先来先运行,后来的按到达时间的先后排在就绪队列上,每次取队首元素运行。
实验环境是WIN7 64位+Visual Studio 2015
//main.cpp
#include "FCFS.h"
int main()
{
std::vector PCBList;
//输入作业信息
InputPCB(PCBList);
//FCFS算法
FCFS(PCBList);
//显示结果
show(PCBList);
return 0;
}
//FCFS.h
#ifndef FCFS_H_
#define FCFS_H_
#include
#include
#include
#include
//作业结构体
typedef struct PCB
{
int ID; //标识符
int ComeTime; //到达时间
int ServerTime; //服务时间
int FinishTime; //完成时间
int TurnoverTime; //周转时间
double WeightedTurnoverTime; //带权周转时间
}PCB;
/*
函数功能:输入作业信息
参数说明:
PCBList std::vector& PCB链
*/
void InputPCB(std::vector &PCBList);
/*
函数功能:FCFS算法
参数说明:
PCBList std::vector& PCB链
*/
void FCFS(std::vector &PCBList);
/*
函数功能:显示结果
参数说明:
PCBList std::vector& PCB链
*/
void show(std::vector &PCBList);
/*
函数功能:比较函数,用于sort(),按ComeTime升序排列
参数说明:
p1 const PCB& PCB
p2 const PCB& PCB
*/
bool cmp(const PCB &p1, const PCB &p2);
#endif
//FCFS.cpp
#include "FCFS.h"
void InputPCB(std::vector &PCBList)
{
do {
PCB temp;
std::cout << "输入标识符: ";
std::cin >> temp.ID;
std::cout << "输入到达时间: ";
std::cin >> temp.ComeTime;
std::cout << "输入服务时间: ";
std::cin >> temp.ServerTime;
PCBList.push_back(temp);
std::cout << "继续输入?Y/N: ";
char ans;
std::cin >> ans;
if ('Y' == ans || 'y' == ans)
continue;
else
break;
} while (true);
}
void FCFS(std::vector &PCBList)
{
std::sort(PCBList.begin(), PCBList.end(), cmp); //按升序排列ComeTime
int FinishTime = -1; //上一个作业的完成时间
for (std::vector::iterator it = PCBList.begin(); it < PCBList.end(); ++it)
{
if ((*it).ComeTime < FinishTime) //作业到达时,上一个作业还没有结束
(*it).FinishTime = (*(it - 1)).FinishTime + (*it).ServerTime;
else
(*it).FinishTime = (*it).ComeTime + (*it).ServerTime;
(*it).TurnoverTime = (*it).FinishTime - (*it).ComeTime;
(*it).WeightedTurnoverTime = (double)(*it).TurnoverTime / (*it).ServerTime;
FinishTime = (*it).FinishTime;
}
}
void show(std::vector &PCBList)
{
int SumTurnoverTime = 0;
double SumWeightedTurnoverTime = 0;
std::cout.setf(std::ios::left);
std::cout << std::setw(20) << "标识符";
for (std::vector::iterator it = PCBList.begin(); it < PCBList.end(); ++it)
std::cout << std::setw(5) << (*it).ID;
std::cout << std::endl;
std::cout << std::setw(20) << "到达时间";
for (std::vector::iterator it = PCBList.begin(); it < PCBList.end(); ++it)
std::cout << std::setw(5) << (*it).ComeTime;
std::cout << std::endl;
std::cout << std::setw(20) << "服务时间";
for (std::vector::iterator it = PCBList.begin(); it < PCBList.end(); ++it)
std::cout << std::setw(5) << (*it).ServerTime;
std::cout << std::endl;
std::cout << std::setw(20) << "完成时间";
for (std::vector::iterator it = PCBList.begin(); it < PCBList.end(); ++it)
std::cout << std::setw(5) << (*it).FinishTime;
std::cout << std::endl;
std::cout << std::setw(20) << "周转时间";
for (std::vector::iterator it = PCBList.begin(); it < PCBList.end(); ++it)
{
std::cout << std::setw(5) << (*it).TurnoverTime;
SumTurnoverTime += (*it).TurnoverTime;;
}
std::cout << std::endl;
std::cout << std::setw(20) << "带权周转时间";
for (std::vector::iterator it = PCBList.begin(); it < PCBList.end(); ++it)
{
std::cout << std::setw(5) << (*it).WeightedTurnoverTime;
SumWeightedTurnoverTime += (*it).WeightedTurnoverTime;;
}
std::cout << std::endl;
std::cout << "平均周转时间: " << (double)SumTurnoverTime / PCBList.size() << std::endl;
std::cout << "平均带权周转时间: " << SumWeightedTurnoverTime / PCBList.size() << std::endl;
}
bool cmp(const PCB &p1, const PCB &p2)
{
return p1.ComeTime < p2.ComeTime;
}