Answer:
Explained below
Explanation:
Compare and Swap(C&S) is simply an atomic operation whereby the compare and swap operations are automatically executed.
Now compare and Swap basically needs 3 arguments namely:
- 2 old values which we will label X and Y
- 1 new value which is written in X that we will call Z
Thus, we now have; C & S = {X, Y, Z}
To explain this well, let X be a variable where X has a value of 7.
Now, if a programmer gives a program me that X be multiplied by 2,then what C&S operation will do is;
I) Y = X where Y is a new variable.
II) Result = C&S(X, Y, X*7)
Variable X is global and this means that mere than one process and more than 1 thread can see the variable X.
Now, if a process named P1 wants multiply the variable X by 7 using C&S operation, it will first make a local copy of variable X (which in this case is now the new variable Y). After that it will atomically compare X & Y and if they are equal, it will replace X with 10X.
However, if they are not equal, P1 will re-read value of X into Y and carry of C&S instruction again.