#include <iostream>
#include <windows.h>
using namespace std;
//#define N 3
typedef struct{
int ID;
int PRIORITY;
int CPUTIME;
int ALLTIME;
int STARTBLOCK;
int BLOCKTIME;
int STATE;
int REACH;
int TIME;
}PROCESS;
void textColor(int color)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
}
int main()
{
int i, max, time, l, ll, time1, flag = 0, total = 0, N, server[10], sum = 0;
PROCESS pro[10];
textColor(13);
cout << "注意:本程序状态代表如下" << endl << "0-运行 1-阻塞 2-就绪 3-结束 4-未到达" << endl << endl;
textColor(15);
cout << "输入进程数:";
cin >> N;
cout << "输入时间片长度。";
cin >> time;
cout << "请输入各进程初始状态:" << endl;
cout << "ID PRIORITY REACH ALLTIME STARTBLOCK BLOCKTIME" << endl;
for (i = 0; i < N; i++)
{
pro[i].CPUTIME = 0;
pro[i].TIME = 0;
cin >> pro[i].ID >> pro[i].PRIORITY >> pro[i].REACH;
cin >> pro[i].ALLTIME >> pro[i].STARTBLOCK >> pro[i].BLOCKTIME;
server[i] = pro[i].ALLTIME;
if (pro[i].REACH == 0)pro[i].STATE = 0;
else pro[i].STATE = 4;
}
do{
cout << endl << "当前时间为" << total;
textColor(12);
cout << endl << "========================各进程状态为======================" << endl;
textColor(15);
cout << "ID PRIORITY CPUTIME ALLTIME STARTBLOCK BLOCKTIME STATE" << endl;
for (i = 0; i < N; i++)
{
cout << pro[i].ID << " " << pro[i].PRIORITY << " " << pro[i].CPUTIME << " ";
cout << pro[i].ALLTIME << " " << pro[i].STARTBLOCK << " " << pro[i].STATE << " ";
total += time;
}
for (i = 0; i < N; i++)
{
if (pro[i].STATE == 4 && pro[i].REACH < total)
{
pro[i].STATE = 1;
}
}
for (i = 0; i < N; i++)
{
time1 = pro[i].ALLTIME;
if (pro[i].STATE == 0)
{
if (pro[i].ALLTIME <= time)
{
//pro[i].CPUTIME+=time1;
pro[i].ALLTIME = 0;
pro[i].STATE = 3;
pro[i].TIME = total - time + time1;
}
else{
//pro[i].CPUTIME+=time;
pro[i].ALLTIME = time;
pro[i].STARTBLOCK--;
if (pro[i].STARTBLOCK == 0)
{
pro[i].STATE = 1;
pro[i].BLOCKTIME = time1;
pro[i].STARTBLOCK = time1;
}
pro[i].PRIORITY = 3;
pro[i].TIME = total;
}
}
if (pro[i].STATE == 1)
{
pro[i].BLOCKTIME--;
if (pro[i].BLOCKTIME == 0)pro[i].STATE = 2;
pro[i].TIME = total;
}
if(pro[i].STATE == 2)
{
//pro[i].CPUTIME+=time;
pro[i].PRIORITY++;
pro[i].TIME = total;
}
}
max = -100;
ll = -1;
l = -1;
for (i = 0; i < N; i++)
{
if (pro[i].PRIORITY > max && (pro[i].STATE = 0 || pro[i].STATE == 2))
{
l = i;
max = pro[i].PRIORITY;
}
if (pro[i].STATE == 0)ll = i;
}
if (l != -1 && l != ll)pro[l].STATE = 1;
if (ll != -1)pro[ll].STATE = 2;
flag = 0;
if (flag ==0)
{
break;
}
}while (1);
cout << endl << "当前时间为" << total;
textColor(12);
cout << endl << "========================各进程状态为======================" << endl;
textColor(15);
cout << "ID PRIORITY CPUTIME ALLTIME STARTBLOCK BLOCKTIME STATE" << endl;
for (i = 0; i < N; i++)
{
cout << pro[i].ID << " " << pro[i].PRIORITY << " " << pro[i].CPUTIME << " ";
cout << pro[i].ALLTIME << " " << pro[i].STARTBLOCK << " " << pro[i].STATE << " ";
total += time;
}
cout << "各个进程运行结束!" << endl;
cout << "进程号 到达时间 结束时间 周转时间 带权周转时间" << endl;
textColor(10);
for (i = 0; i < N;i++)
{
cout << pro[i].ID << " " << pro[i].REACH << " " << pro[i].TIME << " " << pro[i].TIME - pro[i].REACH << " " << (float)(pro[i].TIME - pro[i].REACH) / server[i] << " ";
sum += pro[i].TIME - pro[i].REACH;
}
cout << "平均周转时间为:" << (float)sum / N << endl;
textColor(15);
return 0;
}
05-03