SEMAPHORE - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

SEMAPHORE

Description:

It was devised in 1965 by Edsger Dijkstra to provide competition synchronization ... Originally named P and V by Dijkstra, after two Dutch words passeren (to pass) ... – PowerPoint PPT presentation

Number of Views:49
Avg rating:3.0/5.0
Slides: 14
Provided by: darmawanl
Learn more at: http://www1.chapman.edu
Category:
Tags: semaphore | by

less

Transcript and Presenter's Notes

Title: SEMAPHORE


1
SEMAPHORE
By Wilson Lee
2
  • Concurrency
  • Task
  • Synchronization
  • Example of semaphore
  • Language Support

3
Concurrency
  • First is instruction level
  • executing two or more machine instructions
    simultaneously
  • usually handled by optimizing compiler
  • Second is statement level
  • executing two or more statement instructions
    simultaneously
  • Third is unit level
  • 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

4
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

5
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.

6
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

7
Synchronization alternatives
  • 1. Semaphores
  • 2. Monitors
  • 3. Message Passing

8
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).

9
Two semaphore operations
  • wait (aSemaphore)
  • if aSemaphores counter gt 0 then
  • decrement aSemaphores counter
  • else
  • put the caller in aSemaphores queue
  • attempt to transfer control to some ready task
  • (if the task ready queue is empty, deadlock
    occurs)
  • End
  • release(aSemaphore)
  • if aSemaphores queue is empty (no task is
    waiting) then
  • increment aSemaphores counter
  • else
  • put the calling task in the task ready queue
  • transfer control to a task from aSemaphores
    queue
  • end

10
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 .

11
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)

12
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

13
  • http//www.informatik.uni-stuttgart.de/ipvr/bv/cpp
    vm/online-doc/node53.html
  • http//www.javaworld.com/javaworld/javaqa/1999-11/
    02-qa-semaphore.html
Write a Comment
User Comments (0)
About PowerShow.com