Consider the atomic exchange instruction defined as follows:
void exchange (int *a, int *b)
{
int temp;
temp = *b;
*b = *a;
*a = temp;
}
Now consider the solution to critical section problem based on above instruction:
1 int const n = /* number of processes */;
2 int lock = 0;
3 void P(int i)
4 {
5 int key = 1; //intent to obtain lock
6 while (true)
7 {
8 do exchange (&key, &lock)
9 while (key != 0); //if lock wasn’t free
10 /* critical section */;
11 lock = 0; //release lock, unblock
12 //other processes
13 /* remainder */;
14 }
15 }
16 void main()
17 {
18 lock = 0;
19 parbegin (P(1), P(2), ..., P(n));
20 }
Does the above code meets bounded waiting and progress requirements of critical section problem?