A semaphore is a protected variable whose value can be accessed and altered only by the operations P and V and an initialization operation w...

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


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.


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;
                                While true do
                                                Calculate nextresult;
                                                P( number retrieved );
                                                Numberbuffer := nextresult;
                                                V ( numberdeposited )
Procedure consumerprocess;
                Var nextresult : integer;
                                While true do
                                                P( numberdeposited);
                                                Nextresult := numberbuffer;
                                                Write ( nextresult )
                Semaphoreinitialize ( numberdeposited, 0);
                Semaphoreinitialize ( numberretrieved, 1);

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.

Best Online Tutorials | Source codes | Programming Languages: OS-Semaphores
Best Online Tutorials | Source codes | Programming Languages
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content