宝塔服务器面板,一键全能部署及管理,送你10850元礼包,点我领取

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

短作业优先SJF算法-风君子博客

//main.cpp
#include "SJF.h"int main)
{std::vector<PCB> PCBList;//输入作业信息InputPCBPCBList);//SJF算法SJFPCBList);//显示结果showPCBList);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 InputPCBstd::vector<PCB> &PCBList);/*
函数功能:SJF算法
参数说明:
PCBList		std::vector<PCB>&		PCB链
*/
void SJFstd::vector<PCB> &PCBList);/*
函数功能:显示结果
参数说明:
PCBList		std::vector<PCB>&		PCB链
*/
void showstd::vector<PCB> &PCBList);/*
函数功能:比较函数,用于sort),按ComeTime升序排列
参数说明:
p1			const PCB&				PCB
p2			const PCB&				PCB
*/
bool CmpByComeTimeconst PCB &p1, const PCB &p2);/*
函数功能:比较函数,用于sort),按ServerTime升序排列
参数说明:
p1			const PCB&				PCB
p2			const PCB&				PCB
*/
bool CmpByServerTimeconst PCB &p1, const PCB &p2);#endif//SJF.cpp
#include "SJF.h"//输入作业信息
void InputPCBstd::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_backtemp);std::cout << "继续输入?Y/N: ";char ans;std::cin >> ans;if 'Y' == ans || 'y' == ans)continue;elsebreak;} while true);
}//这里开始写的有问题。起初我没有写从注释//同时到达的作业按ServerTime降序排列.....这一段,发现程序刚开始运行第一个作业时,当有同时到达的作业,程序可能选取一个作业不是最短的程序运行,比如说我输入(标识符,到达时间,服务时间) 0, 0, 5) 1, 0 ,2),程序可能选取第一个元组运行,所以我在SJF算法运行前先排一次序,选出最先运行的作业
//SJF算法
void SJFstd::vector<PCB> &PCBList)
{//按ComeTime升序排序std::sortPCBList.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::sortPCBList.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::sortit + 1, it + i, CmpByServerTime);}std::sortPCBList.begin), PCBList.end), CmpByComeTime);		//重新排列,用于显示结果
}//显示结果
void showstd::vector<PCB> &PCBList)
{int SumTurnoverTime = 0;double SumWeightedTurnoverTime = 0;std::cout.setfstd::ios::left);std::cout << std::setw20) << "标识符";for std::vector<PCB>::iterator it = PCBList.begin); it < PCBList.end); ++it)std::cout << std::setw5) << *it).ID;std::cout << std::endl;std::cout << std::setw20) << "到达时间";for std::vector<PCB>::iterator it = PCBList.begin); it < PCBList.end); ++it)std::cout << std::setw5) << *it).ComeTime;std::cout << std::endl;std::cout << std::setw20) << "服务时间";for std::vector<PCB>::iterator it = PCBList.begin); it < PCBList.end); ++it)std::cout << std::setw5) << *it).ServerTime;std::cout << std::endl;std::cout << std::setw20) << "完成时间";for std::vector<PCB>::iterator it = PCBList.begin); it < PCBList.end); ++it)std::cout << std::setw5) << *it).FinishTime;std::cout << std::endl;std::cout << std::setw20) << "周转时间";for std::vector<PCB>::iterator it = PCBList.begin); it < PCBList.end); ++it){std::cout << std::setw5) << *it).TurnoverTime;SumTurnoverTime += *it).TurnoverTime;;}std::cout << std::endl;std::cout << std::setw20) << "带权周转时间";for std::vector<PCB>::iterator it = PCBList.begin); it < PCBList.end); ++it){std::cout << std::setw5) << *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 CmpByComeTimeconst PCB &p1, const PCB &p2)
{return p1.ComeTime < p2.ComeTime;
}//按ServerTime升序
bool CmpByServerTimeconst PCB &p1, const PCB &p2)
{return p1.ServerTime < p2.ServerTime;
}