Mutual exclusion typically imposes some conditions on access to a given resource by two or more different processes. For instance, a process can proceed beyond a certain point only after another process has reached some other point of its execution. Â If the points in the processes are located before and after the protected resource, then mutual exclusion is achieved. The introduction of a time precedence order in the execution of several processes is called synchronization. Process synchronization is normally applied in operating system and in practice, it is employed for the implementation of resource protection, that is, the access to a resource is ordered in time with the help of a synchronization mechanism.
In computer science, a semaphore is an object that controls the access to a common resource. Putting it in other words, a semaphore is an integer variable which can only be 0 or take positive values. Its initial value is defined in its first declaration in a program and is usually equal to 0 or 1. Semaphores which can take only values 0 and 1 are called binary semaphores.
Two operations are defined on semaphores: signal and wait. The signal operation increases the value of the semaphore by 1; it does not have any other effect on the executing process. The wait operation leads to different results, depending on the current value of the semaphore. If this value is greater than 0, it is decreased by 1 and the process calling the wait function can proceed. If the semaphore has value 0, execution of the process calling wait is halted until the value is increased again by another process with a signal operation. Only then is it possible for wait to decrease the value of the semaphore and proceed with execution.
Note that, the operations of test and decrement of the wait functions are executed in one step only. The operating system is not allowed to break the execution of wait after the test on the value and before the decrement operation. The semaphore wait has the same operational significance as the function test_and_set.
As aforementioned, if the process has value 0, the process must wait for a signal. If several processes are waiting for the same signal, only one of them may continue execution when signal is given. Depending on the implementation, the processes may wait in ordered FIRST IN, FIRST OUT queue or may also be selected at random to proceed. The semaphore alone does not imply a given wait and execution order.
One semaphore variable is enough for access coordination. The processes executing wait are put in the waiting queue and do not have to consume CPU time to test the state of the semaphore. The operating system releases a process when the signal is executed.
Semaphores can be used to restrict the number of threads that access a shared resource. For example, considering the use of semaphores in Java; before accessing a resource, a thread must acquire a permit from the semaphore. After finishing with the resource, the thread must return the permit back to the semaphore as illustrated in the figure below:
Using semaphores, the two processes can access the common resource in an ordered manner. No unnatural bonds are introduced; if one process runs faster than the other one, it will simply access the resource more frequent in a given time interval. A process is forced to wait for the other one only when the latter is in the protected area. If a process should for one reason or the other stop running, provided this occurs outside the protected area, the other is not hindered from continuing its execution.
You may also read:
The world of electronics is constantly evolving, allowing designers and manufacturers to push the boundaries…
PIC microcontrollers are fabricated by Microchip Technology. PIC16C84 and PIC16F84 are the two microcontrollers in…
The ADD instruction tells the microcontroller’s CPU to add the source byte to register A…
8-bit microprocessors are limited in their speed (the number of instructions that can be executed…
The 68HC11 (also abbreviated as 6811 or HC11) is an 8-bit microcontroller that was introduced…
In envisaging a new microcontroller design, it is prudent to follow a design procedure that…