0% found this document useful (0 votes)
75 views

Semaphores: Detail 1

E.W. Dijkstra introduced the concept of semaphores in 1965 to address mutual exclusion and synchronization issues. A semaphore is a protected variable that can only be accessed via P and V atomic operations. The P operation decrements the semaphore value if it is positive, or waits if it is 0. The V operation increments the value and wakes any waiting processes. Counting semaphores can assume non-negative integer values and are commonly used to control access to shared resources and ensure only a fixed number access them at once, such as in the producer-consumer problem.

Uploaded by

Meet Pandya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

Semaphores: Detail 1

E.W. Dijkstra introduced the concept of semaphores in 1965 to address mutual exclusion and synchronization issues. A semaphore is a protected variable that can only be accessed via P and V atomic operations. The P operation decrements the semaphore value if it is positive, or waits if it is 0. The V operation increments the value and wakes any waiting processes. Counting semaphores can assume non-negative integer values and are commonly used to control access to shared resources and ensure only a fixed number access them at once, such as in the producer-consumer problem.

Uploaded by

Meet Pandya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Detail 1

Semaphores


E.W. Dijkstra (1965) abstracted the key notion of mutual exclusion in his concepts of
semaphores.
Definition
A semaphore is a protected variable whose value can be accessed and altered only by the
operations P and V and initialization operation called 'Semaphoiinitislize'.
Binary Semaphores can assume only the value 0 or the value 1 counting semaphores also
called general semaphores can assume only nonnegative values.

The P (or wait or sleep or down) operation on semaphores S, written as P(S) or wait (S),
operates as follows:
P(S): IF S > 0
THEN S := S - 1
ELSE (wait on S)

The V (or signal or wakeup or up) operation on semaphore S, written as V(S) or signal (S),
operates as follows:
V(S): IF (one or more process is waiting on S)
THEN (let one of these processes proceeds)
ELSE S: = S +1

Operations P and V are done as single, indivisible, atomic action. It is guaranteed that once a
semaphore operation has stared, no other process can access the semaphore until operation
has completed. Mutual exclusion on the semaphore, S, is enforced within P(S) and V(S).
If several processes attempt a P(S) simultaneously, only process will be allowed to proceed.
The other processes will be kept waiting, but the implementation of P and V guarantees that
processes will not suffer indefinite postponement.
Semaphores solve the lost-wakeup problem.
Producer-Consumer Problem Using Semaphores
The Solution to producer-consumer problem uses three semaphores, namely, full, empty and
mutex.
The semaphore 'full' is used for counting the number of slots in the buffer that are full. The
'empty' for counting the number of slots that are empty and semaphore 'mutex' to make sure
that the producer and consumer do not access modifiable shared section of the buffer
simultaneously.
Initialization
Set full buffer slots to 0.
i.e., semaphore Full = 0.
Set empty buffer slots to N.
i.e., semaphore empty = N.
For control access to critical section set mutex to 1.
i.e., semaphore mutex = 1.
Producer ( )
WHILE (true)
produce-Item ( );
P (empty);
P (mutex);
enter-Item ( )
V (mutex)
V (full);
Consumer ( )
WHILE (true)
P (full)
P (mutex);
remove-Item ( );
V (mutex);
V (empty);
consume-Item (Item)












DETAI L 2

Counting semaphores are equipped with two operations, historically denoted as V (also known as
signal()) and P (or wait())(see below). Operation V increments the semaphore S, and operation P
decrements it. The semantics of these operations are shown below. Square brackets are used to
indicate atomic operations, i.e., operations which appear indivisible from the perspective of other
processes.
The value of the semaphore S is the number of units of the resource that are currently available.
The P operation wastes time or sleeps until a resource protected by the semaphore becomes
available, at which time the resource is immediately claimed. The V operation is the inverse: it makes
a resource available again after the process has finished using it. One important property of
semaphore S is that its value cannot be changed except by using the V signal() and P wait()
operations.
A simple way to understand wait () and signal() operations is:
wait(): Decrements the value of semaphore variable by 1. If the value becomes negative, the
process executing wait() is blocked, i.e., added to the semaphore's queue.
signal(): Increments the value of semaphore variable by 1. After the increment, if the pre-
increment value was negative (meaning there are processes waiting for a resource), it transfers a
blocked process from the semaphore's waiting queue to the ready queue.
Many operating systems provide efficient semaphore primitives that unblock a waiting process when
the semaphore is incremented. This means that processes do not waste time checking the
semaphore value unnecessarily.
The counting semaphore concept can be extended with the ability to claim or return more than one
"unit" from the semaphore, a technique implemented in UNIX. The modified V and P operations are
as follows:
function V(semaphore S, integer I):
[S S + I]

function P(semaphore S, integer I):
repeat:
[if S >= 0:
S S - I
break]
To avoid starvation, a semaphore has an associated queue of processes (usually a first-in, first out). If
a process performs a P operation on a semaphore that has the value zero, the process is added to
the semaphore's queue. When another process increments the semaphore by performing
a V operation, and there are processes on the queue, one of them is removed from the queue and
resumes execution. When processes have different priorities the queue may be ordered by priority, so
that the highest priority process is taken from the queue first.

You might also like