Question: Does the following lock implemented correctly? If not, point out the problems and justify your answers. typedef struct __lock_t { int flag; // flag ==
Does the following lock implemented correctly? If not, point out the problems and justify your answers.
typedef struct __lock_t
{
int flag; // flag == 0 means resource is free; otherwise, resource is not available
}
lock_t;
void init(lock_t *lock)
{
lock->flag = 1;
}
void acquire(lock_t *lock)
{
while(xchg(&lock->flag, 1) == 0) ; // spin-wait (do nothing)
}
void release(lock_t *lock)
{
lock->flag = 1;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
