csi 400/500 operating systems spring 2009
DESCRIPTION
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 - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: CSI 400/500 Operating Systems Spring 2009](https://reader036.vdocuments.site/reader036/viewer/2022062323/5681522b550346895dc074d9/html5/thumbnails/1.jpg)
CSI 400/500 Operating Systems Spring 2009
Lecture #15 – Synchronization with Semaphores
Monday, March 30th, 2009
![Page 2: CSI 400/500 Operating Systems Spring 2009](https://reader036.vdocuments.site/reader036/viewer/2022062323/5681522b550346895dc074d9/html5/thumbnails/2.jpg)
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
![Page 3: CSI 400/500 Operating Systems Spring 2009](https://reader036.vdocuments.site/reader036/viewer/2022062323/5681522b550346895dc074d9/html5/thumbnails/3.jpg)
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
![Page 4: CSI 400/500 Operating Systems Spring 2009](https://reader036.vdocuments.site/reader036/viewer/2022062323/5681522b550346895dc074d9/html5/thumbnails/4.jpg)
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?
![Page 5: CSI 400/500 Operating Systems Spring 2009](https://reader036.vdocuments.site/reader036/viewer/2022062323/5681522b550346895dc074d9/html5/thumbnails/5.jpg)
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
![Page 6: CSI 400/500 Operating Systems Spring 2009](https://reader036.vdocuments.site/reader036/viewer/2022062323/5681522b550346895dc074d9/html5/thumbnails/6.jpg)
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
![Page 7: CSI 400/500 Operating Systems Spring 2009](https://reader036.vdocuments.site/reader036/viewer/2022062323/5681522b550346895dc074d9/html5/thumbnails/7.jpg)
7
Locks v Kernels
• Some systems create subKernels around critical sections to handle reserving and accessing shared data
• Not preferred – why?
![Page 8: CSI 400/500 Operating Systems Spring 2009](https://reader036.vdocuments.site/reader036/viewer/2022062323/5681522b550346895dc074d9/html5/thumbnails/8.jpg)
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
![Page 9: CSI 400/500 Operating Systems Spring 2009](https://reader036.vdocuments.site/reader036/viewer/2022062323/5681522b550346895dc074d9/html5/thumbnails/9.jpg)
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
![Page 10: CSI 400/500 Operating Systems Spring 2009](https://reader036.vdocuments.site/reader036/viewer/2022062323/5681522b550346895dc074d9/html5/thumbnails/10.jpg)
10
Types of Semaphores
• Binary
• Counting
![Page 11: CSI 400/500 Operating Systems Spring 2009](https://reader036.vdocuments.site/reader036/viewer/2022062323/5681522b550346895dc074d9/html5/thumbnails/11.jpg)
11
How Binary Semaphore Works
• Value starts at 1
• Hold checks value:• If 0, waits
• Once 1, decrements
• Release increments value
![Page 12: CSI 400/500 Operating Systems Spring 2009](https://reader036.vdocuments.site/reader036/viewer/2022062323/5681522b550346895dc074d9/html5/thumbnails/12.jpg)
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
![Page 13: CSI 400/500 Operating Systems Spring 2009](https://reader036.vdocuments.site/reader036/viewer/2022062323/5681522b550346895dc074d9/html5/thumbnails/13.jpg)
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?
![Page 14: CSI 400/500 Operating Systems Spring 2009](https://reader036.vdocuments.site/reader036/viewer/2022062323/5681522b550346895dc074d9/html5/thumbnails/14.jpg)
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?
![Page 15: CSI 400/500 Operating Systems Spring 2009](https://reader036.vdocuments.site/reader036/viewer/2022062323/5681522b550346895dc074d9/html5/thumbnails/15.jpg)
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
![Page 16: CSI 400/500 Operating Systems Spring 2009](https://reader036.vdocuments.site/reader036/viewer/2022062323/5681522b550346895dc074d9/html5/thumbnails/16.jpg)
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
![Page 17: CSI 400/500 Operating Systems Spring 2009](https://reader036.vdocuments.site/reader036/viewer/2022062323/5681522b550346895dc074d9/html5/thumbnails/17.jpg)
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