Question: (5pts) Consider the following multi-threaded C code: 1: // Barrier waits for n threads to call barrier_wait before 2: // allowing the threads to
(5pts) Consider the following multi-threaded C code: 1: // Barrier waits for n threads to call barrier_wait before 2: // allowing the threads to continue. 3: void barrier_init(barrier_t* b, unsigned int n) { 4: 5: 6: 7: } b->n = n; b->count = 0; sem_init(&b->semaphore, 0 /* not shared */, n/*init val*/); 8: void barrier_wait(barrier_t* b) { int new_count atomic_increment (&b->count); 9: 10: if(new_count b->n) { 11: sem_post(&b->semaphore); 12: } 13: 14: sem_wait(&b->semaphore) sem_post(&b->semaphore); 15: } Describe and fix any bug(s) you see, if any.
Step by Step Solution
There are 3 Steps involved in it
Answer There are a couple of potential issues with the given code Race Condition There is a race con... View full answer
Get step-by-step solutions from verified subject matter experts
