06_synchronization

06_synchronization - Chapter 6: Synchronization Chapter...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 6: Synchronization Chapter 6: Synchronization
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6.2 Modified by Bo Li ©2009 Operating System Concepts Module 6: Synchronization Module 6: Synchronization ± Background ± The Critical-Section Problem ± Too much milk problem ± Peterson’s Solution ± Synchronization Hardware (lock) ± Semaphores ± Classic Problems of Synchronization ± Monitors
Background image of page 2
6.3 Modified by Bo Li ©2009 Operating System Concepts Independent vs. Cooperating Threads Independent vs. Cooperating Threads ± Recall If dispatcher can schedule threads in any way, programs must work under all circumstances ± Independent Threads: z No state shared with other threads z Deterministic Input state determines results z Reproducible Can recreate Starting Conditions, I/O z Scheduling order doesn’t matter ± Cooperating Threads: z Shared State between multiple threads z Non-deterministic z Non-reproducible z Non-deterministic and Non-reproducible means that bugs can be intermittent
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6.4 Modified by Bo Li ©2009 Operating System Concepts Why Cooperating Processes (Threads)? Why Cooperating Processes (Threads)? ± Advantage 1: Share resources z One computer, many users z One bank balance, many ATMs ± What if ATMs were only updated at night? z Embedded systems (robot control: coordinate arm & hand) ± Advantage 2: Speedup z Overlap I/O and computation ± Many different file systems do read-ahead z Multiprocessors – chop up program into parallel pieces ± Advantage 3: Modularity z More important than you might think z Chop large problem up into simpler pieces ± To compile, for instance, gcc calls cpp | cc1 | cc2 | as | ld ± Makes system easier to extend
Background image of page 4
6.5 Modified by Bo Li ©2009 Operating System Concepts Problem is at the lowest level Problem is at the lowest level ± Most of the time, threads are working on separate data, so scheduling doesn’t matter: Thread A Thread B x = 1; y = 2; ± However, What about (Initially, y = 12): Thread A Thread B x = 1; y = 2; x = y+1; y = y*2; z What are the possible values of x? ± Or, what are the possible values of x below? Thread A Thread B x = 1; x = 2; z X could be 1 or 2 (non-deterministic!)
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6.6 Modified by Bo Li ©2009 Operating System Concepts Example Example ± Cooperating, or concurrent access to shared data may result in data inconsistency ± Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes ± Suppose that we wanted to provide a solution to the consumer- producer problem that fills all the buffers. z We can do so by having an integer count that keeps track of the number of occupied buffers. z Initially, count is set to 0. z incremented by the producer after it produces a new buffer z decremented by the consumer after it consumes a buffer z Does this work?
Background image of page 6
6.7 Modified by Bo Li ©2009 Operating System Concepts Producer and Consumer Producer and Consumer while (true) { /* produce an item and put in nextProduced */ while (count == BUFFER_SIZE) ; // do nothing buffer [in] = nextProduced; in = (in + 1) % BUFFER_SIZE; count++; } while (true) { while (count == 0) ; // do nothing nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; count--; /* consume the item in nextConsumed }
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6.8
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 40

06_synchronization - Chapter 6: Synchronization Chapter...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online