Class Note for CMPSCI 377 at UMass(44)
Class Note for CMPSCI 377 at UMass(44)
Popular in Course
Popular in Department
This 3 page Class Notes was uploaded by an elite notetaker on Friday February 6, 2015. The Class Notes belongs to a course at University of Massachusetts taught by a professor in Fall. Since its upload, it has received 11 views.
Reviews for Class Note for CMPSCI 377 at UMass(44)
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 02/06/15
CMPSCI 377 Operating Systems Fall 2005 Lecture 9 October 13 Lecturer Emery Berger Scribes Billy Dean and Zeid Rustm Last time Locks7 Semaphores7 and Busy Waiting7 etc Today we re going to learn about more synchronization primitives 0 ReadWrite Locks 0 Monitors 0 Condition Variables 91 More Synchronization Primitives 911 ReadWrite Locks The problem with readers and writers is that they are a group of objects that cannot run simultaneously This can be solved using single locks7 but that does not allow groups of reads at once Another way to approach this is to have two types of locks Reader locks and Writer locks How to build ReaderWriter blocks 0 As long as there are no writers7 let a reader in 0 Writers queue up 0 When all readers are done7 allow one writer readers queue up when that writer is executed WARNING This can starve the writers Two solutions to this 1 Waiting writer always gets served 2 No reader waits unless writer is already being run Good technique is to switch between those two variant modes to ensure equalty 92 Using Semaphores You can implement these locks with semaphores One is a ReaderSemaphore7 the other is a WriterSemaphore o readerSeaphore protects number of readers allowed 91 92 Lecture 9 October 13 o writerSemaphore controls scheduling of writers allows one in at a time See the rst Java Code example in the Lecture Slides for more details Basically the code behaves in the following way If there are no readers then the writerSemaphore allows a writer to execute A readerSemaphore increments the number of readers when it is run If the reader is the rst one then it blocks the writers If the reader is the last one then the writers are unblocked after it completes Writers wait for an empty slot then write they wait for readers and other writers to complete 921 Problems With Semaphores and Locks They do two things 1 Mutual exclusion 2 Scheduling But those require shared global variables The program can be broken its not structured and it does not have any relationship with its protective data We want a synchronization operating that does not suffer from this 93 Monitors Monitors tie mutal exclusion to a particular set of objects Monitors are similar to Java classes in that all their data is private and all their methods are synchronized We de ne the Java keyword 7synchronize7 as more than just a lock it s a recursive lock that a thread can keep reusing It contains a thread ID the lock itself and a count to keep track of the number of recursive calls used to know when to release the lock 931 Problems With Monitors What to do with when the queue is empty 0 Return special error value null 0 Throw an exception 0 Wait for something to appear in queue go to sleep for now However sleep is inside synchronize so it will hold the lock when it sleeps The solution to this problem is to use Condtion Variables Lecture 9 October 13 94 Conditon Variables 0 Has queue of threads waiting in critical section Thread must hold back when performing operations method waitlock l atomically releases lock7 then goes to sleep 0 reaquires lock when awakened method notify wakes up one waiting See the RW lock Java Code example in the Lecture Sldes for more details 95 Summary Beni ts of ReaderWriter locks 0 Permit concurrent reads 0 lmplementable with Semaphores Beni ts of Monitors 0 Tie data and methods with synchronization Beni ts of Condition Variables 0 They release the lock temporarily7 waiting inside critical sections For more information see Doug Lea s book Concurrent Programming in Java77 93
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'