5
5
#include <stdlib.h>
6
6
#include <ctype.h>
7
7
#include <time.h>
8
+ #include <ctype.h>
8
9
#include <semaphore.h>
9
10
10
11
#include "distribute.h"
@@ -43,6 +44,10 @@ int pcb_array_currentSize;
43
44
44
45
int scheduler_mode = SCHEDULER_WAITING ;
45
46
47
+ int checkOut = 0 ;
48
+
49
+ FILE * fp2 ;
50
+
46
51
// Functions and definitions
47
52
48
53
void * generator (void * args );
@@ -77,10 +82,6 @@ struct scheduler_params
77
82
78
83
int isAllpFinished (int size );
79
84
80
- void printArray (int * arr , int size );
81
-
82
- void broadcast (pthread_cond_t * arr , int size );
83
-
84
85
int main (int argc , char const * argv [])
85
86
{
86
87
if ( argc < MIN_ARGS )
@@ -116,8 +117,10 @@ int main(int argc, char const *argv[])
116
117
rqLen = atoi (argv [12 ]); allp = atoi (argv [13 ]); outmode = atoi (argv [14 ]);
117
118
118
119
if ( argc == MIN_ARGS_C + 1 )
119
- {
120
+ {
121
+ checkOut = 1 ;
120
122
strcpy (outfile ,argv [15 ]);
123
+ fp2 = fopen (outfile , "w" );
121
124
}
122
125
123
126
}
@@ -136,7 +139,9 @@ int main(int argc, char const *argv[])
136
139
137
140
if ( argc == MIN_ARGS_F + 1 )
138
141
{
142
+ checkOut = 1 ;
139
143
strcpy (outfile , argv [6 ]);
144
+ fp2 = fopen (outfile , "w" );
140
145
}
141
146
}
142
147
@@ -192,20 +197,30 @@ int main(int argc, char const *argv[])
192
197
pthread_join (generator_tid , NULL );
193
198
pthread_join (scheduler_tid , NULL );
194
199
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
+
197
205
int sum = 0 ;
198
206
for (int i = 0 ; i < pcb_array_currentSize ; i ++ )
199
207
{
200
208
struct Process_Control_Block tmp = pcb_array [i ];
201
209
int turna = tmp .finish_time - tmp .arrival_time ;
202
210
int waitr = turna - tmp .pLength ;
203
211
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 );
205
216
}
206
217
207
218
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 );
209
224
}
210
225
211
226
return 0 ;
@@ -257,14 +272,16 @@ void *generator(void *args)
257
272
258
273
while ( isFull (& runqueue ) )
259
274
usleep ( interarrival_time * 1000 );
260
-
261
275
262
276
// Create thread
263
277
pthread_create (& thread_id_array [i ], NULL , process , (void * ) & p_params );
264
278
265
279
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 );
268
285
}
269
286
270
287
usleep (interarrival_time * 1000 );
@@ -304,11 +321,12 @@ void *process(void *args)
304
321
305
322
pthread_mutex_unlock (& lock1 );
306
323
307
- printQueue (& runqueue );
308
-
309
324
if ( outmode == 3 )
310
325
{
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 );
312
330
}
313
331
314
332
gettimeofday (& arrival , NULL );
@@ -328,17 +346,22 @@ void *process(void *args)
328
346
pthread_cond_wait (& (cond_var_array [pcb .pid - 1 ]), & lock2 );
329
347
}
330
348
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 );
332
353
333
354
if (outmode == 2 )
334
355
{
335
356
gettimeofday (& running , NULL );
336
357
int time = (running .tv_usec - start .tv_usec ) / 1000 ;
337
- printf ("%d %d RUNNING\n" , time , pcb .pid );
338
358
}
339
359
else if (outmode == 3 )
340
360
{
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 );
342
365
}
343
366
344
367
int timeslice = calculate_timeslice (& runqueue , pcb .priority );
@@ -354,7 +377,10 @@ void *process(void *args)
354
377
actualruntime = timeslice ;
355
378
if (outmode == 3 )
356
379
{
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 );
358
384
}
359
385
}
360
386
@@ -378,13 +404,7 @@ void *process(void *args)
378
404
}
379
405
}
380
406
381
- //delete_pcb(&runqueue);
382
-
383
407
heapRebuild (& runqueue , 0 );
384
-
385
- //insert_pcb(&runqueue, pcb);
386
-
387
- printQueue (& runqueue );
388
408
389
409
pthread_mutex_unlock (& lock1 );
390
410
@@ -399,21 +419,19 @@ void *process(void *args)
399
419
pthread_mutex_unlock (& lock2 );
400
420
}
401
421
402
- printf ("FINISH\n" );
403
-
404
422
pthread_mutex_lock (& lock1 );
405
423
406
424
delete_pcb (& runqueue );
407
425
408
426
pthread_mutex_unlock (& lock1 );
409
427
410
- // printQueue(&runqueue);
411
-
412
428
states_array [pcb .pid - 1 ] = FINISHED ;
413
429
if (outmode == 3 )
414
430
{
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 );
417
435
}
418
436
gettimeofday (& finish , NULL );
419
437
pcb_array [pcb_array_currentSize ] = pcb ;
@@ -434,11 +452,9 @@ void *scheduler(void *args)
434
452
struct scheduler_params * sParams = (struct scheduler_params * ) args ;
435
453
int outmode = sParams -> outmode ;
436
454
int allp = sParams -> allp ;
437
- //printf("%d\n", isAllpFinished(allp));
455
+
438
456
while ( isAllpFinished (allp ) == 0 )
439
457
{
440
- //printf("%d\n", isAllpFinished(allp));
441
-
442
458
pthread_mutex_lock (& lock2 );
443
459
444
460
if (runqueue .currentSize == 0 )
@@ -450,8 +466,6 @@ void *scheduler(void *args)
450
466
while (scheduler_mode == SCHEDULER_WAITING )
451
467
pthread_cond_wait (& scheduler_cond_var , & lock2 );
452
468
453
- // printf("Entered\n");
454
-
455
469
pthread_mutex_lock (& lock1 );
456
470
457
471
struct Process_Control_Block pcb = get_min_pcb (& runqueue );
@@ -460,21 +474,24 @@ void *scheduler(void *args)
460
474
461
475
states_array [pcb .pid - 1 ] = RUNNING ;
462
476
pthread_cond_signal (& (cond_var_array [pcb .pid - 1 ]));
463
- //broadcast(cond_var_array, allp);
464
-
465
- //printArray(states_array, allp);
466
477
467
478
if (outmode == 3 )
468
479
{
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 );
470
484
}
471
485
472
486
pthread_mutex_unlock (& lock2 );
473
487
474
488
sem_post (& mutex );
475
489
}
476
490
477
- printf ("Scheduler exit\n" );
491
+ if (checkOut == 1 )
492
+ fprintf (fp2 , "Scheduler exit\n" );
493
+ else
494
+ printf ("Scheduler exit\n" );
478
495
479
496
pthread_exit (0 );
480
497
}
@@ -490,20 +507,3 @@ int isAllpFinished(int size)
490
507
}
491
508
return 1 ;
492
509
}
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