6-Semaphores - COMP 530 Introduction to Operating Systems...

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

View Full Document Right Arrow Icon
1 ©2011 by Kevin Jeffay COMP 530 Introduction to Operating Systems Semaphores Kevin Jeffay Department of Computer Science University of North Carolina at Chapel Hill jeffay@cs.unc.edu September 21, 2011 http://www.cs.unc.edu/~jeffay/courses/comp530 2 ©2011 by Kevin Jeffay Lecture 6: Semaphores Outline and key concepts ± Semaphore abstract data type ± Mutual exclusion versus condition synchronization ± Binary semaphores ± General / counting semaphores ± Semaphore implementations with busy-waiting ± Semaphore implementations with an OS kernel ± Readings: » Chapter 6 (Process Synchronization)
Background image of page 1

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

View Full DocumentRight Arrow Icon
3 ©2011 by Kevin Jeffay Process Coordination Problems with mutual exclusion algorithms ± Mutual exclusion algorithms are complex and brittle ± All the algorithms we’ve seen so far employ busy waiting process P i begin loop inCS [i] := TRUE turn := i while turn = i AND inCS [j] do NOOP end while <critical section> inCS [i] := FALSE end loop end P i 4 ©2011 by Kevin Jeffay procedure down ( sem ) begin if (sem = 0) then < wait until sem > 0 > endif sem -= 1 end down Semaphores A higher-level synchronization primitive ± An abstract data type » down() ² If sem > 0, then decrement sem by 1 Otherwise “wait” until sem > 0 and then decrement object semaphore exports down , up private sem : int procedure down() procedure up() end semaphore ± Both operations are assumed to be atomic ± A non-negative integer variable with two operations: » up() ² Increment sem by 1 procedure up () begin sem += 1 end up () (Also often called “ P() ”, “ wait() ”, . ..) (Also often called “ V() ”, “ signal() ”, . ..)
Background image of page 2
5 ©2011 by Kevin Jeffay Using Semaphores Solving the critical section problem ± Use a binary semaphore for mutual exclusion » The semaphore’s value is only ever 0 or 1 process P 1 begin mutex.down() < critical section > mutex.up() end P 1 var mutex : binary_semaphore := 1 process P 2 begin mutex.down() < critical section > mutex.up() end P 2 private sem : int procedure down () begin if (sem = 0) then < wait until sem > 0 > endif sem := 0 end down procedure up () begin sem := 1 end up 6 ©2011 by Kevin Jeffay Using Semaphores For Mutual Exclusion Producer/Consumer synchronization process Producer begin loop < produce a character “c” > while count = n do NOOP end while buf [ nextIn ] := c nextIn := nextIn + 1 mod n count := count + 1 end loop end Producer process Consumer begin loop while count = 0 do NOOP end while data := buf [ nextOut ] nextOut := nextOut +1 mod n count := count - 1 < consume “data” > end loop end Consumer globals buf : array [0. . n -1] of char nextIn , nextOut : 0. . n -1 := 0 count : 0. . n := 0 mutex.down() mutex.down() mutex.up() mutex.up() mutex : binary_semaphore := 1
Background image of page 3

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

View Full DocumentRight Arrow Icon
7 ©2011 by Kevin Jeffay Shared Memory Process Coordination Condition Synchronization process Producer begin loop < produce a character “c” > while count = n do NOOP end while buf [ nextIn ] := input nextIn := nextIn + 1 mod n mutex . down() count := count + 1 mutex.up() end loop end Producer process Consumer begin loop while count = 0 do NOOP end while data := buf [ nextOut ] nextOut := nextOut +1 mod n mutex.down() count := count - 1 mutex.up() < consume “data” > end loop end Consumer ±
Background image of page 4
Image of page 5
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 University (Engineering School).

Page1 / 14

6-Semaphores - COMP 530 Introduction to Operating Systems...

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

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