In: Computer Science
Q3) Explain; how the synchronization mechanism (semaphores) can
be used, to ensure a
correct access to exclusive resources.
A semaphore can control the access of a exclusive resource
through the use of a counter. If the counter value is greater than
0, then the process can access the resource, otherwise it has to
wait untill the other processes does not free up the
resource.
For example, let say there is a resource that can be shared by 2
processes at a time but not a third one.
So, we will set the initial value of the semaphore to 2. The first
process that requests for the resource will be able to use it, and
it will reduce the value of this semaphore to 1.
Now, the second process that requests for the same resource will
also reduce the value of the semaphore to 0.
If a third process requests for the resource, it will not be able
to execute because the semaphore value is not greater than 0.
This process can only execute when one of the above processes
release the resource and thus increase the value of semaphore.
This is how semaphore ensure a correct access to exclusive resources.
Hope this helps. Please rate the answer if you like it.
Do leave a comment. Any suggestion/query is much appreciated.