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

1 Expert Approved Answer
Step: 1 Unlock 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 Databases Questions!