[ Previous: 3. Advantages and Disadvantages | Up: Site Map | Next: 5. Conclusion ]
Monitors
Well first and foremost it’s a language based mechanism, where in semaphores are low-level synchronization primitives. Monitors were also conceived to address the short comings of semaphores, in chief, the scattered nature of semaphores and the low level implementation, i.e. the convenience of forgetting P or a V, and to address the former reason in a semaphore based implementation, if you want to make changes, it is absolutely compulsory to find all places in the code, and amend accordingly. Which might take a very long time, and could be error prone for obvious reasons.
While semaphores is a special type of counter, monitors are a shared object. A monitor might have many entry points (condition variable), however, only one process can used to enter the monitor at any time. Hence, it implements mutual exclusion in contrast to semaphores (if init n > 1). A monitor consists of:
Suppose a procedure ought to be implemented if a certain condition is met, i.e a process can suspend itself and release the monitor, this is achieved with condition variable. Another process wanting to access that function/procedure would be queued for that particular procedure.
Continuing on with our favourite Hollywood actress Angelina Jolie, we would show how, her bank account could be implemented.
Notice that in the functions, P and V aren’t explicitly stated, this is because the compiler inserts it in.
In the code above the invariant balance, specifies that the balance, reflect past transactions. Since the monitor has a lock associated only Jolie or Pitt could access the account in a given time. So no worries to their millions.
Suppose Jolie decided for every 1000$ she deposited, she wanted a $100 to be given to a charity, all that has to be done is to come and insert the relevant code in the function deposit. However, in the semaphore based implementation, each and every place where the critical section is, we would have to add it. So as you can see it would be very cumbersome.
Lets look at the problem with the implementation of condition variables.
There is one condition variables declared here, OktoWithdraw. In this monitor, the deposit method/function/procedure after updating the balance, calls notifyAll(OktoWithdraw), in which, it would notify all threads in queue and they would resume activity. The rationale behind this is we may have some thread not able to withdraw the money because the amount is greater than the balance. After a deposit, we would want that thread to resume activity and check whether the balance now contains enough money for withdrawal.
When withdrawing, the procedure checks if the amount requested for withdrawal is greater than the balance and then calls the wait(OKtoWithdraw). In the wait method, the calling thread is enqueued in the queue associated with the condition variable OktoWithdraw. The monitor lock is released and the calling thread suspends operation. The other threads which were waiting earlier for the monitor lock now try to acquire the lock again. If the thread requests more money than the balance, it is made to wait (again) and the lock is passed on to the next thread trying to access it. When the condition finally becomes true, i.e. the amount of withdrawal is less than the balance (maybe after a deposit), the line "balance = balance - amount;" is carried out to update the balance.
Java Synchronization (Monitors)
Java uses the “synchronized “keyword to block a statement or a chunk of code so that only one thread can access it at a time. The “synchronized” keyword acts like a lock and it blocks all other threads from entering into that section of code, when it is being used by a thread. This is the Java way of implementing monitors.
In addition to placing the “synchronized” keyword before a chunk of code, it may also be used in conjunction with “wait”,”notify” or “notifyAll” statements, as these allow threads to wait when another thread wants to take over the “synchronized” code.
Let’s understand better with the following code:
Hope you all remember the scenario where Angelina Jolie and Brad Pitt tried accessing Jolie’s account at the same time and was left with $0! Well so the banks nowadays prevented that from happening by using a similar code like:
The situation now becomes as we would prefer:
| Angelina Jolie |
Brad Pitt |
| Load currentBalance=1 million Deposit newAmount=1 million (new balance 2 million) Update currentBalance=2 million |
|
| Load currentBalance=2 million Withdraw newAmount=1 million (new balance 1 million) Update currentBalance=1 million |
[ Previous: 3. Advantages and Disadvantages | Up: Site Map | Next: 5. Conclusion ]