Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 7: Synchronization Examples

Similar presentations


Presentation on theme: "Chapter 7: Synchronization Examples"— Presentation transcript:

1 Chapter 7: Synchronization Examples

2 Chapter 7: Synchronization Examples
Classic Problems of Synchronization Synchronization within the Kernel POSIX Synchronization Synchronization in Java Alternative Approaches

3 Classical Problems of Synchronization
Classical problems used to test newly-proposed synchronization schemes Bounded-Buffer Problem Readers and Writers Problem Dining-Philosophers Problem

4 Bounded-Buffer Problem
Correctness constraints for solution Consumer must wait for producer to fill buffers, if none full (scheduling constraint) Producer must wait for consumer to empty buffers, if all full (scheduling constraint) Only one thread can manipulate buffer queue at a time (mutual exclusion) General rule of thumb: Use a separate semaphore for each constraint. Note how semaphores are being used in multiple ways. n buffers, each can hold one item Semaphore mutex = 1 // mutual exclusion Semaphore full = // consumer’s constraint, if 0, no item in buffer Semaphore empty = n // producer’s constraint, if full, nowhere to put item

5 Bounded Buffer Problem (Cont.)
The structure of the producer process do { ... /* produce an item in next_produced */ wait(empty); wait(mutex); ... /* add next produced to the buffer */ signal(mutex); signal(full); } while (true); Consumer process Do { wait(full); wait(mutex); ... /* remove an item from buffer to next_consumed */ ... signal(mutex); signal(empty); ... /* consume the item in next consumed */ } while (true);

6 Semaphore solution Questions
Why does producer wait() + signal() different semaphores than the consumer? Is order of wait()’s important? Yes! Can cause deadlock Is order of signal()’s important? No, except that it might affect scheduling efficiency What if we have 2 producers or 2 consumers? Do we need to change anything?

7 Readers-Writers Problem
A data set is shared among a number of concurrent Threads Readers – only read the data set concurrently; they do not perform any updates Writers – can both read and write (update) concurrently Problem – allow multiple readers to read at the same time Only one single writer can access the shared data at the same time Several variations of how readers and writers are considered – all involve some form of priorities


Download ppt "Chapter 7: Synchronization Examples"

Similar presentations


Ads by Google