Synchronization - Monitors Flashcards
What is a condition variable?
A variable which supports two operations that enables other types of synchronization: wait and signal
What is a Monitor?
Mutex constructs generated by the compiler.
Only one process is active in a monitor at any given time.
The monitor provides queuing for waiting procedures.
Does signalling has effect if there are no waiting threads?
No.
If only one procedure is active in a monitor, when a signal occurs, which procedure is being executed, the signalling process or the signalled?
There are two ways to continue:
- The signaled procedure will execute first. That is, signalling procedure is immidietly followed by block() or exit_monitor().
- The signalling procedure is allowed to proceed.
Do you remember how Bounded Buffer Producer/Consumer is implemented with Monitors?
It is a good example of the monitor struct; it has condition variables (full, empty). it has global integers and of course, procedures.
What is the problem with the implementation of ProducerConsumer using Monitors? and in which case it does work properly?
- It works properly only if the signalled procedure is the next to enter(Hoare).*
- k producers are waiting. Meantime, k consumers enter one after the other such that only one producer is signalled. (instead of waking them all up - something that happens if the signalled procedure is the first to execute)*
- one producer, p1, is waiting. Meantime, a consumer enters and signals him. Another producer, p2, enters the monitor before p1 succeeded to increament the counter by 1. Eventually, p1 reaches the CS even though the buffer is full. (it behaves propely if the signalled procedure is the first to execute)*
How many condition variables a monitor in java has?
Only a single implicit one
What are the three synchronization operation in Java?
Wait
Notify
Notifyall
How is Monitor used in order to maintain synchronization?
The monitor “compiler” has to automatically insert this code into compiled procedures(in Java the keyword “synchronized” makes that happen):
Pay attention to a main problem with the implementation of Monitors using semaphores - deadlock. It happens due to lack of counting of the number of waiting procedures.
How deadlock is fixed in the implementation of monitors?
We simply add a count variable.
Before we enter the CS of some condition, we increase its counting, release mutex and then call down(C). anytime we finish, we check if count>0. if not, mutex is released if so, we decrease the counting and call up(C), so that the waiting procedure will access the CS and release mutex.
Note: the signalled procedure release mutex for the signalling procedure.
How many condition variables we have in the implementation of monitors in Java?
No named condition variables, only a single implicit one
what are the synchronization operations?
wait
notify
notifyall
What Java.Util.Concurrent provides?
Explicit lock interface
multiple condition variables per lock
(condition interface with lock.newCondition())
What is a barrier?
A type of synchronization method. Any process/thread must stop at this point and cannot proceed until all other processes/threads reach this barrier.