-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.c
More file actions
301 lines (253 loc) · 6.09 KB
/
program.c
File metadata and controls
301 lines (253 loc) · 6.09 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// MFQ Scheduling code - gprof로 최적화
#include <stdio.h>
#define TIME_QUANTUM0 2
#define TIME_QUANTUM1 4
#define MAX_LENGTH 1000
typedef struct Process
{
int pid;
int arrival_time;
int burst_time;
int remain_time;
int finish_time;
int current_q;
} Process;
typedef struct Queue
{
int front;
int rear;
int size;
Process *process_info[MAX_LENGTH];
} Queue;
void init_queue(Queue *q)
{
q -> front = q -> rear = q -> size = 0;
}
void enqueue(Queue *q, Process *p)
{
q->process_info[q->rear] = p;
q->rear++;
q->size++;
}
Process *dequeue(Queue *q)
{
Process *p = q->process_info[q->front];
q->front++;
q->size--;
return p;
}
int isEmpty(Queue *q)
{
if (q->front == q->rear)
return 1;
else
return 0;
}
Process *spn(Queue *q)
{
if (q->size == 0)
return NULL;
int min = q->front;
for (int i = q->front + 1; i < q->rear; i++)
{
if (q->process_info[i]->remain_time < q->process_info[min]->remain_time)
{
min = i;
}
}
Process *shortest = q->process_info[min];
for (int i = min; i < q->rear - 1; i++)
{
q->process_info[i] = q->process_info[i + 1];
}
q->rear--;
q->size--;
return shortest;
}
int read_file(const char *file, Process (*process)[MAX_LENGTH], int *num_process_ptr)
{
FILE *fp = fopen(file, "r");
if (fp == NULL)
{
printf("Error: Cannot open file");
return 0;
}
int num_process = 0;
if (fscanf(fp, "Number of Processes: %d\n", &num_process) != 1) {
fprintf(stderr, "Error: Invalid input format.\n");
fclose(fp);
return 0;
}
if (num_process > MAX_LENGTH) {
fprintf(stderr, "Error: Too many processes. Max is %d\n", MAX_LENGTH);
fclose(fp);
return 0;
}
for (int i = 0; i < num_process; i++) {
if (fscanf(fp, "%d, %d, %d\n", &(*process)[i].pid, &(*process)[i].arrival_time, &(*process)[i].burst_time) != 3) {
fprintf(stderr, "Error: Invalid process data on line %d\n", i + 2);
fclose(fp);
return 0;
}
(*process)[i].remain_time = (*process)[i].burst_time;
}
fclose(fp);
*num_process_ptr = num_process;
return 1;
}
int arrival(Process *process, int time)
{
return process->arrival_time == time;
}
void enqueue_arrival(Process *process, int num, int time, Queue *q0)
{
for (int i = 0; i < num; i++)
{
if (arrival(&process[i], time))
{
enqueue(q0, &process[i]);
}
}
}
Process* next_p(Queue *queue, int level)
{
Process *p = dequeue(queue);
if (p)
{
p -> current_q = level;
}
return p;
}
Process* scheduling(Queue *q0, Queue *q1, Queue *q2)
{
if (!isEmpty(q0))
{
return next_p(q0, 0);
}
if (!isEmpty(q1))
{
return next_p(q1, 1);
}
if (!isEmpty(q2))
{
return next_p(q2, 2);
}
return NULL;
}
void update(Process *process, int *time_quantum, int time, int *chart)
{
chart[time] = process->pid;
process->remain_time--;
(*time_quantum)++;
}
int finish(Process **process, int *time_quantum, int time)
{
if ((*process)->remain_time == 0)
{
(*process)->finish_time = time + 1;
*process = NULL;
*time_quantum = 0;
return 1;
}
return 0;
}
int expiration(Process **process, int *time_quantum, Queue *q1, Queue *q2)
{
if ((*process)->current_q == 0 && *time_quantum == TIME_QUANTUM0)
{
enqueue(q1, *process);
*process = NULL;
*time_quantum = 0;
return 1;
}
else if ((*process)->current_q == 1 && *time_quantum == TIME_QUANTUM1)
{
enqueue(q2, *process);
*process = NULL;
*time_quantum = 0;
return 1;
}
return 0;
}
void execution(Process **process, int *time_quantum, int time, int *chart, Queue *q1, Queue *q2)
{
update(*process, time_quantum, time, chart);
if (finish(process, time_quantum, time)) return;
if (expiration(process, time_quantum, q1, q2)) return;
}
void gantt_char(int *gantt_chart, int clock_time)
{
printf("Gantt Chart\n");
int prev = -1;
for (int i = 0; i < clock_time; i++)
{
if (gantt_chart[i] != prev)
{
printf("|P%-2d", gantt_chart[i]);
prev = gantt_chart[i];
}
}
printf("|\n");
prev = -1;
for (int i = 0; i < clock_time; i++)
{
if (gantt_chart[i] != prev)
{
printf("%-4d", i);
prev = gantt_chart[i];
}
}
printf("%d\n", clock_time);
}
void print_chart(Process *process, int num_process)
{
int turnaround_time = 0;
int waiting_time = 0;
double avg_tt = 0;
double avg_wt = 0;
printf("\nPID TT WT\n");
printf("==================\n");
for (int i = 0; i < num_process; i++)
{
turnaround_time = process[i].finish_time - process[i].arrival_time;
waiting_time = turnaround_time - process[i].burst_time;
avg_tt += turnaround_time;
avg_wt += waiting_time;
printf("P%d %5d %6d\n", process[i].pid, turnaround_time, waiting_time);
}
printf("\nAvergae TT: %.1f\n", avg_tt / num_process);
printf("Average WT: %.1f", avg_wt / num_process);
}
int main()
{
int num_process = 0;
Process process[MAX_LENGTH];
if (!read_file("input.txt", &process, &num_process))
{
return 1;
}
Queue q0, q1, q2;
init_queue(&q0);
init_queue(&q1);
init_queue(&q2);
int gantt_chart[MAX_LENGTH] = {0};
int clock_time = 0;
Process *running_p = NULL;
int time_quantum = 0;
while (1)
{
enqueue_arrival(process, num_process, clock_time, &q0);
if (running_p != NULL)
{
execution(&running_p, &time_quantum, clock_time, gantt_chart, &q1, &q2);
}
if (running_p == NULL)
{
running_p = scheduling(&q0, &q1, &q2);
if (running_p == NULL) break;
}
clock_time++;
}
gantt_char(gantt_chart, clock_time);
print_chart(process, num_process);
}