Semaphore, Mutex 'n Monitor

I speak not for myself, but for those without voice... those who have fought for their rights... their right to live in peace with the processor. 
- Malala Yousafzai (edited)
Semaphore, mutex and a monitor. Let us first treat them first as real-time entities, a counter, a key and a father. Try to imagine the concepts forgetting everything about Operating Systems.

The counter is placed outside a restaurant. The restaurant has 5 tables. The restaurant opens and the counter is set to 5. Abhinav came to the restaurant and sat at a table, and the value in the counter dropped to 4. Then Adithya came and sat at another table, and then the counter value dropped to 3. Aiswarya and Ajin came next in order and sat at 2 different tables, and the counter value changed to 1. By then, Abhinav have finished his job and left, and the counter value clocked up to 2. Now, Akhila came in and sat in one and Annette followed her and they both sat in 2 different tables. The counter value is 0 now. Now when Arun came and tried to enter, the door never opened as the counter value was 0. So he waited there. Just then Adithya came out and Arun saw the counter changing to 1. He immediately entered and sat in a table.

Semaphore is a variable or abstract data type that is used for controlling access, by multiple processes to a common resource. Counter is the semaphore, the tables are the resources and Abhinav, Adithya, Aiswarya, Ajin, Akhila and Arun are all processes trying to acquire the resources.

The key is placed outside a room inside which GOD is there. He is ready to answer any questions, but sets a rule that the key to the room is a special one, one of a kind. The door only opens with the key, which is kept in a basket just outside the door. Thus anyone going to meet GOD should take the key from the basket, open the door, lock it from inside and then have a convo. Thus hearing this, Abhinav, Adithya, Aiswarya and Ajin came rushing to the door. But Abhinav came first and took the key and went in. Others waited outside. Ajin was tricky enough and he managed to forge a key so similar to the original one and tried to open, but it was of no use. So they all waited outside and when Abhinav came outside, he threw the key in the basket. Thus Aditya, next in the line, took the key and entered into the room.

A mutex is a program object that allows multiple program threads to share the same resource, but not simultaneously. The program object here is the key and the resource is the GOD. Program threads are Abhinav, Adithya, Aiswarya and Ajin who all try to access the resource but the program object controls access to the resource.

Once upon a time, a father named Induchoodan had 5 pretty daughters, Adithya, Aiswarya, Akhila, Annette and Aswathy. Abhinav was a naughty boy and wanted to somehow be friends with them. So he one day went to their home and asked Induchoodan that can I say hello to your daughters, Induchoodan saw the innocence in Abhinav and said, OK but he set a condition that Abhinav cannot say hello to all his daughters at once, whereas he can only hello one daughter at a time. He also said no others are thus allowed to interact with his daughters anymore and gave Abhinav the ultimate authority. Abhinav was happy and abided by the rules. By then Arun came to know about Induchoodan's daughters and came and asked Induchoodan in the same way as Abhinav did. But Induchoodan took a gun and asked Arun to run for his life. While in the meantime, Abhinav asked Induchoodan that he would like to take his daughters for a dinner one day. Again Induchoodan, who was a very stubborn but kind father said Abhinav that he could take them, but one at a time. He could only take one girl one day. The happiest Abhinav accepted the rule again and had a happy 5 days in his life. Then he returned back and said to Induchoodan that he was happy and said he no longer need the ultimate authority to date his girls and left. Arun was observing it all, and as soon as he saw Abhinav leave, he ran to Induchoodan and smiled. Induchoodan came to Arun and smiled back and shook hands with Arun. He then said you now have the ultimate authority.

A monitor is a set of multiple routines which are protected by a mutual exclusion lock. The monitor is Induchoodan and the routines are his daughters Adithya, Aiswarya, Akhila, Annette and Aswathy. Abhinav and Arun are threads which can access these routines but only one can do it at a time. Once a thread gets the lock from the monitor and can do any routine at a time. Then the other thread should wait for the releasing of the lock. Once the lock gets released, it can take and use the monitors routines.

- all references made above, mean no disrespect nor are they meant to hurt anyone's feelings. These names are the ones that came to my mind and are the names of the people i love and respect the most. 
- what I meant to infer maybe wrong and is solely based on my readings and observations from the web. Please correct me if I am wrong.

Comments

Popular Posts