This is a concept pertaining to Operating Systems and it is extensively used during the development and functioning of it. It is based on the Idea of locking and unlocking.
Now, tell me why do we lock doors once we are leaving the house or after we park the car in the parking lot? Think about it..
We don’t want unauthorized people to barge into our house or take away our car. We do this because it is something we “own” and we don’t want any other person to access it illegally. Taking that into account, suppose we sell our car and exchange it for a newer car. Now, we are not bothered about the locking status of the car since we don’t “own” it. The car might now have another owner who is taking care of it.
Now, let’s see how we can understand this concept from the Operating Systems point of view. Before we move on further, let me give you a small mapping which would help you understand this concept better.
Resource -> Something which is useful . Eg-Car
Semaphore is a variable which is used during the locking or unlocking of a resource. Whenever a process is using a resource it locks it so that no other process can access that resource during that period of time. It locks it by setting the value of the variable to zero, indicating that the resource is not available at the moment. Whenever the process has finished using the resource, it sets the value of the Semaphore to 1, indicating that the resource is now available for use.
Semaphores come into play while creating multi-threaded applications. Multi -threading means many processes. So, there are situations where many processes want to access the same resource at a given point of time. Semaphores help in resolving this Issue.
For Example-Servers are multi-threaded since they are able to handle and process many requests at a given point of time.