lec05-readerwriter

lec05-readerwriter - CS162 Operating Systems and Systems...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: 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.4 2/1/12 Anthony D. Joseph and Ion Stoica CS162 ©UCB Spring 2012 Recap: Locks int value = 0; Acquire() { // Short busy-wait time disable interrupts; if ( value == 1 ) { put thread on wait-queue; go to sleep() //?? } else { value = 1; enable interrupts; } } Release() { // Short busy-wait time disable interrupts; if anyone on wait queue { take thread off wait-queue Place on ready queue; } else { value = 0; } enable interrupts; } lock.Acquire(); … critical section; … lock.Release(); Acquire() { disable interrupts; } Release() { enable interrupts; } If one thread in critical section, no other activity (including OS) can run! Lec 5.5 2/1/12 Anthony D. Joseph and Ion Stoica CS162 ©UCB Spring 2012 Recap: Locks int guard = 0; int value = 0; Acquire() { // Short busy-wait time while(test&set(guard)); if ( value == 1 ) { put thread on wait-queue; go to sleep()& guard = 0; } else { value = 1; guard = 0; } } Release() { // Short busy-wait time while (test&set(guard)); if anyone on wait queue { take thread off wait-queue Place on ready queue; } else { value = 0; } guard = 0; } lock.Acquire(); … critical section; … lock.Release(); int value = 0; Acquire() { while(test&set(value)); } Release() { value = 0; } Threads waiting to enter critical section busy-wait 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 defined by Dijkstra in late 60s – Main synchronization primitive used in original UNIX • Definition: 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 Lec 5.7Lec 5....
View Full Document

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

Page1 / 71

lec05-readerwriter - CS162 Operating Systems and Systems...

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

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