Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(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

Step: 1

Answer There are a couple of potential issues with the given code Race Condition There is a race con... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Algorithms

Authors: Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest

3rd edition

978-0262033848

More Books

Students also viewed these Programming questions

Question

Show that k ln k = (n) implies k = (n/ ln n).

Answered: 1 week ago