Question: The following code block is to swap values stored at two memory addresses $s1 and $s3: lw $s0, 0($s1) lw $s2, 0($s3) sw $s0, 0($s3)

The following code block is to swap values stored at two memory addresses $s1 and $s3:

lw $s0, 0($s1)

lw $s2, 0($s3)

sw $s0, 0($s3)

sw $s2, 0($s1)

 

However, in a multiprocessing environment, this wouldn't work since between the first line and the last line, if some other thread changes the content of the memory at addresses $s1 and/or $s3, it will create a mess (the swap would not be implemented correctly). In such an environment, you need LL and SC to ensure that the swap is atomic: when the last line is run, the content at addresses $s1 and $s3 has not been changed since after the first line. Modify the above code (which is properly commented) to implement the atomic swap.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

The issue here is that between the code lines the current thread could be interrupted and another thread could alter the values stored in memory leadi... View full answer

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 Operating System Questions!