Skip to content

Commit fb014bd

Browse files
committed
Update cfs.c
1 parent 8a40974 commit fb014bd

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

cfs.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ pthread_mutex_t lock1;
2828

2929
pthread_mutex_t lock2;
3030

31+
sem_t mutex;
32+
3133
pthread_cond_t scheduler_cond_var;
3234

3335
pthread_cond_t *cond_var_array;
@@ -144,6 +146,7 @@ int main(int argc, char const *argv[])
144146
init_queue(&runqueue, rqLen);
145147
pthread_mutex_init(&lock1, NULL);
146148
pthread_mutex_init(&lock2, NULL);
149+
sem_init(&mutex, 0, 1);
147150
pthread_cond_init(&scheduler_cond_var, NULL);
148151

149152
cond_var_array = malloc(sizeof(pthread_cond_t) * allp);
@@ -315,6 +318,8 @@ void *process(void *args)
315318
{
316319
pthread_mutex_lock(&lock2);
317320

321+
sem_wait(&mutex);
322+
318323
scheduler_mode = SCHEDULER_RUNNING;
319324
pthread_cond_signal(&scheduler_cond_var);
320325

@@ -377,7 +382,6 @@ void *process(void *args)
377382

378383
heapRebuild(&runqueue, 0);
379384

380-
381385
//insert_pcb(&runqueue, pcb);
382386

383387
printQueue(&runqueue);
@@ -387,6 +391,11 @@ void *process(void *args)
387391
states_array[pcb.pid - 1] = WAITING;
388392
pcb.context_switch++;
389393

394+
scheduler_mode = SCHEDULER_RUNNING;
395+
pthread_cond_signal(&scheduler_cond_var);
396+
397+
sem_post(&mutex);
398+
390399
pthread_mutex_unlock(&lock2);
391400
}
392401

@@ -458,10 +467,11 @@ void *scheduler(void *args)
458467
if (outmode == 3)
459468
{
460469
//printf("Process with pid %d is selected for CPU\n", pcb.pid);
461-
//printf("Runquoue size %d\n", runqueue.currentSize);
462470
}
463471

464472
pthread_mutex_unlock(&lock2);
473+
474+
sem_post(&mutex);
465475
}
466476

467477
printf("Scheduler exit\n");

0 commit comments

Comments
 (0)