#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;
}
参考链接
更加直观地体现开始时间、结束时间、周转时间和带权周转时间