forked from devhg/FuckCATS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RR.cpp
151 lines (147 loc) · 4.88 KB
/
RR.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <iostream>
#include <vector>
using namespace std;
struct Node {
char name;
int Tarrive; //到达时间
int Tservice; //服务时间
int Tsurplus; //剩余时间
int Tstart; //开始时间
int Taccomplish; //完成时间
int prio; //优先级---数字越大优先级越高
int if_finish; //进程是否完成
int num; //进程个数
} job[10];
vector<int> Safeorder; //表示安全序列
//按到达时间排序
void Arrive_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) {
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 RR(int num) // RR算法
{
int q;
cout << "请输入时间片长度:" << endl;
cin >> q;
int flag = 1; //标志队列中是否还有进程
int finish_pro = 0; //完成的进程数
cout << "进程名称\t"
<< "开始时间\t"
<< "运行时间\t"
<< "剩余服务时间\t"
<< "结束时间\t" << endl;
int time; //记录当前时刻时间
int c = 0;
while (finish_pro < num) {
flag = 0; //就绪队列里没进程
for (int i = c; i < num; i++) {
Arrive_sort(num);
job[i].Tsurplus = job[i].Tservice;
job[i].Tstart = job[i - 1].Taccomplish; //上一个作业结束时间
if (job[i].Tstart < job[i].Tarrive) //该作业的开始时间小于到达时间
{
job[i].Tstart = job[i].Tarrive;
} else {
job[i].Tstart = job[i - 1].Taccomplish;
}
time = job[i].Tstart;
if (job[i].if_finish == 1) {
continue; //该进程已完成
} else {
if (job[i].Tsurplus <= q &&
time >= job[i].Tarrive) //未完成且少于一个时间片
{
flag = 1;
time = time + job[i].Tsurplus;
job[i].if_finish = 1; //该进程完成
job[i].Taccomplish = time;
Safeorder.push_back(i);
cout << job[i].name << "\t\t"
<< job[i].Taccomplish - job[i].Tsurplus << "\t\t"
<< job[i].Tsurplus << "\t\t" << 0 << "\t\t"
<< job[i].Taccomplish << endl;
job[i].Tsurplus = 0;
} else if (job[i].Tsurplus > q && time >= job[i].Tarrive) {
flag = 1;
time = time + q;
job[i].Tsurplus -= q;
job[i].Taccomplish = time;
cout << job[i].name << "\t\t" << time - q << "\t\t" << q
<< "\t\t" << job[i].Tsurplus << "\t\t"
<< job[i].Taccomplish << endl;
job[num].name = job[i].name;
job[num].Tarrive = time;
job[num].Tservice = job[i].Tsurplus;
num++;
}
if (job[i].if_finish == 1) {
finish_pro++;
} //一个进程完成加一
}
c++;
}
break;
if (flag == 0 && finish_pro < num) //没执行完且没进入就绪队列
{
for (int i = 0; i < num; i++) {
if (job[i].if_finish == 0) {
time = job[i].Tarrive;
break;
}
}
}
}
}
//输出
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) { RR(num); }
int main() {
int num;
char jname;
int arrive;
int service;
int accomplish;
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);
// print(num);
// for (int k = 0; k < Safeorder.size(); k++) {
// cout << "P" << Safeorder.at(k) << "\t";
// }
return 0;
}