OS-Semaphores

A semaphore is a protected variable whose value can be accessed and altered only by the operations P and V and an initialization operation we call semphoreinitialize. Binary semaphores can assume only the value 0 or the value 1. Counting semaphores ( or general semaphores) can assume only nonnegative integer values.

The P operation on semaphore S, written P(S), operates as follows :

If S > 0
Then S : = S - 1
Else ( Wait on S )
The V operation on semaphore S, written V ( S ), operates as follows,
If ( one or more processes are waiting on S )
Then ( let one of these processes proceed )
Else S := S+1


PROCESS SYNCHRONIZATION WITH SEMAPHORES:

When a process issues an input/output request, it blocks itself or await the completion of the I/O. Some other process must awaken the blocked process. Such and interaction is an example of a block/wakeup protocol.

Suppose one process wants to be notified about the occurrence of a particular event. Some other process is capable of detecting that event has occurred such implement of a simple two-process block/wakeup synchronization mechanism can be done by semaphore operations.

THE PRODUCER-CONSUMER RELATIONSHIP:

When one process passes data to another process. Such transmission is an example of inter-process communication.

Consider the following producer-consumer relationship. Suppose one process, a producer is generating information that a second process, a consumer is using. Suppose they communicate by a single shared integer variable, numberbuffer. The producer does some calculations and then writes the result into numberbuffer, the consumer reads the data from numberbuffer and prints it.

Suppose the speeds of the processes are mismatched. If the consumer is operating faster than the producer, the consumer could read and print the same number twice before this producer deposits the next number. If the producer is operating faster than the consumer, the producer could overwrite its previous result before the consumer has had a chance to read it and print it; a fast producer could in fact do this several times so that many results would be lost.

The producer and the consumer to cooperate in such a manner that data written to numberbuffer are neither lost nor duplicated. Enforcing such behavior is an example of process synchronization.

The algorithm below shows a concurrent program that uses semaphore operations to implement a producer-consumer relationship. Here there are 2 semaphores : number deposited is indicated (V’d) by the producer and tested (P’d) by the consumer; the consumer cannot proceed until a number has been deposited in numberbuffer. The consumer indicates (V’s) numberretrieved and the producer tests (P’s) it; the producer cannot proceed until a number already in the buffer has been retrieved. The initial settings of the semaphores force the producer to deposit a value in the numberbuffer before the consumer can proceed.
Producer – consumer relationship implemented with semaphores.

Program producer consumer relationship;
Var numberbuffer : integer;
                Numberdeposited : semaphore;
                Numberretrieved : semaphore;

Procedure producerprocess;
                Var nextresult : integer;
                Begin
                                While true do
                                Begin
                                                Calculate nextresult;
                                                P( number retrieved );
                                                Numberbuffer := nextresult;
                                                V ( numberdeposited )
                                End
                End;
Procedure consumerprocess;
                Var nextresult : integer;
                Begin
                                While true do
                                Begin
                                                P( numberdeposited);
                                                Nextresult := numberbuffer;
                                                V(numberretrieved);
                                                Write ( nextresult )
                                End
                End;
Begin
                Semaphoreinitialize ( numberdeposited, 0);
                Semaphoreinitialize ( numberretrieved, 1);
                Parbegin
                                Producerprocess;
                                Consumerprocess;
                Parend
End;


COUNTING SEMAPHORE: 
Counting semaphores are particularly useful when a resource is to be allocated from a pool of identical resources. The semaphore is initialized to the number of resources in the pool. Each P operation decrements the semaphore by 1, indicating that another resource has been removed from the pool and is in use by a process. Each V operation increments the semaphore by 1, indicating that a process has returned a resource to the pool, and the resource may be reallocated to another process. If a P operation is attempted when the semaphore has been decremented to zero, then the process must wait until a resource is returned to the pool by a V operation.
Share this article :
 
Copyright © 2012. Best Online Tutorials | Source codes | Programming Languages - All Rights Reserved