170 likes | 272 Views
CSI 400/500 Operating Systems Spring 2009. Lecture #15 – Synchronization with Semaphores Monday, March 30 th , 2009. Concurrency Synchronization Critical Section. Running multiple processes at the same time
E N D
CSI 400/500 Operating Systems Spring 2009 Lecture #15 – Synchronization with Semaphores Monday, March 30th, 2009
Concurrency Synchronization Critical Section Running multiple processes at the same time Timing use of resources so processes appear to use them at same time (virtually), but don’t interfere with each other Part of process where accesses shared information Definitions
Why important? • Consider payroll database • Raises and vacation pay process runs Friday night • Paychecks run Friday night • Difference if paycheck process checks pay rate of employee before and after raise processed
Simple solution • Read/write flags • Only one process can update a record at a time • Multiple processes can read • Does this help our case?
Next solution: access lock • Before an update, process obtains lock • No other process can access (either read or write) while lock is held • Does this completely solve our situation? • Downside
Deadlock • SERIOUS downside of using locks • Two processes: one holds lock on resource that other needs • Each holds until needed lock released • Since both suspended, locks never released and processes never run • We’ll discuss ways to detect and prevent deadlock in Lecture 17
Locks v Kernels • Some systems create subKernels around critical sections to handle reserving and accessing shared data • Not preferred – why?
Mutual Exclusivity Semaphore Only one process in a shared critical section at a time; others wait Data type that controls singular access to a shared resource More definitions
Value Hold or Set or Wait Release or Signal Value of the semaphore (depends upon type) Request for resource Called P in text Suspends process until resource is available Done with resource Called V in text Parts of a Semaphore
Types of Semaphores • Binary • Counting
How Binary Semaphore Works • Value starts at 1 • Hold checks value: • If 0, waits • Once 1, decrements • Release increments value
How Counting Semaphore Works • Value starts at appropriate value • 0 for empty source, limit for full • Set checks value: • If 0, waits • Once 1, decrements • Release increments value
Semaphore Example #1 – Consumer/Producer Problem • Like shared data buffers, where any process adds data to it, reads data, or removes data • Slightly more complex than classic consumer/producer, as there is a non-destructive consume option (read) • Can’t read or remove if nothing there • Action of removing or adding requires exclusive access • How do this?
Semaphore Example #2 – Readers/Writer Problem • Multiple readers can access same resource, but only one writer at a time • Writer has exclusive access • How handle this?
Semaphore Considerations • To avoid potential deadlock situations by indefinite semaphore hold, interrupts temporarily disabled within Set and Signal routines • Modern OS uses assembly instruction TS (test and set) to accommodate
Handling Semaphore Wait • Within Set routine • Most Operating systems put process in Suspend state, freeing processor for other work • Signal causes interrupt that Readies all processes suspended on that semaphore • First one scheduled executes
Active Release • Some just-in-time operating systems use active Release routine • Informs suspended process that semaphore is now available • Only applies to binary semaphores