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

Semaphore

Semaphores provide synchronization between tasks by controlling access to shared resources through counting semaphores. A semaphore consists of an integer count and a queue of waiting tasks. Operations like P() and V() decrement and increment the count to control access. Semaphores can be used for cooperation between a producer and consumer task to prevent overflow or underflow of a shared buffer through semaphores that track empty and full spots. Monitors improve on semaphores by encapsulating shared data and operations to provide synchronized access and avoid some issues with semaphore implementations.

Uploaded by

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

Semaphore

Semaphores provide synchronization between tasks by controlling access to shared resources through counting semaphores. A semaphore consists of an integer count and a queue of waiting tasks. Operations like P() and V() decrement and increment the count to control access. Semaphores can be used for cooperation between a producer and consumer task to prevent overflow or underflow of a shared buffer through semaphores that track empty and full spots. Monitors improve on semaphores by encapsulating shared data and operations to provide synchronized access and avoid some issues with semaphore implementations.

Uploaded by

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

SEMAPHORE

Semaphore-provide synchronization of tasks.


Semaphore-data

structure

consisting

of an

integer and a queue that stores task descriptors.


Task descriptor-data structure that stores all of
the relevant information about the execution state
of a task.
The idea behind semaphores: to provide limited
access to a data structure, guards are placed
around the code that accesses the structure.

Guard allows the guarded code to be executed


only when a specific condition is true. A guard
can allow only one task to access a shared data
structure at a time.
Semaphore -implementation of a guard.
Two operations in Semaphore:
P-Passeren(to pass)
V-Vrygeren(to release)

Emptyspot-counter to maintain number of


empty locations in a shared buffer used by
producers and consumers.
Fullspot-counter to maintain number of filled
locations in a shared buffer

COOPERATION SYNCHRONIZATION
semaphore fullSpots, emptyspots;
fullSpots.count = 0;
emptySpots.count = BUFLEN;
task producer
loop
-- produce VALUE -wait(emptySpots);
DEPOSIT(VALUE);
release(fullSpots);
end loop
end producer

task consumer
loop
wait(fullSpots);
FETCH(VALUE);
release(emptySpots);
-- consume VALUE -end loop
end consumer

EXAMPLE:
Study rooms-5(A,B,C,D,E)
Total students=10.One student can use 1 study room at
a time.(1,2,3,4,5,6,7,8,9,10)
Emptyspots-no. of empty positions in the buffer.
Fullspots-no. of filled positions in the buffer.
STEP 1:
Emptyspots=5
Fullspots=0

CASE 1:
Emptyspots=5
Fullspots=0
wait(emptySpots);
Wait(5);
if(5>0)
Decrement emptyspots counter
Emptyspots=4

CASE 2:
Emptyspots=0
else //emptyspots<0
put the caller in Sems queue;
attempt to transfer control to some ready task (if the task
queue is empty, deadlocks occur)

STUDY
ROOM
AVAILA
BLE

ROOM
NO

STUDE
NT

{A,B,C,D
,E}

{B,C,D,E
}

{1}

{C,D,E}

{D,E}

QUEUE

EMPTYSP
OTS

FULLSPOT
S

{2,3,4,5,6,
7,8,9,10}

{2}

{3,4,5,6,7,
8,9,10}

{3}

{4,5,6,7,8,
9,10}

{E}

{4}

{5,6,7,8,9,
10}

{5}

{6,7,8,9,
10}

{1,2,3,4 {1,2,3,4,5,
,5,6,7,8, 6,7,8,9,10}
9,10}

Consumer must wait for the producer to produce something if


the queue is empty.
Producer must wait for the consumer to consume something if
the queue is full.

wait(Sem)
if Sems counter > 0 then
decrement Sems counter
else
put the caller in Sems queue;
attempt to transfer control to some ready
task (if the task queue is empty, deadlocks
occur)

release(Sem)
if Sems queue is empty (no task is waiting)
then
increment Sems counter
else
put the calling task in the task-ready
queue;
transfer control to a task from Sems
queue.

Competition Synchronization
Concurrently accessed shared buffer implementation with
semaphores
semaphore access, fullSpots, emptySpots;
access.count = 1;
fullSpots.count = 0;
emptySpots.count = BUFFER_LENGTH;
task producer
loop
-- produce VALUE -wait(emptySpots);
wait(access);
DEPOSIT(VALUE);
release(access);
release(fullSpots);
end loop
end producer

task consumer
loop
wait(fullSpots);
wait(access);
FETCH(VALUE);
release(access);
release(emptySpots);
-- consume VALUE -end loop
end consumer

DISADVANTAGES OF SEMAPHORES
Using semaphores for synchronization creates an
unsafe environment.
Cooperation Synchronization:
Leaving the wait(emptySpots) statement out of the
producer task would cause a buffer overflow.
Leaving the wait(fullSpots) statement out of the
consumer task would result in buffer underflow.

Competition Synchronization:
Leaving out the wait(access) statement in either
task can cause insecure access to the buffer.
Leaving out the release(access) statement in either
task results in a deadlock.

MONITORS
Monitors solve the problems of semaphores by
encapsulating shared data structures with their
operations and hiding their implementation.
Monitor
Process Sub 1
Process Sub 2

Process Sub 3
Process Sub 4

Insert
Remove

B
U
F
F
E
R

Competition Synchronization: Because all


access resides in the monitor, a monitor
implementation can be made to guarantee
synchronized access by allowing only one
access at a time.
Cooperation Synchronization: Cooperation
between processes remains the task of the
programmer who must ensure that a shared
buffer does not experience underflow or
overflow.

Evaluation: Monitors are a better way to


provide synchronization than semaphores,
although some of the semaphore problems in
the

implementation

synchronization do remain.

of

cooperation

SYNCHRONOUS MESSAGE
PASSING
Suppose that task A and task B are both executing, and A wishes to
send a message to B. B is busy, so it is not desirable to allow another
task to interrupt it.
Instead, B can tell other tasks when it is ready to receive messages.
Task A can then send a message. An actual transmission is referred
to as a rendezvous.
Message Passing (both synchronous and asynchronous) is available
in Ada. Cooperation and Competition Synchronization can both be
implemented using message passing.

You might also like