Skip to content

Commit 5bb7e99

Browse files
committed
update
1 parent fb014bd commit 5bb7e99

File tree

2 files changed

+62
-58
lines changed

2 files changed

+62
-58
lines changed

README.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Name ID
2+
------------------------
3+
Efe Erkan 21902248
4+
Recep Uysal 21803637

cfs.c

Lines changed: 58 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <stdlib.h>
66
#include <ctype.h>
77
#include <time.h>
8+
#include <ctype.h>
89
#include <semaphore.h>
910

1011
#include "distribute.h"
@@ -43,6 +44,10 @@ int pcb_array_currentSize;
4344

4445
int scheduler_mode = SCHEDULER_WAITING;
4546

47+
int checkOut = 0;
48+
49+
FILE *fp2;
50+
4651
// Functions and definitions
4752

4853
void *generator(void *args);
@@ -77,10 +82,6 @@ struct scheduler_params
7782

7883
int isAllpFinished(int size);
7984

80-
void printArray(int *arr, int size);
81-
82-
void broadcast(pthread_cond_t *arr, int size);
83-
8485
int main(int argc, char const *argv[])
8586
{
8687
if ( argc < MIN_ARGS )
@@ -116,8 +117,10 @@ int main(int argc, char const *argv[])
116117
rqLen = atoi(argv[12]); allp = atoi(argv[13]); outmode = atoi(argv[14]);
117118

118119
if ( argc == MIN_ARGS_C + 1 )
119-
{
120+
{
121+
checkOut = 1;
120122
strcpy(outfile,argv[15]);
123+
fp2 = fopen(outfile, "w");
121124
}
122125

123126
}
@@ -136,7 +139,9 @@ int main(int argc, char const *argv[])
136139

137140
if ( argc == MIN_ARGS_F + 1 )
138141
{
142+
checkOut = 1;
139143
strcpy(outfile, argv[6]);
144+
fp2 = fopen(outfile, "w");
140145
}
141146
}
142147

@@ -192,20 +197,30 @@ int main(int argc, char const *argv[])
192197
pthread_join(generator_tid, NULL);
193198
pthread_join(scheduler_tid, NULL);
194199

195-
printf("pid arv dept prio cpu waitr turna cs\n");
196-
200+
if(checkOut == 1)
201+
fprintf(fp2, "pid arv dept prio cpu waitr turna cs\n");
202+
else
203+
printf("pid arv dept prio cpu waitr turna cs\n");
204+
197205
int sum = 0;
198206
for(int i = 0; i < pcb_array_currentSize; i++)
199207
{
200208
struct Process_Control_Block tmp = pcb_array[i];
201209
int turna = tmp.finish_time - tmp.arrival_time;
202210
int waitr = turna - tmp.pLength;
203211
sum += waitr;
204-
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);
212+
if(checkOut == 1)
213+
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);
214+
else
215+
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);
205216
}
206217

207218
double avg_wait = (double) sum / pcb_array_currentSize;
208-
printf("avg waiting time : %f\n", avg_wait);
219+
220+
if(checkOut == 1)
221+
fprintf(fp2, "avg waiting time : %f\n", avg_wait);
222+
else
223+
printf("avg waiting time : %f\n", avg_wait);
209224
}
210225

211226
return 0;
@@ -257,14 +272,16 @@ void *generator(void *args)
257272

258273
while ( isFull(&runqueue) )
259274
usleep( interarrival_time * 1000 );
260-
261275

262276
// Create thread
263277
pthread_create(&thread_id_array[i], NULL, process, (void *) &p_params);
264278

265279
if (outmode == 3)
266-
{
267-
printf("New Process with pid %d created\n", p_params.pid);
280+
{
281+
if(checkOut == 1)
282+
fprintf(fp2, "New Process with pid %d created\n", p_params.pid);
283+
else
284+
printf("New Process with pid %d created\n", p_params.pid);
268285
}
269286

270287
usleep(interarrival_time * 1000);
@@ -304,11 +321,12 @@ void *process(void *args)
304321

305322
pthread_mutex_unlock(&lock1);
306323

307-
printQueue(&runqueue);
308-
309324
if ( outmode == 3 )
310325
{
311-
printf("The process with pid %d added to the runqueue\n", pcb.pid);
326+
if(checkOut == 1)
327+
fprintf(fp2, "The process with pid %d added to the runqueue\n", pcb.pid);
328+
else
329+
printf("The process with pid %d added to the runqueue\n", pcb.pid);
312330
}
313331

314332
gettimeofday(&arrival, NULL);
@@ -328,17 +346,22 @@ void *process(void *args)
328346
pthread_cond_wait(&(cond_var_array[pcb.pid - 1]), &lock2);
329347
}
330348

331-
printf("pid %d remaining %d virtual runtime %f\n", pcb.pid, pcb.remaining_pLength, pcb.virtual_runtime);
349+
if(checkOut == 1)
350+
fprintf(fp2, "pid %d remaining %d virtual runtime %f\n", pcb.pid, pcb.remaining_pLength, pcb.virtual_runtime);
351+
else
352+
printf("pid %d remaining %d virtual runtime %f\n", pcb.pid, pcb.remaining_pLength, pcb.virtual_runtime);
332353

333354
if (outmode == 2)
334355
{
335356
gettimeofday(&running, NULL);
336357
int time = (running.tv_usec - start.tv_usec) / 1000;
337-
printf("%d %d RUNNING\n", time, pcb.pid);
338358
}
339359
else if (outmode == 3)
340360
{
341-
printf("Process with pid %d is running in CPU\n", pcb.pid);
361+
if(checkOut == 1)
362+
fprintf(fp2, "Process with pid %d is running in CPU\n", pcb.pid);
363+
else
364+
printf("Process with pid %d is running in CPU\n", pcb.pid);
342365
}
343366

344367
int timeslice = calculate_timeslice(&runqueue, pcb.priority);
@@ -354,7 +377,10 @@ void *process(void *args)
354377
actualruntime = timeslice;
355378
if (outmode == 3)
356379
{
357-
printf("Process with pid %d expired timeslice\n", pcb.pid);
380+
if(checkOut == 1)
381+
fprintf(fp2, "Process with pid %d expired timeslice\n", pcb.pid);
382+
else
383+
printf("Process with pid %d expired timeslice\n", pcb.pid);
358384
}
359385
}
360386

@@ -378,13 +404,7 @@ void *process(void *args)
378404
}
379405
}
380406

381-
//delete_pcb(&runqueue);
382-
383407
heapRebuild(&runqueue, 0);
384-
385-
//insert_pcb(&runqueue, pcb);
386-
387-
printQueue(&runqueue);
388408

389409
pthread_mutex_unlock(&lock1);
390410

@@ -399,21 +419,19 @@ void *process(void *args)
399419
pthread_mutex_unlock(&lock2);
400420
}
401421

402-
printf("FINISH\n");
403-
404422
pthread_mutex_lock(&lock1);
405423

406424
delete_pcb(&runqueue);
407425

408426
pthread_mutex_unlock(&lock1);
409427

410-
// printQueue(&runqueue);
411-
412428
states_array[pcb.pid - 1] = FINISHED;
413429
if (outmode == 3)
414430
{
415-
printf("Process with pid %d finished\n", pcb.pid);
416-
printQueue(&runqueue);
431+
if(checkOut == 1)
432+
fprintf(fp2, "Process with pid %d finished\n", pcb.pid);
433+
else
434+
printf("Process with pid %d finished\n", pcb.pid);
417435
}
418436
gettimeofday(&finish, NULL);
419437
pcb_array[pcb_array_currentSize] = pcb;
@@ -434,11 +452,9 @@ void *scheduler(void *args)
434452
struct scheduler_params *sParams = (struct scheduler_params *) args;
435453
int outmode = sParams->outmode;
436454
int allp = sParams->allp;
437-
//printf("%d\n", isAllpFinished(allp));
455+
438456
while ( isAllpFinished(allp) == 0 )
439457
{
440-
//printf("%d\n", isAllpFinished(allp));
441-
442458
pthread_mutex_lock(&lock2);
443459

444460
if (runqueue.currentSize == 0 )
@@ -450,8 +466,6 @@ void *scheduler(void *args)
450466
while (scheduler_mode == SCHEDULER_WAITING )
451467
pthread_cond_wait(&scheduler_cond_var, &lock2);
452468

453-
// printf("Entered\n");
454-
455469
pthread_mutex_lock(&lock1);
456470

457471
struct Process_Control_Block pcb = get_min_pcb(&runqueue);
@@ -460,21 +474,24 @@ void *scheduler(void *args)
460474

461475
states_array[pcb.pid - 1] = RUNNING;
462476
pthread_cond_signal(&(cond_var_array[pcb.pid - 1]));
463-
//broadcast(cond_var_array, allp);
464-
465-
//printArray(states_array, allp);
466477

467478
if (outmode == 3)
468479
{
469-
//printf("Process with pid %d is selected for CPU\n", pcb.pid);
480+
if(checkOut == 1)
481+
fprintf(fp2, "Process with pid %d is selected for CPU\n", pcb.pid);
482+
else
483+
printf("Process with pid %d is selected for CPU\n", pcb.pid);
470484
}
471485

472486
pthread_mutex_unlock(&lock2);
473487

474488
sem_post(&mutex);
475489
}
476490

477-
printf("Scheduler exit\n");
491+
if(checkOut == 1)
492+
fprintf(fp2, "Scheduler exit\n");
493+
else
494+
printf("Scheduler exit\n");
478495

479496
pthread_exit(0);
480497
}
@@ -490,20 +507,3 @@ int isAllpFinished(int size)
490507
}
491508
return 1;
492509
}
493-
494-
void printArray(int *arr, int size)
495-
{
496-
for (int i = 0; i < size; i++)
497-
{
498-
printf("%d:%d, ", i + 1, arr[i]);
499-
}
500-
printf("\n");
501-
}
502-
503-
void broadcast(pthread_cond_t *arr, int size)
504-
{
505-
for (int i = 0; i < size; i++)
506-
{
507-
pthread_cond_signal(&arr[i]);
508-
}
509-
}

0 commit comments

Comments
 (0)