Question: Why the following implementation using a shared variable mutex as a lock implementation fail in achieving mutual exclusion for two threads to update a shared

 Why the following implementation using a shared variable mutex as alock implementation fail in achieving mutual exclusion for two threads to update

Why the following implementation using a shared variable mutex as a lock implementation fail in achieving mutual exclusion for two threads to update a shared variable balance? Suppose the shared variable mutex is initialized to 0 . In particular, which condition it violates? T0 T1 /* lock implementation */ while (mutex == 1) ; // spin-wait mutex =1; balance = balance +1 ; /* unlock */ mutex =0; /* lock implementation */ while (mutex ==1 ) ; // spin-wait mutex =1; balance = balance +1 ; /* unlock */ mutex =0; At most one process/thread in a critical section No assumptions about CPU speed or number of CPUs Should eventually be able to enter CS Cannot prevent entry if no others inside the critical section Test-and-set (Atomic exchange) hardware instruction reads the 32 bit value from a memory location and set a new value to the same memory location as a single atomic instruction. The following are the machine effects in C. int TestAndSet(int *ptr, int new) \{ / atomic read followed by update */ int old = ptr; *ptr = new; return old; \} Select one that correctly implements a lock to enter a critical section (mutual exclusion) using the TestAndSet above, Suppose the shared variable mutex is initialized to 0 . while (TestAndSet (&mutex,1)==1); while (TestAndSet (&mutex,1)==0); while (TestAndSet (& mutex,0)==0) ; while (TestAndSet (& mutex,0)==1)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!