-
Notifications
You must be signed in to change notification settings - Fork 0
/
RR.h
295 lines (240 loc) · 7.19 KB
/
RR.h
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#ifndef __RR__
#define __RR__
/*
헤더명 : RR.h(Round Robin Scheduling)
내용 : 한 프로세스가 할당받은 시간(타임 슬라이스Time Slice/Time Quantum)
동안작업을 하다가 작업을 완료하지 못하면 준비 큐의 맨 뒤로 가서 자기 차례를 기다리는 방식
입력 : 프로세스 포인터, 프로세스 갯수 , 타임 슬라이스(퀀텀)
출력 : RR 스케쥴링으로 인한
각 프로세스별 대기시간, 평균 대기 시간,
각 프로세스별 응답시간, 평균 응답시간,
각 프로세스별 반환 시간, 평균 반환 시간
*/
#include "Process.h"
#include <Windows.h>
#include "View_Table.h"
#include "Function_to_Sorting.h"
void RR_Process_Waiting_Time(Process* p, int n, Quantum quantum) { // 대기 시간 계산 함수
int current_time = 0; // 현재시간 초기화
int total_run_time = 0; // 총 실행시간 초기화
int* remain_run_time = NULL;
remain_run_time = (int*)malloc(sizeof(int) * n); // 남은 시간 저장 배열 할당
if (remain_run_time == NULL) {
fprintf(stderr, "메모리 할당 실패\n");
exit(1);
}
bool* check_response_time = NULL;
check_response_time = (bool*)malloc(sizeof(bool) * n); // 응답 시간 처리 확인 배열 할당
if (check_response_time == NULL) {
fprintf(stderr, "메모리 할당 실패\n");
exit(1);
}
for (int i = 0; i < n;i++) {
remain_run_time[i] = p[i].run_time; // 각 프로세스별 남은시간 저장
check_response_time[i] = false; // 프로세스 응답 초기화
total_run_time += p[i].run_time; // 총 실행시간 계산
}
while (true) { // 모든 프로세스가 완료될 때 까지 반복
bool check = true;
for (int i = 0; i < n; i++) {
if (remain_run_time[i] > 0) { // 실행 시간이 남아 있는 경우
check = false;
if (check_response_time[i] == false) { // 응답 시간 처리 안한 경우
p[i].response_time = current_time - p[i].arrival_time; // 응답시간 계산
check_response_time[i] = true;
}
if (remain_run_time[i] > quantum) { // 남은 시간이 타임 슬라이스 보다 클 경우
current_time += quantum; // 현재 시간 증가
remain_run_time[i] -= quantum; // 현재 실행중인 프로세스 남은 시간 감소
}
else { // 남은 시간이 타임 슬라이스 보다 작은 경우
current_time += remain_run_time[i]; // 현재시간 증가
p[i].waiting_time = current_time - p[i].run_time; // 대기시간 계산
remain_run_time[i] = 0; // 작업 끝난 프로세스 남은 시간 0으로 초기화
}
}
}
if (check == true) // 남아 있는 프로세스가 없는 경우
break;
}
/* 동적할당한 메모리 해제 */
free(remain_run_time);
free(check_response_time);
}
void RR_Process_TurnAround_Time(Process* p, int n) { // 소요 시간 계산 함수
for (int i = 0; i < n; i++) {
p[i].turnAround_time = p[i].run_time + p[i].waiting_time - p[i].arrival_time;
}
}
/* 대기시간 계산 함수 알고리즘과 동일 */
void RR_print_gantt_chart(Process* p, int n, Quantum quantum)
{
int current_time = 0, total_run_time = 0; // 현재 시간과 총 실행 시간을 저장할 변수
int* remain_run_time = NULL;
remain_run_time = (int*)malloc(sizeof(int) * n);
if (remain_run_time == NULL) {
fprintf(stderr, "메모리 할당 실패\n");
exit(1);
}
for (int i = 0; i < n; i++){
remain_run_time[i] = p[i].run_time;
total_run_time += p[i].run_time;
}
printf("┏");
/* 상단 바 출력 */
while (true){
bool check = true;
for (int i = 0; i < n; i++){
if (remain_run_time[i] > 0){
check = false;
if (remain_run_time[i] < quantum){
for (int j = 0; j < remain_run_time[i]; j++)
printf("");
}else{
for (int j = 0; j < quantum ; j++)
printf("━━");
}
if (remain_run_time[i] > quantum){
current_time += quantum;
remain_run_time[i] -= quantum;
printf("━━");
}else{
current_time += remain_run_time[i];
p[i].waiting_time = current_time - p[i].run_time;
remain_run_time[i] = 0;
}
}
}
if (check == true)
break;
}
printf("┓\n");
for (int i = 0; i < n; i++){
remain_run_time[i] = p[i].run_time;
}
/* 프로세스 아이디 출력 */
while (true){
int check = true;
for (int i = 0; i < n; i++){
if (remain_run_time[i] > 0){
check = false;
if (remain_run_time[i] < quantum){
printf(" | ");
if (remain_run_time[i] != 1){
printf(" %s ", p[i].id);
}
else {
printf(" %s ", p[i].id);
}
}else{
printf("┃");
printf(" %s ", p[i].id);
}
if (remain_run_time[i] > quantum){
current_time += quantum;
remain_run_time[i] -= quantum;
}else
{
current_time += remain_run_time[i];
p[i].waiting_time = current_time - p[i].run_time;
remain_run_time[i] = 0;
}
}
}
if (check == true)
break;
}
printf("┃\n");
for (int i = 0; i < n; i++){
remain_run_time[i] = p[i].run_time;
}
printf("┗");
/* 하단 바 출력 */
while (true){
bool check = true;
for (int i = 0; i < n; i++){
if (remain_run_time[i] > 0){
check = false;
if (remain_run_time[i] < quantum){
for (int j = 0; j < remain_run_time[i]; j++) {
printf("1");
}
}else{
for (int j = 0; j < quantum; j++) {
printf("━━");
}
}
if (remain_run_time[i] > quantum){
current_time += quantum;
remain_run_time[i] -= quantum;
printf("━━");
}else{
current_time += remain_run_time[i];
p[i].waiting_time = current_time - p[i].run_time;
remain_run_time[i] = 0;
}
}
}
if (check == true)
break;
}
printf("┛\n");
for (int i = 0; i < n; i++)
remain_run_time[i] = p[i].run_time;
current_time = 0;
/* 프로세스 시간 출력 */
while (true){
int check = true;
for (int i = 0; i < n; i++){
if (remain_run_time[i] > 0){
check = false;
if (remain_run_time[i] < quantum){
printf("%2d", current_time);
}else{
printf("%2d", current_time);
for (int j = 0; j < quantum / 2; j++) {
printf(" ");
}
}
if (remain_run_time[i] > quantum){
current_time += quantum;
remain_run_time[i] -= quantum;
}else{
current_time += remain_run_time[i];
p[i].waiting_time = current_time - p[i].run_time;
remain_run_time[i] = 0;
}
}
}
if (check == true)
break;
}
printf("%d", total_run_time);
printf("\n");
free(remain_run_time);
// 동적 할당한 배열의 메모리 할당 해제
}
void RR_Scheduling(Process* p, int pc, Quantum quantum) {
int total_waiting_time = 0; // 총 프로세스 대기 시간
int total_turnAround_time = 0; // 총 프로세스 소요 시간
int total_response_time = 0; // 총 프로세스 응답 시간
Process_init(p, pc);
qsort_arrival_time(p, pc);
RR_Process_Waiting_Time(p, pc, quantum);
RR_Process_TurnAround_Time(p, pc);
for (int i = 0; i < pc; i++){
p[i].waiting_time = p[i].turnAround_time - p[i].run_time; // 대기 시간 계산 후 저장
p[i].return_time = p[i].arrival_time + p[i].run_time + p[i].waiting_time; // 반환 시간 계산 후 저장
total_waiting_time += p[i].waiting_time; // 총 대기 시간 증가
total_turnAround_time += p[i].turnAround_time; // 총 소요 시간 증가
total_response_time += p[i].response_time; // 총 응답 시간 증가
}
printf("\tRound Robin Scheduling\n");
RR_print_gantt_chart(p, pc, quantum);
view_table(p, pc);
printf("\n");
printf("Average Total Waiting time = %.2lf\n", (double)total_waiting_time / (double)pc); // 평균 대기 시간
printf("Average Total turnAround time = %.2lf\n", (double)total_turnAround_time / (double)pc); // 평균 반환 시간
printf("Average Total Response time = %.2lf\n", (double)total_response_time / (double)pc); // 평균 응답 시간
}
#endif