-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcfs.c
More file actions
509 lines (399 loc) · 13.4 KB
/
cfs.c
File metadata and controls
509 lines (399 loc) · 13.4 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
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
#include <ctype.h>
#include <semaphore.h>
#include "distribute.h"
#include "priority_queue.h"
#include "scheduling.h"
#include "pcb.h"
#define MIN_ARGS_C 15
#define MIN_ARGS_F 6
#define MIN_ARGS 2
#define STR_SIZE 20
#define SCHEDULER_WAITING 0
#define SCHEDULER_RUNNING 1
// Global (Shared) Data
struct priority_queue runqueue;
pthread_mutex_t lock1;
pthread_mutex_t lock2;
sem_t mutex;
pthread_cond_t scheduler_cond_var;
pthread_cond_t *cond_var_array;
int *states_array;
struct timeval start, arrival, finish, running;
struct Process_Control_Block *pcb_array;
int pcb_array_currentSize;
int scheduler_mode = SCHEDULER_WAITING;
int checkOut = 0;
FILE *fp2;
// Functions and definitions
void *generator(void *args);
void *scheduler(void *args);
void *process(void *args);
struct generator_params
{
char distPL[STR_SIZE], distIAT[STR_SIZE];
int avgPL, avgIAT;
int minPL, minIAT;
int maxPL, maxIAT;
int minPrio, maxPrio;
int allp;
int mode;
char infile[STR_SIZE];
int outmode;
};
struct process_params
{
int process_length;
int priority;
int pid;
int outmode;
};
struct scheduler_params
{
int outmode;
int allp;
};
int isAllpFinished(int size);
int main(int argc, char const *argv[])
{
if ( argc < MIN_ARGS )
{
fprintf(stderr, "Argument number is not sufficient");
exit(-1);
}
else
{
int minPrio, maxPrio;
int avgPL, minPL, maxPL;
char distPL[STR_SIZE], distIAT[STR_SIZE];
int avgIAT, minIAT, maxIAT;
int rqLen, allp, outmode;
char outfile[STR_SIZE];
char infile[STR_SIZE];
char prog_mode[2];
strcpy(prog_mode, argv[1]);
if ( strcmp(prog_mode, "C") == 0)
{
if ( argc < MIN_ARGS_C )
{
fprintf(stderr, "Argument number is not sufficient");
exit(-1);
}
// Take command line parameters
minPrio = atoi(argv[2]); maxPrio = atoi(argv[3]);
//distPL = argv[4];
strcpy(distPL, argv[4]); avgPL = atoi(argv[5]); minPL = atoi(argv[6]); maxPL = atoi(argv[7]);
strcpy(distIAT, argv[8]); avgIAT = atoi(argv[9]); minIAT = atoi(argv[10]); maxIAT = atoi(argv[11]);
rqLen = atoi(argv[12]); allp = atoi(argv[13]); outmode = atoi(argv[14]);
if ( argc == MIN_ARGS_C + 1 )
{
checkOut = 1;
strcpy(outfile,argv[15]);
fp2 = fopen(outfile, "w");
}
}
else if ( strcmp(prog_mode, "F") == 0 )
{
if ( argc < MIN_ARGS_F )
{
fprintf(stderr, "Argument number is not sufficient");
exit(-1);
}
// Take command line parameters
rqLen = atoi(argv[2]);
allp = atoi(argv[3]);
outmode = atoi(argv[4]);
strcpy(infile, argv[5]);
if ( argc == MIN_ARGS_F + 1 )
{
checkOut = 1;
strcpy(outfile, argv[6]);
fp2 = fopen(outfile, "w");
}
}
// Start Simulation
gettimeofday(&start, NULL);
init_queue(&runqueue, rqLen);
pthread_mutex_init(&lock1, NULL);
pthread_mutex_init(&lock2, NULL);
sem_init(&mutex, 0, 1);
pthread_cond_init(&scheduler_cond_var, NULL);
cond_var_array = malloc(sizeof(pthread_cond_t) * allp);
for (int i = 0; i < allp; i++)
{
pthread_cond_init(&(cond_var_array[i]), NULL);
}
states_array = malloc(sizeof(int) * allp);
for (int i = 0; i < allp; i++)
{
states_array[i] = WAITING;
}
pcb_array = malloc(sizeof(struct Process_Control_Block) * allp);
pcb_array_currentSize = 0;
pthread_t generator_tid, scheduler_tid;
struct generator_params params;
strcpy(params.distPL, distPL); strcpy(params.distIAT, distIAT);
params.avgPL = avgPL; params.avgIAT = avgIAT;
params.minPL = minPL; params.minIAT = minIAT;
params.maxPL = maxPL; params.maxIAT = maxIAT;
params.minPrio = minPrio; params.maxPrio = maxPrio;
params.allp = allp; params.outmode = outmode;
if (strcmp(prog_mode, "C") == 0)
params.mode = 0;
else
{
params.mode = 1;
strcpy(params.infile, infile);
}
struct scheduler_params sParams;
sParams.allp = allp;
sParams.outmode = outmode;
pthread_create(&generator_tid, NULL, generator, (void *) ¶ms);
pthread_create(&scheduler_tid, NULL, scheduler, (void*) &sParams);
pthread_join(generator_tid, NULL);
pthread_join(scheduler_tid, NULL);
if(checkOut == 1)
fprintf(fp2, "pid arv dept prio cpu waitr turna cs\n");
else
printf("pid arv dept prio cpu waitr turna cs\n");
int sum = 0;
for(int i = 0; i < pcb_array_currentSize; i++)
{
struct Process_Control_Block tmp = pcb_array[i];
int turna = tmp.finish_time - tmp.arrival_time;
int waitr = turna - tmp.pLength;
sum += waitr;
if(checkOut == 1)
fprintf(fp2, "%d %d %d %d %d %d %d %d\n", tmp.pid, tmp.arrival_time, tmp.finish_time, tmp.priority, tmp.pLength, waitr, turna, tmp.context_switch);
else
printf("%d %d %d %d %d %d %d %d\n", tmp.pid, tmp.arrival_time, tmp.finish_time, tmp.priority, tmp.pLength, waitr, turna, tmp.context_switch);
}
double avg_wait = (double) sum / pcb_array_currentSize;
if(checkOut == 1)
fprintf(fp2, "avg waiting time : %f\n", avg_wait);
else
printf("avg waiting time : %f\n", avg_wait);
}
return 0;
}
void *generator(void *args)
{
struct generator_params *params = (struct generator_params*) args;
int numOfProcesses = params->allp;
int mode = params->mode;
int outmode = params->outmode;
int process_length, interarrival_time, priority;
FILE *fp;
if (mode == 1)
fp = fopen(params->infile, "r");
pthread_t *thread_id_array = malloc(sizeof(pthread_t) * numOfProcesses);
for (int i = 0; i < numOfProcesses; i++)
{
if (mode == 0)
{
process_length = generate_process_length(params->distPL, params->avgPL, params->minPL, params->maxPL);
interarrival_time = generate_interarrival_time(params->distIAT, params->avgIAT, params->minIAT, params->maxIAT);
priority = generate_priority(params->minPrio, params->maxPrio);
}
else
{
int value = 0;
char buffer[10];
fscanf(fp, "%s", buffer); // PL
fscanf(fp, "%d", &value);
process_length = value;
fscanf(fp, "%d", &value);
priority = value;
fscanf(fp, "%s", buffer); // IAT
fscanf(fp, "%d", &value);
interarrival_time = value;
}
struct process_params p_params;
p_params.priority = priority;
p_params.process_length = process_length;
p_params.pid = i + 1;
p_params.outmode = outmode;
while ( isFull(&runqueue) )
usleep( interarrival_time * 1000 );
// Create thread
pthread_create(&thread_id_array[i], NULL, process, (void *) &p_params);
if (outmode == 3)
{
if(checkOut == 1)
fprintf(fp2, "New Process with pid %d created\n", p_params.pid);
else
printf("New Process with pid %d created\n", p_params.pid);
}
usleep(interarrival_time * 1000);
}
for (int i = 0; i < numOfProcesses; i++)
{
pthread_join(thread_id_array[i], NULL);
}
free(thread_id_array);
pthread_exit(0);
}
void *process(void *args)
{
struct process_params *params = (struct process_params*) args;
struct Process_Control_Block pcb;
pcb.pid = params->pid;
pcb.priority = params->priority;
pcb.pLength = params->process_length;
pcb.remaining_pLength = params->process_length;
pcb.context_switch = 0;
pcb.virtual_runtime = 0.0;
pcb.total_time_spent = 0; //?
int outmode = params->outmode;
pthread_mutex_lock(&lock1);
// Critical Section
insert_pcb(&runqueue, pcb);
pthread_mutex_unlock(&lock1);
if ( outmode == 3 )
{
if(checkOut == 1)
fprintf(fp2, "The process with pid %d added to the runqueue\n", pcb.pid);
else
printf("The process with pid %d added to the runqueue\n", pcb.pid);
}
gettimeofday(&arrival, NULL);
pcb.arrival_time = (arrival.tv_usec - start.tv_usec) / 1000;
while ( pcb.remaining_pLength > 0 )
{
pthread_mutex_lock(&lock2);
sem_wait(&mutex);
scheduler_mode = SCHEDULER_RUNNING;
pthread_cond_signal(&scheduler_cond_var);
while ( states_array[pcb.pid - 1] != RUNNING )
{
pthread_cond_wait(&(cond_var_array[pcb.pid - 1]), &lock2);
}
if(checkOut == 1)
fprintf(fp2, "pid %d remaining %d virtual runtime %f\n", pcb.pid, pcb.remaining_pLength, pcb.virtual_runtime);
else
printf("pid %d remaining %d virtual runtime %f\n", pcb.pid, pcb.remaining_pLength, pcb.virtual_runtime);
if (outmode == 2)
{
gettimeofday(&running, NULL);
int time = (running.tv_usec - start.tv_usec) / 1000;
}
else if (outmode == 3)
{
if(checkOut == 1)
fprintf(fp2, "Process with pid %d is running in CPU\n", pcb.pid);
else
printf("Process with pid %d is running in CPU\n", pcb.pid);
}
int timeslice = calculate_timeslice(&runqueue, pcb.priority);
int actualruntime;
if (pcb.remaining_pLength < timeslice)
{
actualruntime = pcb.remaining_pLength;
}
else
{
actualruntime = timeslice;
if (outmode == 3)
{
if(checkOut == 1)
fprintf(fp2, "Process with pid %d expired timeslice\n", pcb.pid);
else
printf("Process with pid %d expired timeslice\n", pcb.pid);
}
}
usleep(actualruntime * 1000);
pcb.virtual_runtime = update_virtual_runtime(pcb.virtual_runtime, pcb.priority, actualruntime);
pcb.remaining_pLength -= actualruntime;
if (pcb.remaining_pLength <= 0)
break;
pthread_mutex_lock(&lock1);
int id = pcb.pid;
for (int i = 0; i < runqueue.currentSize; i++)
{
if (runqueue.heap[i].pid == id)
{
runqueue.heap[i].virtual_runtime = update_virtual_runtime(pcb.virtual_runtime, pcb.priority, actualruntime);
runqueue.heap[i].remaining_pLength -= actualruntime;
}
}
heapRebuild(&runqueue, 0);
pthread_mutex_unlock(&lock1);
states_array[pcb.pid - 1] = WAITING;
pcb.context_switch++;
scheduler_mode = SCHEDULER_RUNNING;
pthread_cond_signal(&scheduler_cond_var);
sem_post(&mutex);
pthread_mutex_unlock(&lock2);
}
pthread_mutex_lock(&lock1);
delete_pcb(&runqueue);
pthread_mutex_unlock(&lock1);
states_array[pcb.pid - 1] = FINISHED;
if (outmode == 3)
{
if(checkOut == 1)
fprintf(fp2, "Process with pid %d finished\n", pcb.pid);
else
printf("Process with pid %d finished\n", pcb.pid);
}
gettimeofday(&finish, NULL);
pcb_array[pcb_array_currentSize] = pcb;
pcb_array_currentSize++;
pcb.finish_time = (finish.tv_usec - start.tv_usec) / 1000; // dept
scheduler_mode = SCHEDULER_RUNNING;
pthread_cond_signal(&scheduler_cond_var);
pthread_mutex_unlock(&lock2);
pthread_exit(0);
}
void *scheduler(void *args)
{
struct scheduler_params *sParams = (struct scheduler_params *) args;
int outmode = sParams->outmode;
int allp = sParams->allp;
while ( isAllpFinished(allp) == 0 )
{
pthread_mutex_lock(&lock2);
if (runqueue.currentSize == 0 )
scheduler_mode = SCHEDULER_WAITING;
if (isAllpFinished(allp) == 1)
break;
while (scheduler_mode == SCHEDULER_WAITING )
pthread_cond_wait(&scheduler_cond_var, &lock2);
pthread_mutex_lock(&lock1);
struct Process_Control_Block pcb = get_min_pcb(&runqueue);
pthread_mutex_unlock(&lock1);
states_array[pcb.pid - 1] = RUNNING;
pthread_cond_signal(&(cond_var_array[pcb.pid - 1]));
if (outmode == 3)
{
if(checkOut == 1)
fprintf(fp2, "Process with pid %d is selected for CPU\n", pcb.pid);
else
printf("Process with pid %d is selected for CPU\n", pcb.pid);
}
pthread_mutex_unlock(&lock2);
sem_post(&mutex);
}
if(checkOut == 1)
fprintf(fp2, "Scheduler exit\n");
else
printf("Scheduler exit\n");
pthread_exit(0);
}
int isAllpFinished(int size)
{
for (int i = 0; i < size; i++)
{
if ( states_array[i] != FINISHED )
{
return 0;
}
}
return 1;
}