What is Semaphore
Semaphore is an integer variable (S). Two atomic operations, wait() and signal(), help to modify the value of the semaphore. When a specific process changes the semaphore value, another process cannot modify the semaphore value simultaneously.
The code of wait and signal are as follows.
wait(S)
{
while(s<=0);
s–;
}
signal(s)
{
s++;
}
Moreover, there are two types of semaphore; binary semaphore and counting semaphore. In a binary semaphore, the integer value can change between 0 to 1. If a process wants to access the critical section, it performs the wait() operation. Then, it decreases the semaphore value from 1 to 0. When exiting the critical section, it performs signal() operation on the semaphore. Thus, it will increase the value to 1.
In counting semaphores, the value can change in an unrestricted domain. If a process requires to execute in the critical section, it a () operation and decrements the semaphore value by 1. When exiting the critical section, it performs signal() operation and increments the semaphore value by one.
SEMAPHORES
Semaphores provide a much more organized approach to controlling the interaction of multiple processes than would be available if each user had to solve all inter process communications using simple variables, but more organization is possible. In a sense, semaphores are something like the goto statement in early programming languages; they can be used to solve a variety of problems, but they impose little structure on the solution and the results can be hard to understand without the aid of numerous comments. Just as there have been numerous control structures devised in sequential programs to reduce or even eliminate the need for goto statements, numerous specialized concurrent control structures have been developed which reduce or eliminate the need for semaphores.
Definition: The effective synchronization tools often used to release mutual exclusion in more complex systems are semaphores. A semaphore S is an integer variable which can be accessed only through two standard atomic operations: wait and signal. The definition of the wait and signal operation are:
The block operation suspends the process. The wakeup(P) operation resumes the execution of a blocked process P. These two operations are provided by the operating system as basic system calls.
One of the almost critical problem concerning implementing semaphore is the situation where two or more processes are waiting indefinitely for an event that can be only caused by one of the waiting processes: these processes are said to be deadlocked. To illustrate this, consider a system consisting of two processes P1 and P2, each accessing two semaphores S and Q, set to the value one:
P1 P2
wait(S); wait(Q);
wait(Q); wait(S);
... ...
signal(S); signal(Q);
signal(Q); signal(S);
Suppose P1 executes wait(S) and then P2 executes wait(Q). When P1 executes wait(Q), it must wait until P2 executes signal(Q). It is no problem, P2 executes wait(Q), then signal(Q). Similarly, when P2 executes wait(S), it must wait until P1 executes signal(S). Thus these signal operations cannot be carried out, P1 and P2 are deadlocked. It is clear, that a set of processes are in a deadlocked state, when every process in the set is waiting for an event that can only be caused by another process in the set.
No comments:
Post a Comment