CS 134

Key Points: Synchronization Primitives

Critical Section Problem

  1. Definition
    • A segment of code that accesses shared resources
    • Must be executed by only one thread at a time
  2. Requirements for Solutions
    • Mutual Exclusion: Only one process in critical section at a time
    • Progress: Selection of next process to enter can't be postponed indefinitely
    • Bounded Waiting: Limit on how many times other processes can enter before a waiting process

Semaphores

  1. Concept
    • A synchronization primitive introduced by Dijkstra in 1965
    • Acts as a counter that can be incremented and decremented
  2. Operations
    • wait (P): Decrement counter, block if zero
    • signal (V): Increment counter, wake up blocked thread if any
  3. Applications
    • Solving producer–consumer problems
    • Implementing other synchronization primitives

Monitors

  1. Definition
    • Higher-level abstraction for synchronization
    • Encapsulates shared data and operations on that data
  2. Components
    • Mutual exclusion: Only one thread can execute monitor methods at a time
    • Condition variables: For managing thread waiting and signaling
  3. Advantages
    • More intuitive and less error-prone than semaphores
    • Built into some programming languages (e.g., Java)

Condition Variables

Part of the monitor concept; provides waiting from inside a critical section.

  1. Purpose
    • Allow threads to wait for a specific condition to become true
    • Enable fine-grained synchronization within monitors
  2. Operations
    • wait: Release lock and sleep until signaled
    • signal/notify: Wake up one waiting thread
    • broadcast/notifyAll: Wake up all waiting threads
  3. Usage Pattern
    • Always used in conjunction with a lock/mutex
    • Check conditions in a loop to handle spurious wakeups

Implementation in Different Languages

  1. Java
    • Built-in support for monitors (synchronized keyword)
    • wait(), notify(), and notifyAll() methods on all objects
  2. Python
    • threading module provides Lock and Condition classes
    • Context managers (with statement) for easy lock management
  3. C (POSIX)
    • pthread_mutex_t for locks
    • pthread_cond_t for condition variables
    • Functions like pthread_mutex_lock(), pthread_cond_wait(), etc.

OS/161 Specifics

  1. Lock API
    • lock_create(), lock_destroy(), lock_acquire(), lock_release(), lock_do_i_hold()
    • Similar to POSIX, but with separate create/destroy functions
  2. Condition Variable API
    • cv_create(), cv_destroy(), cv_wait(), cv_signal(), cv_broadcast()
    • Follows monitor-style usage patterns

Remember!

Understanding these synchronization primitives is crucial for managing concurrent access to shared resources in operating systems and multithreaded applications.

Always consider the trade-offs between different synchronization methods and choose the most appropriate one for your specific use case.

(When logged in, completion status appears here.)