1 / 13

SEMAPHORE

SEMAPHORE. By: Wilson Lee. Concurrency Task Synchronization Example of semaphore Language Support. Concurrency. First is instruction level executing two or more machine instructions simultaneously usually handled by optimizing compiler Second is statement level

smithandrea
Download Presentation

SEMAPHORE

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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) • ifaSemaphore’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) • ifaSemaphore’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

  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/cppvm/online-doc/node53.htmlhttp://www.informatik.uni-stuttgart.de/ipvr/bv/cppvm/online-doc/node53.html • http://www.javaworld.com/javaworld/javaqa/1999-11/02-qa-semaphore.html

More Related