Transcript SEMAPHORE

SEMAPHORE
By: Wilson Lee





Concurrency
Task
Synchronization
Example of semaphore
Language Support
Concurrency

First is instruction level



Second is statement level


executing two or more statement instructions simultaneously
Third is unit level


executing two or more machine instructions simultaneously
usually handled by optimizing compiler
executing two or more machine subprogram units simultaneously
Fourth is programming level


executing two or more machine program simultaneously
usually handled by the operating system
Concurrency Execution

It can be physical


on separate processor (single or multiple
processors)
It can be logical

in some time-sliced method on a single
processor computer system
Task



Task is a unit of a program that can be in
concurrent execution with other units of the
same program. Each task in a program can
provide one thread of control.
A task can communicate with other tasks
through shared non-local variables, through
message passing, or through parameters.
Because tasks often work together to solve
problems, they must use some form of
communication to either synchronize their
executions or shard data.
Synchronization

Two kind of synchronization

cooperation synchronization


when task A must wait for task B to complete
some specific activity before task A can
continue its execution.
competition synchronization

when two tasks require the use of some
resource that cannot be simultaneously used
Synchronization alternatives:



1. Semaphores
2. Monitors
3. Message Passing
Facts about semaphore





It is a mechanism that can be used to provide
synchronization of tasks.
It is a low level synchronization mechanism.
It was devised in 1965 by Edsger Dijkstra to provide
competition synchronization and also cooperation
synchronization.
It is a data structure that contains an integer and a
queue that store tasks descriptors.
Semaphore has only two operations. They are
pass/wait and release. Originally named P and V by
Dijkstra, after two Dutch words passeren (to pass)
and vrygeren (to release).
Two semaphore operations
wait (aSemaphore)

if aSemaphore’s counter > 0 then

decrement aSemaphore’s counter

else

put the caller in aSemaphore’s queue

attempt to transfer control to some ready task

(if the task ready queue is empty, deadlock occurs)

End
release(aSemaphore)

if aSemaphore’s queue is empty (no task is waiting) then

increment aSemaphore’s counter

else

put the calling task in the task ready queue

transfer control to a task from aSemaphore’s queue

end
Example of semaphore

Producer and consumer problem


The producer should stop producing when the
warehouse is full. The consumer should stop
consuming when the warehouse is empty
Retrieving databases

For example, we would initialize a semaphore to the
number of database connections available. As each
thread acquires the semaphore, the number of
available connections is decremented by one. Upon
consumption of the resource, the semaphore is
released, incrementing the counter .
Language Support



PL/I and ALGOL 68 are the only
languages that support semaphore
ALGOL 68 has a data type name sema
Java has no built in semaphore
mechanism. Although, it is possible to
construct one. (hand out)
Famous quote from Per Brinch
Hansen

“The semaphore is an elegant
synchronization tool for an ideal
programmer who never makes
mistakes.” Unfortunately, programmers
of that kind are rare.[1]
[1] Concepts of Programming Language,
Robert W. Sebesta, Addison Wesley,
2002 pg. 528


http://www.informatik.unistuttgart.de/ipvr/bv/cppvm/onlinedoc/node53.html
http://www.javaworld.com/javaworld/ja
vaqa/1999-11/02-qa-semaphore.html