SRT

在这里插入图片描述

#include<windows.h>
#include<iostream>
#include<vector>
#include<time.h>

using namespace std;

int Flag=1;
int PID_Set = 0;
int System_Time = 0;
int Max_Time=7;

struct PCB
{
	bool finish;
	bool run;
	int PID;
	int Arrive_Time;
	int Rest_Time;
	int Run_Time;
	int Begin_Time;
	int Finish_Time;
	int T;
	float W;
};

class Process
{
private:
	vector<PCB>V_S;
	vector<PCB>temp;
public:
	void SRT();
	void Create_PCB();
};

void Process::Create_PCB()
{
	PCB item;
	item.PID = PID_Set++;
	cout << "Please input jobs' Arrive_Time & Run_Time!" << endl;
	cin >> item.Arrive_Time >> item.Run_Time;
	item.Rest_Time = item.Run_Time;
	item.finish = 0;
	item.run = 0;
	V_S.push_back(item);
}

void Process::SRT()
{
	PCB S;
	float T1 = 0, W1 = 0;
	int job,Min_Time,Begin_Number,Rest_Number,pid;
	cout << "Please input the number of jobs!" << endl;
	cin >> job;
	for (int i = 0;i < job;i++)
	{
		Create_PCB();
	}
	while (Flag)
	{
		int flag = 0;
		for (int i = 0;i < V_S.size();i++)
		{
			if (V_S[i].finish == 0)
				flag = 1;
		}
		if (!flag)
		{
			Flag = 0;
		}
		for (int i = 0;i < V_S.size();i++)
		{
			if ((V_S[i].Arrive_Time <= System_Time) && (V_S[i].finish != 1))

			{
				S = V_S[i];
				temp.push_back(S);
			}
		}
		if (!temp.empty())
		{
			Min_Time = Max_Time;
			for (int i = 0;i < temp.size();i++)
			{
				if (temp[i].Rest_Time <= Min_Time)
				{
					Min_Time = temp[i].Rest_Time;
					Begin_Number = i;
				}
			}
			for (int j = 0;j < temp.size();j++)
			{
				if ((temp[j].Rest_Time == Min_Time) && (temp[j].run == 1))
				{
					Begin_Number = j;
				}
			}
			pid = temp[Begin_Number].PID;
			if (V_S[pid].run == 0)
			{
				V_S[pid].Begin_Time = System_Time;
				V_S[pid].run = 1;
			}
			for (int i = 0;i < V_S.size();i++)
			{
				if (V_S[i].PID == pid)
				{
					Rest_Number = i;
				}
			}
			V_S[Rest_Number].Rest_Time--;
			System_Time++;
			if (V_S[Rest_Number].Rest_Time == 0)
			{
				V_S[Rest_Number].Finish_Time = System_Time;
				V_S[Rest_Number].finish = 1;
			}
		}
		else
		{
			System_Time++;
		}
		temp.clear();
	}
	for (int i = 0;i < V_S.size();i++)
	{
		V_S[i].T = V_S[i].Finish_Time - V_S[i].Arrive_Time;
		V_S[i].W = V_S[i].T / float(V_S[i].Run_Time);
		T1 += V_S[i].T;
		W1 += V_S[i].W;
	}
	T1 = T1 / job;
	W1 = W1 / job;
	cout << "T1=" << T1 << endl;
	cout << "W1=" << W1 << endl;
	cout << "PID" << '\t' << "Tin" << '\t' << "Tr" << '\t' << "Ts" << '\t' << "Tc" << '\t' << "T" << '\t' << "W" << endl;
	for (int i = 0;i < V_S.size();i++)
	{
		cout << V_S[i].PID << '\t' << V_S[i].Arrive_Time << '\t' << V_S[i].Run_Time << '\t' << V_S[i].Begin_Time << '\t' << V_S[i].Finish_Time << '\t' << V_S[i].T << '\t' << V_S[i].W << endl;
	}
}

int main()
{
	Process example;
	example.SRT();
	system("pause");
	return 0;
}

在这里插入图片描述

参考链接
更加直观地体现开始时间、结束时间、周转时间和带权周转时间

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值