Semaphore provides simple and effective way of controlling multiple threads trying to access same resource. This concepts was invented by dutch scientist in 1965. Semaphore can be used as deadlock handling mechanism for single resource. There are two main types of semaphore,
1. Counting semaphore :
Here semaphore represents arbitrary number of resource unit. Like 10,20 etc.
2. Binary semaphore :
Binary semaphore holds only two values 0 or 1. Means available/ not-available or locked /unlocked. This binary semaphore works nearly as Mutex.
Here is one simple example of explaining semaphore concept,
Consider internet cafe where 10 seat are available to be used by customer and there is one gate keeper. Now when customer wants to occupy seat in the cafe then first he/she has to take permission from gate keeper. If gate keeper is giving permission then and only then he/she can occupy seat. Again if customer wants to leave any seat then after leaving that seat customer has to inform gate keeper that he/she just released one seat.
Here gate keeper is simply maintaining count of number of seats ready to use. If count is more than 0 then gatekeeper will allow customer to occupy seat and if count is 0 then gate keeper will not allow you to occupy the seat.
This explains the semaphore concept where seats are number of resources, customers means processes and gate keeper means semaphore. Semaphore neer maintains which resources are empty but it only maintains number of resources ready to use.
Even if we are using semaphore the dead lock may occur because of multiple resource problem. This problem can be explained using dining philosophers problem.
1. Counting semaphore :
Here semaphore represents arbitrary number of resource unit. Like 10,20 etc.
2. Binary semaphore :
Binary semaphore holds only two values 0 or 1. Means available/ not-available or locked /unlocked. This binary semaphore works nearly as Mutex.
Here is one simple example of explaining semaphore concept,
Consider internet cafe where 10 seat are available to be used by customer and there is one gate keeper. Now when customer wants to occupy seat in the cafe then first he/she has to take permission from gate keeper. If gate keeper is giving permission then and only then he/she can occupy seat. Again if customer wants to leave any seat then after leaving that seat customer has to inform gate keeper that he/she just released one seat.
Here gate keeper is simply maintaining count of number of seats ready to use. If count is more than 0 then gatekeeper will allow customer to occupy seat and if count is 0 then gate keeper will not allow you to occupy the seat.
This explains the semaphore concept where seats are number of resources, customers means processes and gate keeper means semaphore. Semaphore neer maintains which resources are empty but it only maintains number of resources ready to use.
Even if we are using semaphore the dead lock may occur because of multiple resource problem. This problem can be explained using dining philosophers problem.