@@ -28,6 +28,8 @@ pthread_mutex_t lock1;
28
28
29
29
pthread_mutex_t lock2 ;
30
30
31
+ sem_t mutex ;
32
+
31
33
pthread_cond_t scheduler_cond_var ;
32
34
33
35
pthread_cond_t * cond_var_array ;
@@ -144,6 +146,7 @@ int main(int argc, char const *argv[])
144
146
init_queue (& runqueue , rqLen );
145
147
pthread_mutex_init (& lock1 , NULL );
146
148
pthread_mutex_init (& lock2 , NULL );
149
+ sem_init (& mutex , 0 , 1 );
147
150
pthread_cond_init (& scheduler_cond_var , NULL );
148
151
149
152
cond_var_array = malloc (sizeof (pthread_cond_t ) * allp );
@@ -315,6 +318,8 @@ void *process(void *args)
315
318
{
316
319
pthread_mutex_lock (& lock2 );
317
320
321
+ sem_wait (& mutex );
322
+
318
323
scheduler_mode = SCHEDULER_RUNNING ;
319
324
pthread_cond_signal (& scheduler_cond_var );
320
325
@@ -377,7 +382,6 @@ void *process(void *args)
377
382
378
383
heapRebuild (& runqueue , 0 );
379
384
380
-
381
385
//insert_pcb(&runqueue, pcb);
382
386
383
387
printQueue (& runqueue );
@@ -387,6 +391,11 @@ void *process(void *args)
387
391
states_array [pcb .pid - 1 ] = WAITING ;
388
392
pcb .context_switch ++ ;
389
393
394
+ scheduler_mode = SCHEDULER_RUNNING ;
395
+ pthread_cond_signal (& scheduler_cond_var );
396
+
397
+ sem_post (& mutex );
398
+
390
399
pthread_mutex_unlock (& lock2 );
391
400
}
392
401
@@ -458,10 +467,11 @@ void *scheduler(void *args)
458
467
if (outmode == 3 )
459
468
{
460
469
//printf("Process with pid %d is selected for CPU\n", pcb.pid);
461
- //printf("Runquoue size %d\n", runqueue.currentSize);
462
470
}
463
471
464
472
pthread_mutex_unlock (& lock2 );
473
+
474
+ sem_post (& mutex );
465
475
}
466
476
467
477
printf ("Scheduler exit\n" );
0 commit comments