-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
484 lines (402 loc) · 15.6 KB
/
main.c
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define SILVER_QUANTUM 80
#define GOLD_QUANTUM 120
int instruction_times[21];
enum Type {
PLATINUM,
GOLD,
SILVER
};
struct Instr {
char strValue[100]; // Assuming a maximum length of 100 for the string.
int duration;
};
struct Proc {
int index;
struct Instr process_instructions[100];
int arrival_time;
int burst_time;
int last_instruction;
char name[20];
int real_arrival_time;
int priority;
enum Type type;
int isFinished;
int quantum_number;
int CPU_time;
bool is_last;
};
struct Proc procs[10];
enum Type stringToType(const char *str) {
if (strcmp(str, "GOLD") == 0) {
return GOLD;
} else if (strcmp(str, "SILVER") == 0) {
return SILVER;
} else if (strcmp(str, "PLATINUM") == 0) {
return PLATINUM;
}
// Handle unknown type or error case
return -1; // You can define a default value or handle errors differently
}
const char* typeToString(enum Type type) {
switch (type) {
case GOLD:
return "GOLD";
case SILVER:
return "SILVER";
case PLATINUM:
return "PLATINUM";
default:
return "UNKNOWN"; // or handle error as appropriate
}
}
void printProcs(struct Proc procs[], int size) {
//printf("Process Name\tPriority\tArrival Time\tType\tIsFinished\n");
for (int i = 0; i < size; i++) {
// printf("%s\t\t%d\t\t%d\t\t%s\t%d\t\n", procs[i].name, procs[i].priority, procs[i].arrival_time, typeToString(procs[i].type),procs[i].isFinished);
}
}
void printProcs2(struct Proc *procs[], int size) {
printf("Process Name\tPriority\tArrival Time\tType\n");
for (int i = 0; i < size; i++) {
//printf("%s\t\t%d\t\t%d\t\t%s\n", procs[i]->name, procs[i]->priority, procs[i]->arrival_time, typeToString(procs[i]->type));
}
}
int compareProcs(const void *a, const void *b) {
struct Proc *procA = (struct Proc *)a;
struct Proc *procB = (struct Proc *)b;
if(procA->isFinished != procB->isFinished){
return procA->isFinished - procB->isFinished;
}
if(procA->type==SILVER && procB->type==PLATINUM){
return 1;
}
if(procA->type==PLATINUM && procB->type==SILVER){
return -1;
}
if(procA->type==PLATINUM && procB->type==GOLD){
return -1;
}
if(procA->type==GOLD && procB->type==PLATINUM){
return 1;
}
if (procA->priority != procB->priority) {
return procB->priority - procA->priority;
}
// Compare arrival time
if (procA->arrival_time != procB->arrival_time) {
return procA->arrival_time - procB->arrival_time;
}
// If arrival times are the same, compare priorities
// If both arrival times and priorities are the same, compare names
return strcmp(procA->name, procB->name);
}
int compareProcsForTime(const void *a, const void *b) {
struct Proc *procA = (struct Proc *)a;
struct Proc *procB = (struct Proc *)b;
if(procA->isFinished != procB->isFinished){
return procA->isFinished - procB->isFinished;
}
if (procA->arrival_time != procB->arrival_time) {
return procA->arrival_time - procB->arrival_time;
}
if(procA->type==SILVER && procB->type==PLATINUM){
return 1;
}
if(procA->type==PLATINUM && procB->type==SILVER){
return -1;
}
if(procA->type==PLATINUM && procB->type==GOLD){
return -1;
}
if(procA->type==GOLD && procB->type==PLATINUM){
return 1;
}
if (procA->priority != procB->priority) {
return procB->priority - procA->priority;
}
// Compare arrival time
// If arrival times are the same, compare priorities
// If both arrival times and priorities are the same, compare names
return strcmp(procA->name, procB->name);
}
void readInstructionsFromFile(const char *fileName, struct Instr instructions[], int *instructionCount) {
FILE *file = fopen(fileName, "r");
if (!file) {
perror("Unable to open file");
return;
}
int count = 0;
while (count < 100 && fscanf(file, "%49s", instructions[count].strValue) == 1) {
char copied[25];
char* token;
strcpy(copied,instructions[count].strValue);
token = strtok(copied,"r");
token = strtok(NULL,"\0");
//printf("Token is : %s\n",token);
if(token !=NULL){
int index = atoi(token);
int time = instruction_times[index-1];
instructions[count].duration=time;
}
else{
instructions[count].duration=10;
}
count++;
}
//*instructionCount = count;
fclose(file);
}
void populateProcessInstructions(struct Proc *proc) {
char fileName[50]; // Assuming the file name won't exceed 50 characters
snprintf(fileName, sizeof(fileName), "%s.txt", proc->name);
readInstructionsFromFile(fileName, proc->process_instructions, &proc->last_instruction);
}
int init = 0;
int main() {
int burst_time_total=0;
FILE *file = fopen("instructions.txt", "r"); // Replace "file.txt" with your file name
if (file == NULL) {
printf("Error opening the file.\n");
return 1; // Return with an error code
}
char line[100];
while (fgets(line, sizeof(line), file)) {
char instr[20];
int time;
if (strncmp(line, "exit", 4) == 0) {
instruction_times[20]= 10;
continue; // Skip this iteration of the loop
}
if (sscanf(line, "%s %d", instr, &time) == 2) {
// Assuming instruction names are like "instrX"
int index;
sscanf(instr, "instr%d", &index);
instruction_times[index - 1] = time;
}
}
fclose(file);
// Printing the filled array for verification
/*for (int i = 1; i <= 21; i++) {
printf("instr%d %d\n", i, instruction_times[i-1]);
}*/
FILE *procFiles = fopen("definition.txt", "r");
if (!procFiles) {
perror("Unable to open file");
return 1;
}
int count = 0;
char typeStr[20]; // Temporary buffer to read the type as a string
while (fscanf(procFiles, "%s %d %d %s", procs[count].name, &procs[count].priority, &procs[count].arrival_time, typeStr) == 4) {
procs[count].type = stringToType(typeStr); // Convert string to enum
procs[count].index = count;
int real = procs[count].arrival_time;
procs[count].real_arrival_time=real;
count++;
if (count > 10) {
//("Too many processes in the file. Only the first 10 will be processed.\n");
break;
}
}
fclose(procFiles);
// printProcs(procs, count);
for (int i = 0; i < count; i++) {
populateProcessInstructions(&procs[i]);
}
qsort(procs, count, sizeof(struct Proc), compareProcs);
//TODO : current time 0 dan basla. Koşullara göre instructionları uygula
// her instructionda sortla ve tekrar isleme al (Arrayin headi !!!).
// Type dönüstürme kosulunu yaz. Global bi variableda süreleri tut. Last inst indexi tut.
// printProcs(procs, count);
int curr_time = 0;
int turnaround=0;
while(1) {
bool allFinished = true;
int test = 0;
int temp_count = 0;
init++;
struct Proc *procs_temp[20];
struct Proc *proc_last;
if(init !=0) {
for(int i = 0 ; i<count ; i++){
if(procs[i].is_last){
proc_last = &procs[i];
//printf("proc_last is %s \n",proc_last->name);
}
}
}
qsort(procs, count, sizeof(struct Proc), compareProcs);
// printProcs(procs,count);
for (int i = 0; i < count; i++) {
if (procs[i].arrival_time <= curr_time) {
temp_count++;
procs_temp[0] = &procs[i];
break;
}
}
if(proc_last!=NULL&&strcmp(proc_last->name,procs_temp[0]->name)!=0 && init>1){
printProcs(procs,count);
//printf("Preemption from2 %s to %s \n",proc_last->name,procs_temp[0]->name);
if(proc_last->type==SILVER && proc_last->quantum_number<3&&proc_last->CPU_time!=0 ){
proc_last->quantum_number++;
if(proc_last->CPU_time>=SILVER_QUANTUM){
proc_last->arrival_time=curr_time;
}
if(proc_last->quantum_number==3){
proc_last->quantum_number=0;
proc_last->type=GOLD;
proc_last->CPU_time=0;
}
proc_last->arrival_time=curr_time;
}
if(proc_last->type==GOLD && proc_last->quantum_number<5 &&proc_last->CPU_time!=0){
proc_last->quantum_number++;
if(proc_last->CPU_time>=GOLD_QUANTUM){
proc_last->arrival_time=curr_time;
}
if(proc_last->quantum_number==5){
proc_last->quantum_number=0;
proc_last->type=PLATINUM;
proc_last->CPU_time=0;
}
proc_last->arrival_time=curr_time;
}
}
for(int i = 0 ; i<count;i++){
if(!procs[i].isFinished){
allFinished=false;
}
}
//printf("temp_count is %d \n",temp_count);
//printf("count is %d \n",count);
if(procs_temp[0]->isFinished && !allFinished ){
qsort(procs, count, sizeof(struct Proc), compareProcsForTime);
printf("arr %d \n",procs[0].arrival_time);
curr_time=procs[0].real_arrival_time;
continue;
}
if(!procs_temp[0]->is_last && !allFinished){
//printf("Context switch here \n");
curr_time+=10;
}
//printProcs2(procs_temp, temp_count);
if(procs_temp[0]->isFinished){
for(int k = 0 ; k<count;k++){
burst_time_total+=procs[k].burst_time;
}
if ((float) (turnaround - burst_time_total) / count != (int) ((turnaround - burst_time_total) / count)) {
printf("%.1f\n", (float) (turnaround - burst_time_total) / count);
} else {
printf("%d\n", (int) ((turnaround - burst_time_total) / count));
}
if ((float) turnaround / count != (int) (turnaround / count)) {
printf("%.1f\n", (float) turnaround / count);
} else {
printf("%d\n", (int) (turnaround / count));
}
// printf("Current time is : %d",curr_time);
break;
}
//curr_time+=10; // Context switch.
if(procs_temp[0]->type==PLATINUM){
int time_elapsed=0;
int counter_for_last_instr=0;
for(int i = procs_temp[0]->last_instruction ; i<sizeof(procs_temp[0]->process_instructions)/sizeof(procs_temp[0]->process_instructions[0]);i++){
time_elapsed+=procs_temp[0]->process_instructions[i].duration;
procs_temp[0]->burst_time+=procs_temp[0]->process_instructions[i].duration;
counter_for_last_instr++;
}
for(int i = 0 ; i<count ; i++){
procs[i].is_last=false;
}
procs_temp[0]->is_last=true;
procs_temp[0]->last_instruction=counter_for_last_instr;
procs_temp[0]->isFinished=1;
curr_time+=time_elapsed;
turnaround+=curr_time-procs_temp[0]->real_arrival_time;
//("IN PLATINUM BLOCK and P--name is %s and Current time is : %d\n",procs_temp[0]->name,curr_time);
continue;
}
else if(procs_temp[0]->type==SILVER){
if(!procs_temp[0]->is_last){
procs_temp[0]->CPU_time=0;
}
if(procs_temp[0]->CPU_time<SILVER_QUANTUM) {
int index = procs_temp[0]->last_instruction;
for(int i = 0 ; i<count ; i++){
procs[i].is_last=false;
}
procs_temp[0]->is_last=true;
if (strncmp(procs_temp[0]->process_instructions[index].strValue, "exit", 4) == 0) {
// printf("EXIT HIT IN SILVER BLOCK !!! \n");
curr_time += procs_temp[0]->process_instructions[index].duration;
turnaround += curr_time-procs_temp[0]->real_arrival_time;
procs_temp[0]->last_instruction++;
procs_temp[0]->burst_time+=procs_temp[0]->process_instructions[index].duration;
procs_temp[0]->isFinished = 1;
} else {
curr_time += procs_temp[0]->process_instructions[index].duration;
procs_temp[0]->last_instruction++;
procs_temp[0]->burst_time+=procs_temp[0]->process_instructions[index].duration;
}
procs_temp[0]->CPU_time+=procs_temp[0]->process_instructions[index].duration;
//printf("IN SILVER BLOCK and P--name is %s and Current time is : %d AND INST : %s \n", procs_temp[0]->name, curr_time,procs_temp[0]->process_instructions[index].strValue);
continue;
}
else{
procs_temp[0]->quantum_number++;
if(procs_temp[0]->quantum_number==3){
procs_temp[0]->quantum_number=0;
procs_temp[0]->type=GOLD;
}
procs_temp[0]->CPU_time=0;
procs_temp[0]->arrival_time=curr_time;
continue;
}
}
// GOLD BLOCK
else{
if(!procs_temp[0]->is_last){
procs_temp[0]->CPU_time=0;
}
//Üst taraf yeni eklendi. 11.49
if(procs_temp[0]->CPU_time<GOLD_QUANTUM) {
int index = procs_temp[0]->last_instruction;
for(int i = 0 ; i<count ; i++){
procs[i].is_last=false;
}
procs_temp[0]->is_last=true;
if (strncmp(procs_temp[0]->process_instructions[index].strValue, "exit", 4) == 0) {
// printf("EXIT HIT IN GOLD BLOCK !!! \n");
curr_time += procs_temp[0]->process_instructions[index].duration;
turnaround+=curr_time-procs_temp[0]->real_arrival_time;
procs_temp[0]->last_instruction++;
procs_temp[0]->burst_time+=procs_temp[0]->process_instructions[index].duration;
procs_temp[0]->isFinished = 1;
} else {
curr_time += procs_temp[0]->process_instructions[index].duration;
procs_temp[0]->burst_time+=procs_temp[0]->process_instructions[index].duration;
procs_temp[0]->last_instruction++;
}
procs_temp[0]->CPU_time+=procs_temp[0]->process_instructions[index].duration;
//printf("IN GOLD BLOCK and P--name is %s and Current time is : %d AND INST : %s \n", procs_temp[0]->name, curr_time,procs_temp[0]->process_instructions[index].strValue);
continue;
}
else{
procs_temp[0]->quantum_number++;
if(procs_temp[0]->quantum_number==5){
procs_temp[0]->quantum_number=0;
procs_temp[0]->type=PLATINUM;
}
procs_temp[0]->CPU_time=0;
//printf("here is %s \n",procs_temp[0]->name);
procs_temp[0]->arrival_time=curr_time;
continue;
}
}
}
return 0;
}