Question: In this part, you need to implement condition variables for OS/161. The interface for the cv structure is defined in synch.h and stubbed code is

In this part, you need to implement condition variables for OS/161. The interface for the cv structure is defined in synch.h and stubbed code is provided in synch.c. The original idea behind a condition variable is to allow a thread to suspend itself while executing in a monitor until some Boolean condition is satisfied. Executing in a monitor implies that the thread is holding some lock, which must be passed as an argument to the cv_wait function. Threads that call cv_wait always block, and they must release the lock before doing so. Before returning from cv_wait, a thread must re-acquire the lock. Thus, the lock is held when cv_wait is called, and is held when cv_wait returns, but must be released while the thread sleeps. The pseudo code for cv_wait is given below: cv_wait (struct lock *lock, struct cv *cv) { // Atomically, do the following: // 1. release the lock // 2. put the thread to sleep on cv // 3. re-acquire the lock } The signal operation on a condition variable allows a thread executing in a monitor (which implies it must be holding the same lock that the waiting thread was holding when it called cv_wait) to notify a waiting thread that the thing it was waiting for has happened. The cv_signal function should wake up exactly one thread waiting on the address cv. If no threads are waiting, then a cv_signal has no effect. A read of the thread_wakeup function shows that it does not have the desired behavior for condition variable signals. You will need to implement a new function that does what you want. The cv_broadcast operation is identical to signal, except that it should wake up all threads waiting on the cv.

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!