04_semaphores - Chair of Software Engineering Concepts of...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
Chair of Software Engineering Concepts of Concurrent Computation Bertrand Meyer Sebastian Nanz Lecture 4: Semaphores
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Today's lecture In this lecture you will learn about: the type of semaphores, an important synchronization primitive, implementation variants of semaphores, in particular weak and strong semaphores, uses of semaphores, in particular solutions to problems involving mutual exclusion, condition synchronization (the producer-consumer problem), and barriers.
Background image of page 2
Chair of Software Engineering The type of semaphores
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 The need for a new synchronization primitive The synchronization algorithms can provide process synchronization using atomic read and write only As a low-level synchronization primitive, they also have a number of disadvantages they rely on busy waiting (inefficient for multitasking) their synchronization variables are freely accessible within the program (no encapsulation) they can become very complex (difficult to implement)
Background image of page 4
5 Semaphores Semaphores: a higher-level synchronization primitive (not really high-level though) that alleviates some of the problems of synchronization algorithms A very important primitive, widely implemented and with many uses This comes at a price: the implementation of semaphores needs stronger atomic operations Invented by E.W. Dijkstra in 1965 In other contexts, "semaphore" means traffic signal, e.g. to keep rail tracks free in railroad traffic control
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 General semaphores A general semaphore is an object that consists of a variable count and two operations down and up : if a process calls down where count > 0, then count is decremented; otherwise the process waits until count is positive. if a process calls up then count is incremented. Atomicity requirements: testing and decrementing, as well as incrementing have to be atomic A general semaphore is sometimes also called a counting semaphore Value of a semaphore : value of its count variable
Background image of page 6
7 Simple implementation of a general semaphore class SEMAPHORE feature count : INTEGER down do await count > 0 count := count 1 end up do count := count + 1 end end
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 Comments on the simple implementation We have used the await statement: this is busy waiting – we'll get rid of it in more refined implementations We use object-oriented / Eiffel-like syntax, but in pseudo-code style We will also write for a semaphore s s.count -- value of variable count of s s.down , s.up -- calls to routines of s Of course, when semaphores were invented, object- orientation was not yet around
Background image of page 8
Mutual exclusion for two processes (1) Providing mutual exclusion with semaphores: initialize s.count to 1, and enclose the critical section as follows s.down critical section s.up Presented in the style of the mutual exclusion problem: count := 1 P1 P2 1 2 3 4 while true loop await count > 0 count := count 1 critical section count := count + 1 non-critical section end 1
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/18/2012 for the course INFORMATIK 2011 taught by Professor Phanthuongcang during the Winter '11 term at Cornell.

Page1 / 42

04_semaphores - Chair of Software Engineering Concepts of...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online