您的位置:首页 > 其它

短作业优先(SJF)算法

2015-11-29 19:49 429 查看
SJF算法:将一些作业按照服务时间升序排列,较短的作业先执行,这样可以保证较好的周转时间,但是同时,长作业可能会等待很长时间才会运行。



//main.cpp
#include "SJF.h"

int main()
{
std::vector<PCB> PCBList;

//输入作业信息
InputPCB(PCBList);

//SJF算法
SJF(PCBList);

//显示结果
show(PCBList);

return 0;
}

//SJF.h
#ifndef SJF_H_
#define SJF_H_

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>

//作业结构体
typedef struct PCB
{
int ID; //标识符
int ComeTime; //到达时间
int ServerTime; //服务时间
int FinishTime; //完成时间
int TurnoverTime; //周转时间
double WeightedTurnoverTime; //带权周转时间
}PCB;

/*
函数功能:输入作业信息
参数说明:
PCBList std::vector<PCB>& PCB链
*/
void InputPCB(std::vector<PCB> &PCBList);

/*
函数功能:SJF算法
参数说明:
PCBList std::vector<PCB>& PCB链
*/
void SJF(std::vector<PCB> &PCBList);

/*
函数功能:显示结果
参数说明:
PCBList std::vector<PCB>& PCB链
*/
void show(std::vector<PCB> &PCBList);

/*
函数功能:比较函数,用于sort(),按ComeTime升序排列
参数说明:
p1 const PCB& PCB
p2 const PCB& PCB
*/
bool CmpByComeTime(const PCB &p1, const PCB &p2);

/*
函数功能:比较函数,用于sort(),按ServerTime升序排列
参数说明:
p1 const PCB& PCB
p2 const PCB& PCB
*/
bool CmpByServerTime(const PCB &p1, const PCB &p2);

#endif

//SJF.cpp
#include "SJF.h"

//输入作业信息
void InputPCB(std::vector<PCB> &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);
}

//这里开始写的有问题。起初我没有写从注释//同时到达的作业按ServerTime降序排列.....这一段,发现程序刚开始运行第一个作业时,当有同时到达的作业,程序可能选取一个作业不是最短的程序运行,比如说我输入(标识符,到达时间,服务时间) (0, 0, 5) (1, 0 ,2),程序可能选取第一个元组运行,所以我在SJF算法运行前先排一次序,选出最先运行的作业
//SJF算法
void SJF(std::vector<PCB> &PCBList)
{
//按ComeTime升序排序
std::sort(PCBList.begin(), PCBList.end(), CmpByComeTime);

//同时到达的作业按ServerTime升序排列,决定首先运行的作业
int i = 1;
std::vector<PCB>::iterator it = PCBList.begin() + 1;
while ((*it).ComeTime == (*(it - 1)).ComeTime)
{
++i;
++it;
}
std::sort(PCBList.begin(), PCBList.begin() + i, CmpByServerTime);

int FinishTime = -1;
for (it = PCBList.begin(); it < PCBList.end(); ++it)
{
if ((*it).ComeTime >= FinishTime) //没有作业正在运行,取队首作业运行
(*it).FinishTime = (*it).ComeTime + (*it).ServerTime;
else //有作业正在运行,等待作业完毕,此作业再运行
(*it).FinishTime = FinishTime + (*it).ServerTime;
(*it).TurnoverTime = (*it).FinishTime - (*it).ComeTime;
(*it).WeightedTurnoverTime = (double)(*it).TurnoverTime / (*it).ServerTime;
FinishTime = (*it).FinishTime;

//在一个作业运行期间,如果有其他作业到达,将他们按照服务时间升序排列
int i = 1;
while ((it + i) < PCBList.end() && (*(it + i)).ComeTime <= FinishTime)
++i;
std::sort(it + 1, it + i, CmpByServerTime);
}

std::sort(PCBList.begin(), PCBList.end(), CmpByComeTime); //重新排列,用于显示结果
}

//显示结果
void show(std::vector<PCB> &PCBList)
{
int SumTurnoverTime = 0;
double SumWeightedTurnoverTime = 0;

std::cout.setf(std::ios::left);

std::cout << std::setw(20) << "标识符";
for (std::vector<PCB>::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<PCB>::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<PCB>::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<PCB>::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<PCB>::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<PCB>::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;
}

//按ComeTime升序
bool CmpByComeTime(const PCB &p1, const PCB &p2)
{
return p1.ComeTime < p2.ComeTime;
}

//按ServerTime升序
bool CmpByServerTime(const PCB &p1, const PCB &p2)
{
return p1.ServerTime < p2.ServerTime;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: