CSI 400/500 Operating Systems Spring 2009
Lecture #15 – Synchronization with Semaphores
Monday, March 30th, 2009
2
Definitions
• 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
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
More definitions
• 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
9
Parts of a Semaphore
• 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
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