Semaphore
Semaphore
structure
consisting
of an
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}
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
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.