1 / 17

CSI 400/500 Operating Systems Spring 2009

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

kizzy
Download Presentation

CSI 400/500 Operating Systems Spring 2009

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSI 400/500 Operating Systems Spring 2009 Lecture #15 – Synchronization with Semaphores Monday, March 30th, 2009

  2. 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

  3. 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

  4. Simple solution • Read/write flags • Only one process can update a record at a time • Multiple processes can read • Does this help our case?

  5. 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

  6. 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

  7. Locks v Kernels • Some systems create subKernels around critical sections to handle reserving and accessing shared data • Not preferred – why?

  8. 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

  9. 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

  10. Types of Semaphores • Binary • Counting

  11. How Binary Semaphore Works • Value starts at 1 • Hold checks value: • If 0, waits • Once 1, decrements • Release increments value

  12. 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

  13. 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?

  14. 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?

  15. 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

  16. 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

  17. 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

More Related