CS330 · Chapter 6 · Annotated SlidesProcess Synchronization
Original
Overview

Process Synchronization

Race conditions, the critical-section problem and its three requirements, Peterson’s solution, hardware support (test_and_set, swap), semaphores and their implementation, deadlock and starvation, the bounded-buffer, readers–writers and dining-philosophers problems, and monitors.

How to read this page

Every original slide is shown unchanged. Yellow marks on a slide point to a numbered note: hover or tap a mark to jump to its note, and hover a note to see its marks. Questions tagged Past exam appeared on a real paper; Prediction questions are likely angles, not confirmed. Pick an answer to each question: a wrong pick tells you why it is wrong, so you can try again.

All 45 slides
  1. 1 Slide 1
  2. 2 Outline of this lecture
  3. 3 Objectives
  4. 4 Background
  5. 5 Producer
  6. 6 Race condition example
  7. 7 Race Condition
  8. 8 Race condition
  9. 9 The Critical Section (CS) Problem
  10. 10 Critical Section
  11. 11 Solution to Critical-Section Problem
  12. 12 Types of Solutions
  13. 13 Peterson ’ s Solution
  14. 14 Algorithm for Process P
  15. 15 Drawbacks of Software Solutions
  16. 16 Synchronization Hardware
  17. 17 test_and_set Instruction
  18. 18 Solution using test_and_set
  19. 19 swap Instruction
  20. 20 Solution using swap
  21. 21 Semaphore
  22. 22 Semaphore Usage
  23. 23 Semaphore Implementation
  24. 24 Semaphore Implementation with no Busy waiting
  25. 25 Semaphore Implementation with no Busy waiting (Cont.)
  26. 26 Deadlock and Starvation
  27. 27 Classical Problems of Synchronization
  28. 28 Bounded-Buffer Problem
  29. 29 Bounded Buffer Problem (Cont.)
  30. 30 Bounded Buffer Problem (Cont.)
  31. 31 Readers-Writers Problem
  32. 32 Readers-Writers Problem (Cont.)
  33. 33 Readers-Writers Problem (Cont.)
  34. 34 Dining-Philosophers Problem
  35. 35 Dining-Philosophers Problem (Cont.)
  36. 36 Problems with Semaphores
  37. 37 Operating System Solutions: Monitors
  38. 38 Operating System Solutions: Monitors
  39. 39 Operating System Solutions: Monitors
  40. 40 Variables
  41. 41 Monitor with Condition Variables
  42. 42 Condition Variables choices
  43. 43 Solution to Dining Philosophers
  44. 44 Solution to Dining Philosophers (cont)
  45. 45 Process Synchronization Conclusion
Slide 1 / 45

Slide 1

Slide 1: Slide 1
Slide 2 / 45

Outline of this lecture

Slide 2: Outline of this lecture
Slide 3 / 45

Objectives

Slide 3: Objectives
Slide 4 / 45

Background

Slide 4: Background
Slide 5 / 45

Producer

Slide 5: Producer
Slide 6 / 45

Race condition example

Slide 6: Race condition example

Notes

  1. count++ is load–increment–store; interleaving these steps from two processes can lose an update.

Slide 7 / 45

Race Condition

Slide 7: Race Condition

Notes

  1. Race condition: the result depends on the order in which processes access shared data.

Exam angle

PredictionQ1

A situation where the outcome depends on the order of access to shared data is a:

Slide 8 / 45

Race condition

Slide 8: Race condition
Slide 9 / 45

The Critical Section (CS) Problem

Slide 9: The Critical Section (CS) Problem
Slide 10 / 45

Critical Section

Slide 10: Critical Section

Notes

  1. Critical section: the code that touches shared data. Structure: entry section, critical section, exit section, remainder section.

Slide 11 / 45

Solution to Critical-Section Problem

Slide 11: Solution to Critical-Section Problem

Notes

  1. Requirements: Mutual exclusion (one at a time), Progress (no needless waiting when the CS is free), Bounded waiting (no starvation).

  2. Memory trick M-P-B: Mutual exclusion, Progress, Bounded waiting.

Exam angle

PredictionQ1

Which is NOT a requirement of a critical-section solution?

Slide 12 / 45

Types of Solutions

Slide 12: Types of Solutions
Slide 13 / 45

Peterson ’ s Solution

Slide 13: Peterson ’ s Solution

Notes

  1. Peterson’s solution (2 processes): shared turn and flag[2]. Pᵢ sets flag[i] = true, turn = j, then waits while flag[j] && turn == j.

Slide 14 / 45

Algorithm for Process P

Slide 14: Algorithm for Process P
Slide 15 / 45

Drawbacks of Software Solutions

Slide 15: Drawbacks of Software Solutions
Slide 16 / 45

Synchronization Hardware

Slide 16: Synchronization Hardware

Notes

  1. Hardware support: atomic (uninterruptible) instructions such as test_and_set and compare_and_swap.

Slide 17 / 45

test_and_set Instruction

Slide 17: test_and_set Instruction

Notes

  1. test_and_set returns the old value and sets the lock to true, in one atomic step.

Slide 18 / 45

Solution using test_and_set

Slide 18: Solution using test_and_set
Slide 19 / 45

swap Instruction

Slide 19: swap Instruction
Slide 20 / 45

Solution using swap

Slide 20: Solution using swap
Slide 21 / 45

Semaphore

Slide 21: Semaphore

Notes

  1. Semaphore S: integer accessed only by wait() (P: decrement, block if < 0) and signal() (V: increment, wake one).

Exam angle

PredictionQ1

wait(S) on a semaphore:

Slide 22 / 45

Semaphore Usage

Slide 22: Semaphore Usage

Notes

  1. Binary semaphore (0/1) = mutex lock. Counting semaphore controls access to N instances; initialize it to N.

Exam angle

PredictionQ1

A counting semaphore guarding 5 identical printers should be initialized to:

Slide 23 / 45

Semaphore Implementation

Slide 23: Semaphore Implementation

Notes

  1. Busy waiting (spinlock) wastes CPU; better implementation blocks the process on the semaphore’s waiting list.

Slide 24 / 45

Semaphore Implementation with no Busy waiting

Slide 24: Semaphore Implementation with no Busy waiting
Slide 25 / 45

Semaphore Implementation with no Busy waiting (Cont.)

Slide 25: Semaphore Implementation with no Busy waiting (Cont.)
Slide 26 / 45

Deadlock and Starvation

Slide 26: Deadlock and Starvation

Exam angle

PredictionQ1

P0 runs wait(S); wait(Q); and P1 runs wait(Q); wait(S); with S = Q = 1. What can happen?

Compare with the model answer

Deadlock: P0 gets S, P1 gets Q, then each waits forever for the semaphore the other holds.

Slide 27 / 45

Classical Problems of Synchronization

Slide 27: Classical Problems of Synchronization
Slide 28 / 45

Bounded-Buffer Problem

Slide 28: Bounded-Buffer Problem

Notes

  1. Bounded buffer: mutex = 1, full = 0, empty = n. Producer: wait(empty); wait(mutex); … signal(mutex); signal(full).

  2. Trap Swapping the order to wait(mutex); wait(empty) can deadlock when the buffer is full.

Exam angle

PredictionQ1

In the bounded-buffer solution with n slots, empty starts at:

Slide 29 / 45

Bounded Buffer Problem (Cont.)

Slide 29: Bounded Buffer Problem (Cont.)
Slide 30 / 45

Bounded Buffer Problem (Cont.)

Slide 30: Bounded Buffer Problem (Cont.)
Slide 31 / 45

Readers-Writers Problem

Slide 31: Readers-Writers Problem
Slide 32 / 45

Readers-Writers Problem (Cont.)

Slide 32: Readers-Writers Problem (Cont.)
Slide 33 / 45

Readers-Writers Problem (Cont.)

Slide 33: Readers-Writers Problem (Cont.)
Slide 34 / 45

Dining-Philosophers Problem

Slide 34: Dining-Philosophers Problem
Slide 35 / 45

Dining-Philosophers Problem (Cont.)

Slide 35: Dining-Philosophers Problem (Cont.)

Exam angle

PredictionQ1

Which avoids deadlock in dining philosophers?

Slide 36 / 45

Problems with Semaphores

Slide 36: Problems with Semaphores

Notes

  1. Semaphore misuse: signal before wait, wait twice, or forgetting one → broken mutual exclusion or deadlock.

Slide 37 / 45

Operating System Solutions: Monitors

Slide 37: Operating System Solutions: Monitors

Notes

  1. Monitor: high-level construct; only one process active inside at a time.

Slide 38 / 45

Operating System Solutions: Monitors

Slide 38: Operating System Solutions: Monitors
Slide 39 / 45

Operating System Solutions: Monitors

Slide 39: Operating System Solutions: Monitors
Slide 40 / 45

Variables

Slide 40: Variables

Notes

  1. Condition variables: x.wait() suspends the caller; x.signal() resumes one waiting process (no effect if none).

Slide 41 / 45

Monitor with Condition Variables

Slide 41: Monitor with Condition Variables
Slide 42 / 45

Condition Variables choices

Slide 42: Condition Variables choices
Slide 43 / 45

Solution to Dining Philosophers

Slide 43: Solution to Dining Philosophers
Slide 44 / 45

Solution to Dining Philosophers (cont)

Slide 44: Solution to Dining Philosophers (cont)
Slide 45 / 45

Process Synchronization Conclusion

Slide 45: Process Synchronization Conclusion