SJF算法
见名思意,就是谁所需的服务时间最短就先服务谁,前提是进程有到达的情况下,没到达肯定不可能执行啦
主要逻辑:
有两点:第一点是排序问题,初始化时进程需要按两点排序那就是按到达时间以及服务时间排序
第二点就是搞定好时间片记录与进程到达的情况,要考虑当前时间多少个进程进来了,这时候进程到达的则按服务时间排序即可
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
struct PCB {
string Name;//进程名字
double arriveTime;//进程到达时间
double serviceTime;//进程需要服务时间
double endTime;//已服务时间
double finishTime;//完成时间点记录
double RunTime;//进程周转时间
string State;//进程状态
};
bool myComapre_ServiceTime(PCB P1, PCB P2) {
//按服务时间排序
return P1.serviceTime < P2.serviceTime;
}
bool myCompare_ArriveTime_ServiceTime(PCB P1, PCB P2) {
//如果到达时间相同则按服务时间排序,如果到达时间不同那么就按到达时间排序
if (P1.arriveTime == P2.arriveTime) {
return P1.serviceTime < P2.serviceTime;
}
//按到达时间排序
return P1.arriveTime < P2.arriveTime;
}
queue<PCB> Change( queue<PCB>q) {
vector<PCB>v;
while (q.size() != 0) {
v.push_back(q.front());
q.pop();
}
sort(v.begin(), v.end(), myComapre_ServiceTime);
for (vector<PCB>::iterator it = v.begin(); it != v.end(); it++) {
q.push(*it);
}
return q;
}
void showData_Vector(vector<PCB>& v) {
cout << "-------------------------------------------------当前进程情况-------------------------------------------------" << endl;
cout << "进程名\t" << "进程到达时间\t" << " 进程服务时间\t" << "进程已服务时间\t" << " 进程完成时间\t" << " 进程周转时间\t" << "进程当前状态\t" << endl;
for_each(v.begin(), v.end(), [](PCB p) {cout << p.Name << "\t " << p.arriveTime
<< "\t\t\t" << p.serviceTime << "\t\t" << p.endTime << "\t\t" << p.finishTime << "\t\t" << p.RunTime << " \t" << p.State << endl; });
cout << "--------------------------------------------------------------------------------------------------------------" << endl;
}
void showData(queue<PCB> &q) {
cout << "-------------------------------------------------当前进程情况-------------------------------------------------" << endl;
cout << "进程名\t" << "进程到达时间\t" << " 进程服务时间\t" << "进程已服务时间\t" << " 进程完成时间\t" << " 进程周转时间\t" << "进程当前状态\t" << endl;
while (q.size() != 0) {
cout << q.front().Name << "\t " << q.front().arriveTime << "\t\t\t"
<< q.front().serviceTime << "\t\t" << q.front().endTime << "\t\t"
<< q.front().finishTime << "\t\t" << q.front().RunTime << " \t" << q.front().State << endl;
q.pop();
}
}
/*
先来先服务算法
*/
void SJF() {
vector<PCB>v;
cout << "请输入进程个数:";
int sum;
int increatment = 0;
cin >> sum;
increatment = sum - 1;
PCB p;
queue<PCB>readyList;
for (int i = 1; i <= sum; i++) {
cout << "请输入第 " << i << " 个进程名字:";
cin >> p.Name;
cout << "请输入第 " << i << " 个进程到达时间:";
cin >> p.arriveTime;
cout << "请输入第 " << i << " 个进程需要服务的时间:";
cin >> p.serviceTime;
p.State = "就绪状态";
p.endTime = 0;
p.finishTime = 0;
p.RunTime = 0;
//把进程放入容器中
v.push_back(p);
}
//按进程到达的时间排序
sort(v.begin(),v.end(), myCompare_ArriveTime_ServiceTime);
//展示当前情况
showData_Vector(v);
//把数据放入队列中
for (vector<PCB>::iterator it = v.begin(); it != v.end(); it++) {
readyList.push(*it);
}
int tempTime = 0;
queue<PCB>endlist;
queue<PCB>runList;
queue<PCB>tempList;
PCB temp;
//获取第一个数据前的操作:
while (1) {
if (tempTime == readyList.front().arriveTime) {
cout << "-------------------------------------------------------" << endl;
cout << "第" << tempTime << "秒"<<readyList.front().Name << "进程到达" << endl;
cout << "-------------------------------------------------------" << endl;
//把进来的进程放入执行队列中
runList.push(readyList.front());
readyList.pop();
break;
}
else {
cout << "第" << tempTime << "秒没有进程被执行" << endl;
tempTime++;
}
}
//showData(readyList);
//拿到第一个开始执行下一步操作:
while (runList.size() != 0 )
{
if (runList.front().serviceTime != 0) {
//如果时间还需服务时间不为0则做相关的加或者减
cout << "第" << tempTime << "秒 -" << runList.front().Name << "- 正在执行" << endl;
tempTime++;
runList.front().endTime++;
runList.front().serviceTime--;
}
if (runList.front().serviceTime == 0) {
//如果第一个数据的所需服务时间为0了那么这个数据所需执行的时间够了则交给下一个程序
cout << "-------------------------------------------------------" << endl;
cout << "第" << tempTime << "秒 -" << runList.front().Name << "- 已执行完毕" << endl;
cout << "-------------------------------------------------------" << endl;
//把结束时间标记上
runList.front().finishTime = tempTime;
//把状态进行修改
runList.front().State = "执行完毕";
//计算周转时间
runList.front().RunTime = tempTime - runList.front().arriveTime;
//放入另一个队列中保存起来
endlist.push(runList.front());
//弹出第一个数据让下一个开始执行
runList.pop();
if (readyList.size() != 0) {
while (readyList.size() != 0) {
if (tempTime >= readyList.front().arriveTime) {
runList.push(readyList.front());
readyList.pop();
}
else {
tempTime++;
if (tempTime == readyList.front().arriveTime) {
runList.push(readyList.front());
readyList.pop();
break;
}
}
}
if (runList.size() != 0) {
if (tempTime >= runList.front().arriveTime) {
cout << "-------------------------------------------------------" << endl;
cout << "第" << runList.front().arriveTime << "秒" << runList.front().Name << "进程到达" << endl;
cout << "-------------------------------------------------------" << endl;
}
}
runList = Change(runList);
}
}
}
queue<PCB>result;
result = endlist;
showData(endlist);
//平均周转时间
double avgTime = 0;
double sumTime = 0;
int size = result.size();
while (result.size() != 0) {
sumTime += result.front().RunTime;
result.pop();
}
avgTime = sumTime / size;
cout << "平均周转时间:" << avgTime << endl;
cout << "--------------------------------------------------------------------------------------------------------------" << endl;
}
int main() {
SJF();
return 0;
}