I had made my below link using this content
One of the most important benefit of having a machine doing our job is its speed and the trust we are having about the completion of the work.
For most of us productivity of a computer is not dependent upon its ability but its related to our imagination. Talking about myself no matter how hard we try but for a computer that work is even not a feet. Computers are basically meant to do hard and smart work. Our job is to give challanges to the machine. One category of these challanges is Multitasking.
A computer (Mainly with single processor and memory) can not do multiple tasks at same time. But don’t take it too seriously, we humans neither take it also :p
Infact multitasking in computers and real life is just an illusion. It’s nothing but just the speed and flexibility to smoothly switch between multiple tasks. And one thing that we all need is Synchronisation.
Synchronization we all know is just an agreement between coexisting things to work concurrently. Same is in case of computers which does multiple jobs of a same process (Threads). These share computer resources like Processor, Memory etc.
As such this sharing sometimes turns into competition leading for certain tasks to freeze (Deadlock). In other words it hinders the completion of certain tasks.
So now the million dollar question is how to make these threads coordinate with each other so that sharing is done synchronously. Semaphores is one of the answer for above question.
A common example of a semaphore is the traffic head light. It has only three colors which build a communicating link between roads and the vehicles.
In computing semaphore is a variable associated with a resource. This variable decides whether the resource shall be assigned to the process or not.
The value of a semaphore is integer in nature. This value is never used directly by the programmer or the program. We can only increment it or decrement it.
Below are the basic steps which a Thread can do with respect to semaphores:
- Ask for access to a resource. This is generally called as wait.
- Keeping an access to a resource. This is generally called a Signal.
The Wait is basically grant of the access to the resource or remain in the waiting state till the completion of current Thread. The wait function decreases the value of the semaphore by one. And if after decrementing the value, the semaphore is still a positive quantity, the access is granted to that thread
An example about semaphores working is that if initially its value is 10. Then that means 10 threads can call the wait function of that semaphore and those threads will be assigned the resource if there are 10 copies of that particular resource.
In other words value of semaphore indicates about the no. of copies of a resource.
On the other side if value of semaphore is negative after decrementation it means that the particular thread has to wait in a queue as that resource is being assigned to some other thread. In other words that negative number indicates about the no. of threads waiting for that particular resource.
Now talking about the Signal, it basically increases the value by one. (In case of semaphores normally the incrementation or decrementation is by one only.)
It increases by one when the thread is leaving a resource and other threads are waiting for the same resource. Then it is assigned to the next thread and again decremented. This process goes on till all the threads have the access over the resource one by one.
Using only these 2 operation semaphores are used in a variety of Embedded systems and softwares requiring working of parallel processes in them.
If you all found it interesting and want to go in depth. You can have a look at The Little Book of Semaphores
Illusion: A false idea or belief
Relinquish: To let go