Question: Synchronization: mutex and semaphore typedef struct semaphore { int N; list_t wait_list; mutex_lock_+ lock; } sem_ti void sem_init (sem_= *s, int N) { s->N =

 Synchronization: mutex and semaphore typedef struct semaphore { int N; list_t

Synchronization: mutex and semaphore typedef struct semaphore { int N; list_t wait_list; mutex_lock_+ lock; } sem_ti void sem_init (sem_= *s, int N) { s->N = N; list_init(&s->wait_list); // initializes the list mutex_lock_init(&s->lock); // initializes the lock } void sem_wait (sem_. *) { int tid; LOCK->lock; s->N--; if (s-> wait_list, tid); UNLOCK&S->lock); wait(); // returns the calling thread's unique id // adds tid to the waiting list // releases lock before sleeping (waiting) // waits until woken up by the wakeup // function with the tid } else { UNLOCK&S->lock); } } void sem_post(sem_= *9) { int tid; LOCK&S->lock); s->N++; if (!is_empty (s->wait_list)) { tid = list_remove_one (es->wait_list); // Removes and returns an element // from the waiting list UN LOCK&S->lock); // releases lock before waking // up the waiting thread wakeup (tid); // wakes up the waiting thread } else { UNLOCK & 9->lock); } } The code above implements a semaphore where multiple threads can call sem_wait and sem_post concurrently. In addition to the integer value N, the semaphore includes a waiting list that keeps track of the waiting threads and a mutex lock that protects the shared data inside the semaphore instance. Does this code correctly implement the semaphore? If not, what can go wrong? Synchronization: mutex and semaphore typedef struct semaphore { int N; list_t wait_list; mutex_lock_+ lock; } sem_ti void sem_init (sem_= *s, int N) { s->N = N; list_init(&s->wait_list); // initializes the list mutex_lock_init(&s->lock); // initializes the lock } void sem_wait (sem_. *) { int tid; LOCK->lock; s->N--; if (s-> wait_list, tid); UNLOCK&S->lock); wait(); // returns the calling thread's unique id // adds tid to the waiting list // releases lock before sleeping (waiting) // waits until woken up by the wakeup // function with the tid } else { UNLOCK&S->lock); } } void sem_post(sem_= *9) { int tid; LOCK&S->lock); s->N++; if (!is_empty (s->wait_list)) { tid = list_remove_one (es->wait_list); // Removes and returns an element // from the waiting list UN LOCK&S->lock); // releases lock before waking // up the waiting thread wakeup (tid); // wakes up the waiting thread } else { UNLOCK & 9->lock); } } The code above implements a semaphore where multiple threads can call sem_wait and sem_post concurrently. In addition to the integer value N, the semaphore includes a waiting list that keeps track of the waiting threads and a mutex lock that protects the shared data inside the semaphore instance. Does this code correctly implement the semaphore? If not, what can go wrong

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!