Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1.In your own words, explain how the Compare and Swap instruction can prevent mo

ID: 3795647 • Letter: 1

Question

1.In your own words, explain how the Compare and Swap instruction can prevent more than one process using the shared resource.

2.In your own words, explain how a Semaphore can prevent more than one process using the shared resource.

3.In your own words, explain why the solution on the left is not a correct solution for the consumer/producer problem, but the right one is.

4.Two processes want to use the same shared resource. In your own words, explain how messages can prevent concurrency problems.

int compare-and-swap Cint *value, int expected, int new-value) int temp tok value if value expected) *value new-value; return tempi Figure 5.5 The definition of the compare-and-swap instruction. do while (compare-and-swap (&lock;, 0, 1) 0) do nothing critical section lock 0; remainder section while (true); Figure 5.6 Mutual-exclusion implementation with the compare-and-swap) instruction

Explanation / Answer

Compare and Swap instruction Uses 1 for locked state and 0 for available .

If a process checks when lock=1 , using the function compare_and_swap , the status does'nt change and it continues in the locked state .

If a process checks when lock=0 , using the function compare_and_swap , the status changes to lock=1 and a new lock is returned .

This ensures mutual exclusion as a lock is granted only when there is no existing lock . i.e. when lock == expected or from the code when , (*value == expected ) is true i.e. ( 0 == 0 )