-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathschedule.c
More file actions
285 lines (254 loc) · 9.42 KB
/
schedule.c
File metadata and controls
285 lines (254 loc) · 9.42 KB
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
#include "schedule.h"
#include "stats.h"
#include "process.h"
#include "memory.h"
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#define HIGH_PRIORITY_WEIGHT 3
#define MID_PRIORITY_WEIGHT 2
#define LOW_PRIORITY_WEIGHT 1
extern Process *processes[MAX_PROCESSES];
extern int process_count;
extern FrameTableEntry *frame_table;
extern int frame_count;
Process *high_priority_queue = NULL;
Process *mid_priority_queue = NULL;
Process *low_priority_queue = NULL;
// Define colors
#define RESET "\033[0m"
#define GREEN "\033[32m"
#define BRIGHT_BLUE "\033[94m"
#define BRIGHT_RED "\033[91m" // 인터럽트 발생한 프로세스를 표시할 색상
// Function to get terminal size
void get_terminal_size(int *rows, int *cols) {
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
*rows = w.ws_row;
*cols = w.ws_col;
}
// Clear the terminal screen
void clear_screen() {
printf("\033[2J");
printf("\033[H");
}
void move_cursor(int row, int col) {
printf("\033[%d;%dH", row, col);
}
void display_progress() {
int rows, cols;
get_terminal_size(&rows, &cols);
clear_screen();
for (int i = 0; i < process_count; i++) {
Process *process = processes[i];
move_cursor(i * 5 + 1, 1);
if (process->was_interrupted) {
printf("%sProcess %d%s\n", BRIGHT_RED, process->id, RESET);
} else {
printf("%sProcess %d%s\n", BRIGHT_BLUE, process->id, RESET);
}
Thread *thread = process->threads;
while (thread != NULL) {
printf(" Thread %d: %s", thread->tid, GREEN);
for (int j = 0; j < thread->burst_time; j++) {
if (j < thread->executed_time) {
printf("#");
} else {
printf("-");
}
}
if (thread->remaining_time == 0) {
printf("."); // 프로세스 완료 표시
}
printf("%s %d/%d\n", RESET, thread->executed_time, thread->burst_time);
thread = thread->next;
}
}
/*
// 물리 메모리 프레임을 표시
int start_col = cols - 20; // 오른쪽으로 25 컬럼만큼 이동
move_cursor(1, start_col);
printf("Physical Memory:\n");
for (int i = 0; i < frame_count; i++) {
if (frame_table[i].valid) {
printf("[P%d, Pg%d]", frame_table[i].process_id, frame_table[i].page_number);
} else {
printf("[Empty]");
}
if ((i + 1) % 5 == 0) {
printf("\n");
move_cursor((i + 1) / 5 + 2, start_col); // 다음 줄로 이동
}
}
fflush(stdout);*/
}
void display_running_thread(Process *process, Thread *thread) {
move_cursor(process_count * 5 + 1, 1);
printf("Running Process %d's Thread %d\n", process->id, thread->tid);
fflush(stdout);
}
void enqueue_process(Process **queue, Process *process) {
if (*queue == NULL) {
*queue = process;
} else {
Process *temp = *queue;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = process;
}
process->next = NULL;
}
Process *dequeue_process(Process **queue) {
if (*queue == NULL) {
return NULL;
} else {
Process *temp = *queue;
*queue = (*queue)->next;
temp->next = NULL;
return temp;
}
}
int get_queue_length(Process *queue) {
int length = 0;
while (queue != NULL) {
length++;
queue = queue->next;
}
return length;
}
Process *select_process() {
int high_priority_count = get_queue_length(high_priority_queue);
int mid_priority_count = get_queue_length(mid_priority_queue);
int low_priority_count = get_queue_length(low_priority_queue);
int total_weight = (high_priority_count * HIGH_PRIORITY_WEIGHT) +
(mid_priority_count * MID_PRIORITY_WEIGHT) +
(low_priority_count * LOW_PRIORITY_WEIGHT);
if (total_weight == 0) {
return NULL;
}
int random_value = rand() % total_weight;
if (random_value < (high_priority_count * HIGH_PRIORITY_WEIGHT)) {
return dequeue_process(&high_priority_queue);
} else if (random_value < ((high_priority_count * HIGH_PRIORITY_WEIGHT) + (mid_priority_count * MID_PRIORITY_WEIGHT))) {
return dequeue_process(&mid_priority_queue);
} else {
return dequeue_process(&low_priority_queue);
}
}
void run_thread(Thread *thread, Process *process) {
if (thread->remaining_time > 0) {
int run_time = thread->remaining_time < TIME_QUANTUM ? thread->remaining_time : TIME_QUANTUM;
for (int i = 0; i < run_time; i++) {
sleep(1);
thread->executed_time++;
thread->remaining_time--;
process->remaining_time--;
add_total_cpu_time(1);
increment_total_simulation_time();
display_progress(); // Update display
}
if (thread->remaining_time == 0) {
printf("Process %d's Thread %d completed.\n", process->id, thread->tid);
if (process->remaining_time == 0) {
process->state = COMPLETED;
record_process_completion_time(process->id, total_simulation_time);
}
}
}
}
void handle_io_interrupt(Process *process) {
if (rand() % 100 < 30) { // 30% 확률로 I/O 인터럽트 발생
process->state = WAITING;
process->io_remaining_time = (rand() % 10) + 7; // I/O 대기 시간 설정
process->was_interrupted = 1; // 인터럽트 발생 여부를 저장
printf("Process %d I/O interrupt. Moving to I/O wait.\n", process->id);
display_progress(); // Display progress after I/O interrupt
}
}
void update_io_waiting_processes() {
for (int i = 0; i < process_count; i++) {
Process *process = processes[i];
if (process->state == WAITING && process->io_remaining_time > 0) {
process->io_remaining_time--; // 1초씩 감소시킴
if (process->io_remaining_time == 0) {
process->state = READY;
process->was_interrupted = 0; // 인터럽트 플래그 초기화
printf("Process %d I/O wait completed. Returning to ready state.\n", process->id);
display_progress(); // Display progress after I/O wait completion
// 프로세스를 다시 큐에 추가
if (process->priority == 0) {
enqueue_process(&high_priority_queue, process);
} else if (process->priority == 1) {
enqueue_process(&mid_priority_queue, process);
} else {
enqueue_process(&low_priority_queue, process);
}
}
}
}
}
void multi_level_queue_scheduling() {
// 프로세스를 각 우선순위 큐에 배치
for (int i = 0; i < process_count; i++) {
Process *process = processes[i];
if (process->state == READY) {
if (process->priority == 0) {
enqueue_process(&high_priority_queue, process);
} else if (process->priority == 1) {
enqueue_process(&mid_priority_queue, process);
} else {
enqueue_process(&low_priority_queue, process);
}
}
}
Process *current_process;
while (1) {
current_process = select_process();
while (current_process != NULL) {
Thread *current_thread = current_process->threads;
while (current_thread != NULL) {
int page_number = current_thread->executed_time / TIME_QUANTUM;
int frame_number = request_page(current_process, page_number); // 페이지 요청
display_running_thread(current_process, current_thread);
run_thread(current_thread, current_process);
handle_io_interrupt(current_process); // I/O 인터럽트 처리
if (current_thread->remaining_time == 0 || current_process->state == WAITING) {
break;
}
current_thread = current_thread->next;
}
update_io_waiting_processes(); // I/O 대기 중인 프로세스 상태 업데이트
// 스케줄링 반복을 위해 큐 재배치 로직 이동
if (current_process->state == READY) {
if (current_process->priority == 0) {
enqueue_process(&high_priority_queue, current_process);
} else if (current_process->priority == 1) {
enqueue_process(&mid_priority_queue, current_process);
} else {
enqueue_process(&low_priority_queue, current_process);
}
}
current_process = select_process();
}
// 모든 프로세스가 완료되었는지 확인
int all_completed = 1;
for (int i = 0; i < process_count; i++) {
if (processes[i]->state != COMPLETED) {
all_completed = 0;
break;
}
}
// 모든 프로세스가 완료되었으면 스케줄링 종료
if (all_completed) {
break;
}
// 아직 완료되지 않은 프로세스가 있는 경우 대기
sleep(1);
update_io_waiting_processes(); // I/O 대기 중인 프로세스 상태 업데이트
}
// Free processes after scheduling
free_processes();
}