操作系统短作业优先调度算法

项目名称:短作业优先调度算法(非抢占式)

短作业优先调度算法(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++ 运行,结果如下:

 

  • 12
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值