Authors: A. Silberschatz, P. Baer Galvin and G. Gagne ©2003
Introduce the critical section problem, ensuring consistency of shared data.
Present software (not hardware) solutions to the critical section problem.
Examine several classical process synchronization problems.
Explore tools used for solving process synchronization issues.
What can go wrong when multiple processes access the same data?
What is the general solution to this problem?
What different approaches can be used to implement this in software?
Provide two examples of classic synchronization problems.
Scenario: Two users sharing a bank account with initial balance £10, both depositing £1 concurrently.
Timeline events:
t0: Balance = £10
t1: Both get balance (£10)
t2: Both deposit £1 (£10)
t3: First deposit updates account (£11)
t4: Second gets updated (£11) and deposits (£1)
t5: Account updates to (£12)
Outcome: Ambiguity arises as both processes access the same data leading to inconsistent outputs.
Cooperating processes may share data through accessing files/messages or shared memory.
Threads can share code and data, especially in concurrent execution.
Issues arise from interrupted execution, leading to potential data inconsistency.
Mechanisms are needed to ensure orderly execution of cooperating processes.
Definition: A producer generates data consumed by a consumer via a shared buffer of limited size.
Key concepts:
Buffer: Circular list with a maximum size (BUFFER_SIZE).
Producer: Adds items when the buffer is not full.
Consumer: Removes items when the buffer is not empty.
Loop to continuously produce items.
Busy waiting when buffer is full.
Update buffer and counters appropriately.
Loop continuously to consume items.
Busy waiting when buffer is empty.
Update buffer and counters appropriately.
Occurs when multiple processes manipulate a shared variable (e.g., counter) concurrently.
Results in uncertain outcomes due to the non-deterministic order of execution.
Therefore, processes must be synchronized to manage shared variable access.
Definition: Multiple processes each having a critical section of code that should not be executed concurrently.
Objective: Ensure that no two processes execute in their critical sections at once.
Entry Section: Requests permission to enter critical section.
Critical Section: Protected code segment.
Exit Section: Notifies exit from the critical section.
Remainder Code: Non-critical section of the process.
Mutual Exclusion: Only one process in critical section at a time.
Progress: Allow non-blocking decisions for other processes to enter critical section.
Bounded Waiting: Limit the number of entries in critical sections before a waiting process gains access.
Effective algorithm for two processes sharing data with critical sections.
Data items shared:
int turn: Indicates whose turn to enter critical section.
boolean flag[2]: Indicates readiness to enter critical section.
Mutual Exclusion: Maintained for critical sections.
Progress: Guaranteed by turn management.
Bounded Waiting: Max wait of one turn enforced.
Software solutions that protect critical sections.
Implementation of simple acquire/release mechanism for locks.
Busy waiting occurs; referred to as spinlocks.
More sophisticated synchronization tool than mutex locks.
Semaphore S: An integer variable accessed via atomic operations: wait() and signal().
Variants include counting semaphores and binary semaphores.
Semaphores can block processes when unavailable, avoiding busy waiting.
Deadlock: Processes waiting indefinitely for one another.
Starvation: Indefinitely blocking of a process from execution due to scheduling.
Priority Inversion: Lower-priority process holding a lock requested by a higher-priority process, solvable via priority inheritance.
Bounded Buffer: Producer and consumer share synchronization primitives and rules for adding/removing items from the buffer.
Dining Philosophers Problem: Philosophers alternate between thinking and eating, needing two chopsticks to eat, leading to potential deadlock scenarios.
High-level abstraction that restricts access to shared variables by one active process.
Eases process synchronization but may not cover all synchronization cases effectively.
5.1 Background
5.2 The Critical-Section Problem
5.3 Peterson’s Solution
5.5 Mutex Locks
5.6 Semaphores
5.7 Classic Problems of Synchronization
5.8 Monitors