Lec05-readerwriterx4 - Goals for Today CS162 Operating Systems and Systems Programming Lecture 5 Semaphores Conditional Variables February 1 2012

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

View Full Document Right Arrow Icon
Page 1 CS162 Operating Systems and Systems Programming Lecture 5 Semaphores, Conditional Variables February 1, 2012 Anthony D. Joseph and Ion Stoica http://inst.eecs.berkeley.edu/~cs162 Lec 5.2 2/1/12 Anthony D. Joseph and Ion Stoica CS162 ©UCB Spring 2012 Goals for Today • Continue with Synchronization Abstractions – Semaphores, Monitors and condition variables • Readers-Writers problem and solution Note: Some slides and/or pictures in the following are adapted from slides ©2005 Silberschatz, Galvin, and Gagne. Many slides generated from lecture notes by Kubiatowicz. Lec 5.3 2/1/12 Anthony D. Joseph and Ion Stoica CS162 ©UCB Spring 2012 Where are we going with synchronization? • We are going to implement various higher-level synchronization primitives using atomic operations – Everything is pretty painful if only atomic primitives are load and store – Need to provide primitives useful at user-level Load/Store Disable Ints Test&Set Comp&Swap Locks Semaphores Monitors Send/Receive Shared Programs Hardware Higher- level API Programs Lec 5.6 2/1/12 Anthony D. Joseph and Ion Stoica CS162 ©UCB Spring 2012 Semaphores • Semaphores are a kind of generalized locks – First de±ned by Dijkstra in late 60s – Main synchronization primitive used in original UNIX • De±nition: a Semaphore has a non-negative integer value and supports the following two operations: – P(): an atomic operation that waits for semaphore to become positive, then decrements it by 1 » Think of this as the wait() operation – V(): an atomic operation that increments the semaphore by 1, waking up a waiting P, if any » This of this as the signal() operation – Note that P() stands for “ proberen” (to test) and V() stands for verhogen” (to increment) in Dutch
Background image of page 1

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

View Full DocumentRight Arrow Icon
Page 2 Lec 5.7 2/1/12 Anthony D. Joseph and Ion Stoica CS162 ©UCB Spring 2012 Semaphores Like Integers Except • Semaphores are like integers, except – No negative values – Only operations allowed are P and V – can ʼ t read or write value, except to set it initially – Operations must be atomic » Two P ʼ s together can ʼ t decrement value below zero » Similarly, thread going to sleep in P won ʼ t miss wakeup from V – even if they both happen at same time • Semaphore from railway analogy – Here is a semaphore initialized to 2 for resource control: Value=2 Lec 5.8 2/1/12 Anthony D. Joseph and Ion Stoica CS162 ©UCB Spring 2012 Two Uses of Semaphores • Mutual Exclusion (initial value = 1) – Also called “Binary Semaphore”. – Can be used for mutual exclusion: semaphore.P(); // Critical section goes here semaphore.V(); • Scheduling Constraints (initial value = 0) – Locks are Fne for mutual exclusion, but what if you want a thread to wait for something? – Example: suppose you had to implement ThreadJoin which must wait for thread to terminiate: Initial value of semaphore = 0 ThreadJoin { semaphore.P(); } ThreadFinish { semaphore.V(); } Lec 5.9 2/1/12 Anthony D. Joseph and Ion Stoica CS162 ©UCB Spring 2012 Producer-consumer with a bounded buffer • Problem DeFnition
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/12/2012 for the course CS 162 taught by Professor Kubiatowicz during the Spring '02 term at University of California, Berkeley.

Page1 / 18

Lec05-readerwriterx4 - Goals for Today CS162 Operating Systems and Systems Programming Lecture 5 Semaphores Conditional Variables February 1 2012

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

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