项目名称:短作业优先调度算法(非抢占式)
短作业优先调度算法(SJF)是以作业的长度来计算优先级,作业越短,优先度越高。作业的长短是作业的运行时间来衡量的。
具体可以看https://blog.csdn.net/weixin_41931278/article/details/106025095?utm_source=app
源程序
(源程序C++编写,由老师提供的参考、网上查询、同学之间讨论之后完善,非诚勿扰!!!)
#include<iostream>
using namespace std;
struct Node {
char name; //进程编号
int Tarrive; //到达时间
int Tservice; //服务时间
int Tsurplus; //剩余时间
int Tstart; //开始时间
int Taccomplish;//完成时间
int if_finish; //进程是否完成
int num; //进程个数
}job[10];
//按服务时间排序
void Service_sort(int num)
{
int temp1, temp2, temp3;
for (int i = 1; i < num; i++)
{
for (int j = i + 1; j < num; j++)
{
if (job[i].Tservice > job[j].Tservice)
{
temp1 = job[j].name;
job[j].name = job[i].name;
job[i].name = temp1;
temp2 = job[j].Tarrive;
job[j].Tarrive = job[i].Tarrive;
job[i].Tarrive = temp2;
temp3 = job[j].Tservice;
job[j].Tservice = job[i].Tservice;
job[i].Tservice = temp3;
}
}
}
}
//如果 到达时间相等,服务时间 按 从小到大排序
void Arrive_Short_sort(int num)
{
int temp1, temp2, temp3;
for (int i = 0; i < num; i++)
{
for (int j = i + 1; j < num; j++)
{
if (job[i].Tarrive >= job[j].Tarrive)
{
if (job[i].Tarrive > job[j].Tarrive)
{
temp1 = job[j].name;
job[j].name = job[i].name;
job[i].name = temp1;
temp2 = job[j].Tarrive;
job[j].Tarrive = job[i].Tarrive;
job[i].Tarrive = temp2;
temp3 = job[j].Tservice;
job[j].Tservice = job[i].Tservice;
job[i].Tservice = temp3;
}
else
{
if (job[i].Tservice > job[j].Tservice)
{
temp1 = job[j].name;
job[j].name = job[i].name;
job[i].name = temp1;
temp2 = job[j].Tarrive;
job[j].Tarrive = job[i].Tarrive;
job[i].Tarrive = temp2;
temp3 = job[j].Tservice;
job[j].Tservice = job[i].Tservice;
job[i].Tservice = temp3;
}
}
}
}
}
}
void sjf(int num){//最短作业优先算法
int i;
for(i=0;i<=num;i++){
if(i==0){
job[i].Tstart=job[i].Tarrive;
job[i].Taccomplish=job[i].Tarrive+job[i].Tservice;
}
else{
if(job[i].Tarrive<=job[i-1].Taccomplish){
job[i].Tstart=job[i-1].Taccomplish;
job[i].Taccomplish=job[i-1].Taccomplish+job[i].Tservice;
}
else{
job[i].Tstart=job[i-1].Taccomplish;
job[i].Taccomplish=job[i-1].Taccomplish+job[i].Tservice+job[i].Tarrive;
}
}
}
}
//输出
void print(int num)
{
cout << "进程名" << "\t" << "到达时间" << "\t" << "服务时间" << "\t" << "完成时间" << endl;
for (int i = 0; i < num; i++)
{
cout << job[i].name << "\t" << job[i].Tarrive << "\t\t" << job[i].Tservice << "\t\t" << job[i].Taccomplish << endl;
}
}
//选择即将执行的算法
void display(int num)
{
int ch = 0;
cout << "-------------------------" << endl;
cout << "----------2、SJF算法----------" << endl;
cout << "----------3、退出 -----------" << endl;
cout << "-------------------------" << endl;
do {
cout << "请选择:" << endl;
cin >> ch;
switch (ch) {
case 2:
Service_sort(num);
Arrive_Short_sort(num);
sjf(num);
print(num);
break;
case 3:
exit(1);
}
} while (ch != 3);
}
int main() //主函数
{
int num;
char jname;
int arrive;
int service;
cout << "请输入进程个数:" << endl;
cin >> num;
for (int i = 0; i < num; i++)
{
cout << "请输入进程名、到达时间、服务时间" << endl;
cin >> jname;
job[i].name = jname;
cin >> arrive;
job[i].Tarrive = arrive;
cin >> service;
job[i].Tservice = service;
}
display(num);
return 0;
}
在Bloodshed Dev-C++ 运行,结果如下: