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

Problem 5: Describe how the compare_and_swap() instruction can be used to provid

ID: 3705114 • Letter: P

Question

Problem 5: Describe how the compare_and_swap() instruction can be used to provide mutual exclusion that satisfies the bounded-waiting requirement. Consider how to implement a mutex lock using an atomic hardware instruction. Assume that the following structure defining the mutex lock is available: a) b) typedef struct int available; lock; where (available0) indicates the lock is available; a value of 1 indicates the lock is unavailable. Using this struct, illustrate how the following functions may be implemented using the test_and_set() and compare_and_swap() instructions void acquire(lock *mutex) void release(lock *mutex) Be sure to include any initialization that may be necessary. c) Show how to implement the wait() and signal() semaphore operations in multiprocessor environments using test and set() waiting. instruction. The solution should exhibit minimal busy

Explanation / Answer

1.Ans
********

compare-and-swap is an atomic instruction used in multithreading to achieve synchronization.

do {
waiting[x] = TRUE;
key = TRUE;
while (waiting[x] && key) key = Swap(&lock, &key);
waiting[x] = FALSE;
/ critical section /
y = (x+1) % n;
while ((y != x) && !waiting[y])
y = (y+1) % n;
if (y== x) lock = FALSE;
else waiting[y] = FALSE;
while (TRUE);
}

2.Ans:
Test and Set:
************
typedef struct
{
int available;
}
lock;
void init(lock *mutex)
{
mutex->available=0;
}
int test_And_Set(int *destination)
{
int xy = *destination;
*destination = true;
return xy
}
void acquire(lock *mutex)
{
while(test_and_set(&mutex->available,1)==1);   
}
void release(lock *mutex)
{
mutex->available=0;
}

Compare and Swap:
*****************
int compare_and_Swap(int *pointer,int expected,int first)
{
int original = *pointer;
if(original == expected)
*pointer = first;
return original;
}
void acquire(lock *mutex)
{
while(compare_and_swap(&mutex->available,0,1)==1);
  
}
void release(lock *mutex)
{
mutex->available=0;
}   

3

A wait operation atomically decrements the value associated with a semaphore.
If two wait operations are executed on a semaphore when its value is 1, if the two operations are not performed atomically,
then it is possible that both operations might proceed to decrement the semaphore value thereby violating mutual exclusion.
A semaphore can be imlemented using the following code

wait()
int guard = 0;
int semaphore value = 0;
{ while (TestAndSet(&guard) == 1);
if (semaphore value == 0) { atomically add process to a queue of processes
waiting for the semaphore and set guard to 0;
}
else { semaphore value--;
guard = 0;
}
}
signal()
int guard = 0;
int semaphore value = 0;
{ while (TestAndSet(&guard) == 1);
if (semaphore value == 0 && there is a process on the wait queue)
wake up the first process in the queue of waiting processes
else
semaphore value++;
guard = 0;
}