π Lecture 9 β Overview of todayβs lecture
π Overview: This lecture addresses the fundamental challenge of coordinating multiple threads that access shared data in concurrent programs. It introduces the concepts of shared variable analysis, synchronization, critical sections, and the classic problems that arise when threads interleave their executions without proper control. Understanding these concepts is essential for writing correct and reliable multi-threaded programs.
ποΈ Topics Covered
This lecture covers shared variable analysis in multi-threaded C programs, the threads memory model and what resources are shared versus private, the concept of synchronization for controlling thread cooperation, concurrent execution and incorrect interleavings illustrated with badcnt.c, progress graphs for visualizing execution states, critical sections and unsafe regions, the classic bank account example demonstrating race conditions, the requirements for critical section solutions, and the "Too Much Milk" problem as a practical example of synchronization challenges.
π Lecture Summary
Shared Variables in Threaded C Programs
The question of which variables are shared in a threaded C program is not simply "global variables are shared" and "stack variables are private." Determining shared variables requires answering: what is the memory model for threads, how are variables mapped to memory instances, and how many threads reference each of these instances.
Threads Memory Model
Conceptually, each thread runs in the context of a process and has its own separate thread context including thread ID, stack, stack pointer, program counter, condition codes, and general purpose registers. All threads share the remaining process context: code, data, heap, and shared library segments of the process virtual address space, as well as open files and installed handlers. Operationally, this model is not strictly enforced because while register values are truly separate and protected, any thread can read and write the stack of any other thread. This mismatch between the conceptual and operational model is a source of confusion and errors.
What resources are shared?
Local variables are not shared because they refer to data on the stack and each thread has its own stack. You should never pass, share, or store a pointer to a local variable on another thread's stack. Global variables are shared because they are stored in the static data segment and accessible by any thread. Dynamic objects are shared because they are stored in the heap and shared if you can name them; in C, you can conjure up the pointer (e.g., void *x = (void *) 0xDEADBEEF), while in Java, strong typing prevents this and references must be passed explicitly.
Synchronization
Threads cooperate in multithreaded programs to share resources and access shared data structures (e.g., threads accessing a memory cache in a web server) and also to coordinate their execution (e.g., a disk reader thread hands off blocks to a network writer thread through a circular buffer). For correctness, we must control this cooperation and assume threads interleave executions arbitrarily and at different rates because scheduling is not under the application writer's control. We control cooperation using synchronization, which enables us to restrict the interleaving of executions. Note: this also applies to processes and across machines in a distributed system.
Shared Variable Analysis
A variable x is shared iff multiple threads reference at least one instance of x. In the example with variable instances: ptr, svar, and msgs are shared because they are referenced by main thread and peer threads. i and myid are NOT shared because they are only referenced by one thread each.
badcnt.c: An Improperly Synchronized Threaded Program
The lecture presents badcnt.c which is an improperly synchronized threaded program. The assembly code for the counter loop reveals why problems occur at the instruction level.
Concurrent Execution
The key idea is that in general, any sequentially consistent interleaving is possible, but some are incorrect. Notation: I_i denotes that thread i executes instruction I, and %eax_i is the contents of %eax in thread i's context. An incorrect ordering is shown where two threads increment the counter, but the result is 1 instead of 2 because the interleaving of load, increment, and store operations from both threads causes one update to be lost.
Progress Graphs
A progress graph depicts the discrete execution state space of concurrent threads. Each axis corresponds to the sequential order of instructions in a thread. Each point corresponds to a possible execution state (Instβ, Instβ). For example, (Lβ, Sβ) denotes the state where thread 1 has completed Lβ and thread 2 has completed Sβ.
Trajectories in Progress Graphs
A trajectory is a sequence of legal state transitions that describes one possible concurrent execution of the threads. Example: Hβ, Lβ, Uβ, Hβ, Lβ, Sβ, Tβ, Uβ, Sβ, Tβ.
Critical Sections and Unsafe Regions
Instructions L, U, and S form a critical section with respect to the shared variable cnt. Instructions in critical sections (with respect to some shared variable) should not be interleaved. Sets of states where such interleaving occurs form unsafe regions.
Safe and Unsafe Trajectories
A trajectory is safe iff it doesn't touch any part of an unsafe region. A trajectory is correct with respect to cnt iff it is safe.
The classic example
Consider a function to withdraw money from a bank account:
int withdraw(account, amount) {
int balance = get_balance(account);
balance -= amount;
put_balance(account, balance);
return balance;
}
If a husband and wife share a bank account with a balance of $100.00 and both go to separate ATM machines to simultaneously withdraw $10.00, the execution of the two threads can be interleaved. One possible interleaving: get_balance (100), get_balance (100), balance -= amount (90), balance -= amount (90), put_balance (90), put_balance (90). The final balance is $90 instead of the correct $80. The bank loses $10 and the customers gain $10.
Race conditions and concurrency
An atomic operation is an operation that always runs to completion or not at all. It is indivisible and cannot be stopped in the middle. On most machines, memory reference and assignment (load and store) of words are atomic. However, many instructions are not atomic. For example, on most 32-bit architectures, double precision floating point store is not atomic because it involves two separate memory operations.
π Definition β Atomic operation: An operation that always runs to completion or not at all. It is indivisible and cannot be stopped in the middle.
The crux of the problem
The problem is that two concurrent threads (or processes) access a shared resource (account) without any synchronization, which creates a race condition where the output is non-deterministic and depends on timing. We need mechanisms for controlling access to shared resources in the face of concurrency so we can reason about the operation of programs, essentially re-introducing determinism. Synchronization is necessary for any shared data structure: buffers, queues, lists, hash tables, scalars, and so on.
Synchronization related definitions
Mutual exclusion: ensuring that only one thread does a particular thing at a time. One thread doing it excludes all others.
π Definition β Mutual exclusion: Ensuring that only one thread does a particular thing at a time. One thread doing it excludes all others.
Critical section: piece of code that only one thread can execute at one time. All other threads are forced to wait on entry. When a thread leaves a critical section, another can enter. Mutual exclusion is required inside the critical section.
π Definition β Critical section: Piece of code that only one thread can execute at one time. All other threads are forced to wait on entry. When a thread leaves a critical section, another can enter.
The key idea is that all synchronization involves waiting.
Critical section solution requirements
Critical sections have the following requirements:
- Mutual exclusion: at most one thread is in the critical section.
- Progress: if thread T is outside the critical section, then T cannot prevent thread S from entering the critical section.
- Bounded waiting (no starvation): if thread T is waiting on the critical section, then T will eventually enter the critical section. This assumes threads eventually leave critical sections.
- Performance: the overhead of entering and exiting the critical section is small with respect to the work being done within it.
Synchronization: Too Much Milk
This is a classic synchronization problem. Person A looks in the fridge, sees no milk at 3:00 and leaves for the store at 3:05. Person B independently looks in the fridge at 3:10, sees no milk, and also leaves for the store. Both buy milk and return home with milk, resulting in too much milk.
Too much milk: Solution 1
Solution #1 attempts to use notes:
if (noMilk) {
if (noNote) {
leave Note;
buy milk;
remove note;
}
}
This does not work because both threads could check for no note before either leaves a note, and then both would buy milk.
Too much milk: Solution 2
Solution #1 makes the problem worse because it fails only occasionally, making it very hard to debug. The constraint has to be satisfied independent of what the dispatcher does because the timer can go off and a context switch can happen at any time.
Solution #2:
- Thread A: leave note A, if (no Note from B) { if (noMilk) buy Milk; }, remove Note A.
- Thread B: leave note B, if (no Note from A) { if (noMilk) buy Milk; }, remove Note B.
π‘ Why this matters: Solution #2 can still fail because if both threads leave notes before checking each other's notes, both may see no note from the other and both could proceed to buy milk.
β Key Takeaways
The most critical concept from this lecture is that concurrent access to shared variables without synchronization creates race conditions where program behavior becomes non-deterministic and can produce incorrect results, as demonstrated by the bank account example where both withdrawals complete but only one is reflected in the balance. Students must understand that a variable is shared if multiple threads reference at least one instance of it, and that instructions in critical sections (like load, increment, store on a counter) must not be interleaved across threads to maintain correctness. Progress graphs provide a visual tool for understanding which interleavings are safe and which enter unsafe regions. The four requirements for any correct critical section solution are mutual exclusion, progress, bounded waiting, and performance. Finally, the "Too Much Milk" problem illustrates that simple solutions using only atomic load and store operations are often insufficient for synchronization.
π§ Quick Revision Questions
- What is the definition of a shared variable in a multi-threaded C program?
- In the badcnt.c example, explain how the interleaving of assembly instructions from two threads can cause the final counter value to be 1 instead of 2.
- What are the four requirements for a correct critical section solution, and what does each requirement mean?
- In the bank account withdrawal example with a starting balance of $100, what sequence of interleaved operations from two threads withdrawing $10 each results in a final balance of $90?
- Why does Solution #2 for the "Too Much Milk" problem fail to guarantee mutual exclusion?
π Lecture 10 β Overview of todayβs lecture
π Overview: This lecture completes the analysis of concurrency examples, focusing on the βToo Much Milkβ problem and its solution. It then introduces locks as a fundamental synchronization primitive, detailing their implementation via disabling interrupts, busy-waiting, and hardware atomic instructions like test&set. Finally, it introduces semaphores as a higher-level synchronization mechanism.
ποΈ Topics Covered
This lecture continues the previous discussion of concurrency with the βToo Much Milk Solution 3β and its summary. It then formally introduces locks, their definition, and three implementation strategies: disabling interrupts (flawed on uniprocessors), busy-waiting (with its performance problems), and using atomic read-modify-write instructions like test&set (suitable for multiprocessors). The lecture concludes with an introduction to semaphores, their definition, operations (wait and signal), blocking behavior, and the two main types (binary and counting).
π Lecture Summary
Too Much Milk Solution 3
This solution uses leaving notes to coordinate two threads (A and B) buying milk. Thread A leaves a note, then checks if Bβs note is present; if so, it busy-waits. If not, it checks for milk and buys if needed. Thread B leaves a note and does the same, but only buys milk if there is no note from A.
π Definition β Busy waiting: A thread continuously checks a condition in a loop while waiting, consuming CPU cycles without doing useful work.
π Example: Thread A checks βwhile (note B)β at point X. If Bβs note is absent, itβs safe to proceed. If Bβs note is present, A does not know if B will buy milk or is also waiting β so A loops. At point Y, B checks βif (no note A)β: if Aβs note is absent, B knows it is safe to buy milk. If Aβs note is present, B knows A is either buying or waiting for B to quit, so B can safely quit.
π‘ Why this matters: This solution works but is complex, thread-specific, and uses busy waiting. It motivates the need for simpler, higher-level synchronization primitives like locks.
Locks
A lock is a synchronization mechanism that prevents more than one thread from entering a critical section at the same time. The Acquire() call waits until the lock is free, then grabs it. The Release() call unlocks it, waking up a waiter if any. These operations must be atomic to prevent race conditions.
π Definition β Atomic operation: An operation that appears to happen instantaneously from the perspective of other threads β no intermediate state is visible.
π Example: With locks, the Too Much Milk problem becomes trivial: lock->Acquire(); if (noMilk) buy milk; lock->Release();
Implementing Locks with Disabling Interrupts (Uniprocessor Only)
On a uniprocessor, an interrupt can cause a context switch in the middle of an operation. Disabling interrupts prevents such external events, making the operation atomic. However, a flawed simple implementation is Lock::Acquire() { disable interrupts; } and Lock::Release() { enable interrupts; }.
π Definition β Disabling interrupts: A hardware mechanism that tells the processor to delay handling of external events (like I/O interrupts) until interrupts are re-enabled.
π Example: If a thread uses the flawed implementation, it might disable interrupts and then take a page fault or do disk I/O. In that case, it needs interrupts to be enabled to handle the I/O completion. Also, disabling interrupts for a long critical section could cause keystrokes to be lost because the interrupt for one keystroke isnβt handled by the time the next one occurs.
Implementing Locks with Busy Waiting
In this approach, a Lock class has an integer value (FREE = 0, BUSY = 1). The Acquire() method disables interrupts, then busy-waits (while (value != FREE)) β during the wait, it briefly enables then disables interrupts to allow other events. When free, it sets value = BUSY and re-enables interrupts. Release() sets value = FREE.
π Formula: Lock::Acquire(): disable interrupts; while (value != FREE) { enable interrupts; disable interrupts; } value = BUSY; enable interrupts.
π Example: If a high-priority thread enters the busy-waiting loop for a lock held by a low-priority thread, the timer may go off but the low-priority thread might never run (depending on the scheduling policy). This is a priority inversion problem, and the busy-waiting consumes CPU cycles inefficiently.
Implementing Locks with Test and Set (Hardware Instruction)
On multiprocessors, disabling interrupts is insufficient. Modern processors provide atomic read-modify-write instructions that read a value from memory into a register and write a new value, all atomically. The test&set instruction reads a memory location, sets it to 1, and returns the old value.
π Definition β Test&Set: An atomic instruction that reads a memory location, writes 1 back to it, and returns the original value.
π Formula: Lock::Acquire { while (test&set(value) == 1) ; // Do nothing } Lock::Release { value = 0; }
π Example: Initially, value = 0 (free). When thread A calls Acquire, test&set(value) reads 0 and sets value = 1. Since it returns 0, the loop exits, and A enters the critical section. If thread B tries to Acquire, test&set(value) reads 1 and sets it back to 1 (no change). It returns 1, so B busy-waits.
π‘ Why this matters: This works on both uniprocessors and multiprocessors. The busy-wait can be modified to put the waiting thread to sleep, making it more efficient.
Semaphores β Introduction and Definition
Semaphores are a higher-level synchronization primitive, invented by Dijkstra. A semaphore is a variable manipulated atomically through two operations: wait (also called P or down) and signal (also called V or up). wait decrements the counter; signal increments it. If the counter falls below zero, the wait operation blocks the calling thread.
π Definition β Semaphore: A synchronization primitive with a counter and a queue of waiting threads, accessed only through atomic wait and signal operations.
π Example: A binary semaphore (counter initialized to 1) provides exclusive access to a resource. A counting semaphore (counter initialized to N) allows up to N threads to access a resource concurrently.
π Definition β Binary semaphore (mutex): A semaphore with a counter initialized to 1, used to guarantee mutually exclusive access. π Definition β Counting semaphore: A semaphore with a counter initialized to N, representing N units of a resource.
π Example: To protect a shared variable cnt updated by multiple threads, we initialize a semaphore mutex to 1. Each thread calls wait(mutex) before accessing cnt and signal(mutex) after, ensuring safe sharing.
β Key Takeaways
The βToo Much Milkβ problem demonstrates that simple load/store instructions are insufficient for reliable synchronization, motivating the need for locks and semaphores. Locks provide a straightforward Acquire/Release interface, but their implementation must be atomic. On a uniprocessor, disabling interrupts can achieve atomicity but has severe drawbacks (user code disabling interrupts, long critical sections causing lost keystrokes, and blocking I/O). Busy-waiting implementations waste CPU and can cause priority inversion. For multiprocessors, hardware atomic instructions like test&set are essential. Semaphores abstract synchronization further, using a counter and a blocking queue to provide both mutual exclusion (binary semaphore) and resource management (counting semaphore), making them more robust for general use.
π§ Quick Revision Questions
- What are the three main problems with the βToo Much Milkβ Solution 3 that motivate the need for locks?
- Why is the simple
Lock::Acquirethat just disables interrupts considered flawed? Provide two specific reasons. - Explain the key difference between implementing a lock with busy-waiting vs. without busy-waiting (using a queue).
- How does the
test&setinstruction implement a lock, and why is it better than interrupt disabling on a multiprocessor? - What is the difference between a binary semaphore and a counting semaphore? In what scenario would you use each?
π Lecture 11 β Producer Consumer Problem, Semaphores, and Condition Variables
π Overview: This lecture addresses the classic producer-consumer synchronization problem using a bounded buffer. It explains how semaphores can be used for both mutual exclusion and scheduling constraints, and introduces condition variables and monitors as higher-level synchronization constructs. Understanding these concepts is crucial for designing correct concurrent systems where threads must coordinate access to shared resources.
ποΈ Topics Covered
The lecture covers the producer-consumer problem with a bounded buffer, including correctness constraints and a semaphore-based solution. It then explains the two distinct uses of semaphores: for mutual exclusion and for scheduling constraints. The lecture introduces monitors as a synchronization abstraction composed of a lock and condition variables, defines the condition variable operations (wait, signal, broadcast), and finally distinguishes between Hoare-style and Mesa-style monitor implementations.
π Lecture Summary
Producer-consumer with a bounded buffer
The problem involves a producer that puts items into a shared fixed-size buffer and a consumer that takes items out. Synchronization is needed to coordinate their access. Practical examples include multimedia processing (producer creates MPEG frames, consumer renders them) and event-driven graphical user interfaces (producer detects mouse clicks and keyboard hits, consumer retrieves events and paints the display). The goal is to avoid lockstep operation between producer and consumer by using a fixed-size buffer with synchronized access. The producer must wait if the buffer is full, and the consumer must wait if the buffer is empty.
π Definition β Correctness constraints: The solution must satisfy three constraints: 1) Consumer must wait for producer to fill buffers if none are full (scheduling constraint). 2) Producer must wait for consumer to empty buffers if all are full (scheduling constraint). 3) Only one thread can manipulate the buffer queue at a time (mutual exclusion).
Semaphore solution
The solution uses a separate semaphore for each constraint. Semaphores are used in multiple ways simultaneously.
- Semaphore
fullBuffers= 0 (initially, no item in buffer) - Semaphore
emptyBuffers= numBuffers (initially, number of empty slots; counts available resources) - Semaphore
mutex= 1 (no one using the buffer; used for mutual exclusion)
Producer() code:
emptyBuffers.P(); // wait for empty slot
mutex.P(); // ensure exclusive access to buffer
put 1 item in buffer;
mutex.V(); // release exclusive access
fullBuffers.V(); // signal that a full buffer is available
Consumer() code:
fullBuffers.P(); // wait for a full buffer
mutex.P(); // ensure exclusive access to buffer
take 1 item out;
mutex.V(); // release exclusive access
emptyBuffers.V(); // signal that an empty slot is available
π Formula: The sequence of operations shows that producer and consumer perform P and V operations on different semaphores. The producer waits on emptyBuffers and signals fullBuffers; the consumer waits on fullBuffers and signals emptyBuffers.
π Example: If the buffer has 5 slots initially: emptyBuffers = 5, fullBuffers = 0, mutex = 1. The first producer call executes emptyBuffers.P() (decrements to 4), then mutex.P() (decrements to 0), puts an item, then mutex.V() (increments to 1), then fullBuffers.V() (increments to 1). A consumer can now run.
π‘ Why this matters: The order of P operations is important β if mutex.P() were done before emptyBuffers.P(), a deadlock could occur. The order of V operations is not critical because they release resources. The solution works for multiple producers and consumers without changes, as the semaphores manage the counts.
Two uses of semaphores
-
Mutual exclusion: The semaphore has an initial value of 1.
P()is called before the critical section, andV()is called after the critical section. This ensures only one thread accesses the shared data at a time. -
Scheduling constraints: Semaphores express generalized scheduling constraintsβa way for a thread to wait for something. The initial value is typically 0 but not always. For example, implementing Thread's
join(reaping) uses semaphores with an initial value of 0:ThreadJoincallsP()andThreadFinishcallsV().
π Definition β Monitor: A monitor is a lock and zero or more condition variables for managing concurrent access to shared data. While textbooks describe monitors as a programming language construct where the lock is acquired automatically on calling any procedure, no widely-used language does this. In real-life operating systems (Windows, Linux, Solaris), monitors are used with explicit calls to locks and condition variables.
Condition variables
A condition variable is a queue of threads waiting for something inside a critical section. Key idea: make it possible to go to sleep inside a critical section by atomically releasing the lock at the same time as going to sleep. This prevents deadlock where a sleeping thread holds the lock, blocking others from entering.
Condition variables support three operations:
- Wait() β Release lock, go to sleep, re-acquire lock (releasing lock and going to sleep is atomic).
- Signal() β Wake up a waiter, if any.
- Broadcast() β Wake up all waiters.
Rule: Must hold the lock when doing condition variable operations.
A synchronized queue using condition variables:
AddToQueue() {
lock.Acquire();
put item on queue;
condition.signal();
lock.Release();
}
RemoveFromQueue() {
lock.Acquire();
while nothing on queue
condition.wait(&lock); // release lock; go to sleep; re-acquire lock
remove item from queue;
lock.Release();
return item;
}
π Example: If the queue is empty and a consumer thread calls RemoveFromQueue, it acquires the lock, then enters the while loop. It calls condition.wait(&lock) which atomically releases the lock and puts the thread to sleep. When a producer calls AddToQueue, it acquires the lock, puts an item on the queue, calls condition.signal() (waking up the waiter), then releases the lock. The consumer then re-acquires the lock, exits the while loop, removes the item, releases the lock, and returns the item.
π‘ Why this matters: The while loop (not an if) is critical because of Mesa-style semanticsβthe waiter may be woken up but another thread could have taken the item before it runs.
Mesa vs. Hoare monitors
The precise definition of signal and wait matters.
Mesa-style (most real operating systems): The signaller keeps the lock and the processor. The waiter is simply put on the ready queue with no special priority. The waiter may have to wait for the lock again.
Hoare-style (most textbooks): The signaller gives up the lock and CPU to the waiter; the waiter runs immediately. The waiter gives the lock and processor back to the signaller when it exits the critical section or if it waits again.
Readers/Writers problem
Motivation: Shared database (e.g., bank balances, airline seats) with two classes of users: Readers (never modify database) and Writers (read and modify database). Using a single lock on the database is overly restrictive. The goal is to allow many readers at the same time but only one writer at a time.
β Key Takeaways
The producer-consumer problem with a bounded buffer requires three correctness constraints: consumer waits if no items, producer waits if buffer full, and mutual exclusion on buffer manipulation. Semaphores serve two distinct purposesβmutual exclusion (initial value 1) and scheduling constraints (initial value 0), and the order of P operations is critical to avoid deadlock. Condition variables allow a thread to sleep inside a critical section by atomically releasing the lock, supporting wait, signal, and broadcast operations with the rule that the lock must be held when using them. In Mesa-style monitors (used in real systems), the waiter is simply placed on the ready queue and may compete for the lock again, while Hoare-style (textbook) gives immediate control to the waiter. The Readers/Writers problem demonstrates that different types of access to shared data may require different synchronization strategiesβmultiple readers can coexist, but writing requires exclusive access.
π§ Quick Revision Questions
- What are the three correctness constraints for the producer-consumer problem with a bounded buffer, and which semaphore enforces each?
- Why must the
P()operation on the scheduling semaphore be performed before theP()operation on the mutex semaphore in the producer-consumer solution? - What is the key distinction between using a semaphore for mutual exclusion versus using it for scheduling constraints, in terms of initial value and operations?
- How does a condition variable's
Wait()operation atomically handle the release of the lock and going to sleep, and why is this atomicity necessary? - What is the difference between Mesa-style and Hoare-style monitors regarding what happens when a thread calls
Signal()on a condition variable?
π Lecture 12 β Readers/Writers Problem & Thread Safety
π Overview: This lecture presents the classic readers/writers synchronization problem and its solution using condition variables. It then explores the duality between semaphores and monitors (condition variables), demonstrating how condition variables can be implemented using semaphores. Finally, the lecture introduces thread safety concepts, categorizing thread-unsafe functions and presenting methods to make functions reentrant and thread-safe.
ποΈ Topics Covered
The lecture covers the readers/writers problem with its constraints and solution using condition variables, including state variables (AR, AW, WR, WW) and conditions okToRead and okToWrite. It examines the duality of synchronization primitives and implements condition variables using semaphores as building blocks. The lecture then introduces thread safety, four classes of thread-unsafe functions (failing to protect shared variables, relying on persistent state, returning pointer to static variable, calling thread-unsafe functions), reentrant functions, and thread-safe C library functions.
π Lecture Summary
Readers/Writers
Constraints:
- Readers can access database when no writers (Condition okToRead)
- Writers can access database when no readers or writers (Condition okToWrite)
- Only one thread manipulates state variables at a time
Basic structure of solution:
- Reader: wait until no writers β access database β check out -- wake up waiting writer
- Writer: wait until no readers or writers β access database β check out -- wake up waiting readers or writer
State variables:
- # of active readers -- AR = 0
- # of active writers -- AW = 0
- # of waiting readers -- WR = 0
- # of waiting writers -- WW = 0
- Condition okToRead = NIL
- Condition okToWrite = NIL
- Lock lock = FREE
Code:
Reader() {
lock.Acquire();
while ((AW + WW) > 0) { // check if safe to read if any writers, wait
WR++;
okToRead.Wait(&lock);
WR--;
}
AR++;
lock.Release();
Access DB
lock.Acquire();
AR--;
If (AR == 0 && WW > 0) // if no other readers still active, wake up writer
okToWrite.Signal(&lock);
lock.Release();
}
Writer() { // symmetrical
lock.Acquire();
while ((AW + AR) > 0) { // check if safe to write
// if any readers or writers, wait
WW++;
okToWrite->Wait(&lock);
WW--;
}
AW++;
lock.Release();
Access DB
// check out
lock.Acquire();
AW--;
if (WW > 0) // give priority to other writers
okToWrite->Signal(&lock);
else if (WR > 0)
okToRead->Broadcast(&lock);
lock.Release();
}
Questions:
- Can readers or writers starve? Who and Why? β Writers could starve because readers are given priority (readers can join while writer is waiting)
- Why does checkRead need a while? β Because multiple readers might be waiting; after a writer signals, the condition might change if another reader woke up first
π‘ Why this matters: The readers/writers problem is fundamental in database systems and file systems where concurrent reads are safe but writes need exclusive access.
Semaphores and Monitors
Illustrate the differences by considering: can we build monitors out of semaphores?
Does this work?
Wait() { semaphore -> P(); }
Signal() { semaphore -> V(); }
Condition variables only work inside of a lock. Does this work?
Wait(Lock *lock) {
lock->Release();
Semaphore -> P();
Lock -> Acquire();
}
Signal() {
Semaphore -> V();
}
Key insight:
- What if thread signals and no one is waiting? No op.
- What if thread later waits? Thread waits.
- What if thread V's and no one is waiting? Increment.
- What if thread later does P? Decrement and continue.
- P + V are commutative β result is the same no matter what order they occur.
- Condition variables are NOT commutative. That's why they must be in a critical section β need to access state variables to do their job.
Does this fix the problem?
Signal() {
if semaphore queue is not empty
semaphore->V();
}
For one, not legal to look at contents of semaphore queue. But also: race condition β signaller can slip in after lock is released, and before wait. Then waiter never wakes up! Need to release lock and go to sleep atomically.
Implementation of condition variables using semaphores:
Semaphore mutex = 1; // This lock is outside of the condition object
Condition {
Semaphore lock = 1;
Semaphore waitSem = 0;
Int numWaiters = 0;
}
wait(cond, mutex) signal(cond, mutex)
{ {
P(cond.lock); P(cond.lock);
cond.numWaiters++; if (cond.numWaiters > 0)
V(cond.lock); {
V(mutex); V(cond.waitSem);
P(cond.waitSem); }
P(cond.lock); V(cond.lock);
cond.numWaiters--;
V(cond.lock);
P(mutex);
}
Thread Safety
- Functions called from a thread must be thread-safe.
- We identify four (non-disjoint) classes of thread-unsafe functions:
- Class 1: Failing to protect shared variables.
- Class 2: Relying on persistent state across invocations.
- Class 3: Returning a pointer to a static variable.
- Class 4: Calling thread-unsafe functions.
Thread-Unsafe Functions
-
Class 1: Failing to protect shared variables.
- Fix: Use P and V semaphore operations.
- Issue: Synchronization operations will slow down code.
-
Class 3: Returning a ptr to a static variable.
- Fixes:
- Rewrite code so caller passes pointer to struct: Requires changes in caller and callee.
struct hostent *gethostbyname(char name) { static struct hostent h; <contact DNS and fill in h> return &h; } - Lock-and-copy: Requires only simple changes in caller (and none in callee). However, caller must free memory.
struct hostent *gethostbyname_ts(char *p) { struct hostent *q = Malloc(...); P(&mutex); /* lock */ p = gethostbyname(name); *q = *p; /* copy */ V(&mutex); return q; } hostp = Malloc(...)); gethostbyname_r(name, hostp);
- Rewrite code so caller passes pointer to struct: Requires changes in caller and callee.
- Fixes:
-
Class 4: Calling thread-unsafe functions.
- Calling one thread-unsafe function makes an entire function thread-unsafe.
- Fix: Modify the function so it calls only thread-safe functions.
Reentrant Functions
- A function is reentrant iff it accesses NO shared variables when called from multiple threads.
- Reentrant functions are a proper subset of the set of thread-safe functions.
- The fixes to Class 2 and 3 thread-unsafe functions require modifying the function to make it reentrant.
Thread-Safe Library Functions
- All functions in the Standard C Library are thread-safe.
- Examples: malloc, free, printf, scanf
- Most Unix system calls are thread-safe, with a few exceptions.
β Key Takeaways
The readers/writers problem solution uses four state variables (AR, AW, WR, WW) and two condition variables (okToRead, okToWrite) protected by a single lock. Condition variables cannot be naively implemented with semaphores because semaphore P/V operations are commutative while condition variables are not β the lock release and sleep must be atomic to avoid race conditions. Thread-unsafe functions fall into four classes, with Class 3 (returning pointer to static variable) being fixable through lock-and-copy or rewriting callers to pass pointers. Reentrant functions are a proper subset of thread-safe functions that access no shared variables, making them inherently safe for concurrent use.
π§ Quick Revision Questions
- In the readers/writers solution, why must the Reader() use a while loop instead of an if statement when checking (AW + WW) > 0?
- When a Writer finishes accessing the database, why does it first check if WW > 0 before checking WR > 0?
- Why are semaphore P() and V() operations commutative, while condition variable Wait() and Signal() are not commutative?
- In the implementation of condition variables using semaphores, why must V(mutex) and P(cond.waitSem) be separated by the count increment?
- What is the difference between a thread-safe function and a reentrant function?
π Lecture 13 β Deadlocks
π Overview: This lecture provides a comprehensive introduction to deadlocks in operating systems, covering their formal definition, necessary conditions, detection methods, and various strategies for prevention and avoidance. Understanding deadlocks is critical for building robust concurrent systems where multiple processes or threads share resources.
ποΈ Topics Covered
The lecture covers the formal definition of a deadlock, the four necessary and sufficient conditions (mutual exclusion, no preemption, hold and wait, circular wait), detection through resource graphs, prevention techniques including removing one of the four conditions, the Banker's algorithm for deadlock avoidance, resource ordering, and current practices in commercial operating systems and databases.
π Lecture Summary
Overview of todayβs lecture
This section introduces the main topics to be covered in Lecture 13, including the definition of deadlocks, detection methods, the four necessary and sufficient conditions for deadlock, examples, avoidance strategies, prevention techniques, and current practice in real systems.
Formal definition of a deadlock
A deadlock occurs when a set of processes is deadlocked if each process in the set is waiting for an event that only another process in the set can cause. Usually, the event is the release of a currently held resource. None of the processes can run, release resources, or be awakened.
π‘ Why this matters: This definition captures the essence of a deadlock β a self-sustaining cycle of waiting where no progress is possible.
Resource graph
A resource graph is used to model the relationship between processes and resources. It helps visualize which processes hold which resources and which resources processes are waiting for, making it easier to detect deadlocks.
Conditions for deadlock
Without all of the following four conditions, a deadlock cannot occur:
- Mutual exclusion β resources cannot be shared (e.g., mutex on bounded buffer)
- No preemption β if a process has a resource, it cannot be forcibly taken away
- Hold and wait β processes hold allocated resources while waiting for additional ones
- Circular wait β there exists a cycle of processes each waiting for a resource held by another in the cycle
Deadlock example
The lecture presents an example using a transfer function that operates on two accounts, each protected by a semaphore. Given two threads, a deadlock can occur if Thread 1 locks account a and waits for account b, while Thread 2 locks account b and waits for account a β creating a circular wait.
/* transfer x dollars from a to b */
void transfer(account *a, account *b, int x) {
P(a->sema);
P(b->sema);
a->balance += x;
b->balance -= x;
V(a->sema);
V(b->sema);
}
Deadlocks donβt require synch primitives
Deadlocks do not require locks; they just require circular constraints. An example is given with two threads that send and receive data using two circular buffers of size 4096 bytes. A full buffer causes the sender to block until the receiver removes data.
Example:
T1: T2:
send n bytes to T2 while(receive 4K of data)
; process data
while(receive data) send 4K result to T1
display data exit
exit
Solutions to Deadlock: Detect deadlock and fix
The first approach is to detect deadlocks and then fix them. Detection involves scanning the resource graph and detecting cycles. Fixing is the hard part and can be done by:
- Shooting the thread β force it to give up resources. However, this is not always possible; for instance, with a mutex, you cannot shoot a thread and leave the world in an inconsistent state.
- Rolling back actions of deadlocked threads (transactions) β this is a common technique in databases.
Transactions and two phase commit
In two-phase commit, all resources are acquired first. If any resource is blocked on, all previously acquired resources are released, and the transaction is retried.
Example (Printfile):
lock-file
lock-printer
lock-disk
do work
release all
Pros: Dynamic, simple, flexible Cons:
- Cost increases with the number of resources
- Length of critical section increases
- Hard to know what's needed a priori
Preventing deadlock
To prevent deadlock, one of the four conditions must be eliminated: a) No sharing β totally independent threads b) Preempt resources c) Make all threads request and reserve everything they'll need at the beginning
- Problem: Predicting future is hard, leading to over-estimation of resource needs (inefficient)
Banker's algorithm
The Banker's algorithm is more efficient than reserving all resources on startup. It works by:
- Stating maximum resource needs in advance
- Allocating resources dynamically when needed
- Waiting if granting a request would lead to deadlock (a request can be granted if some sequential ordering of requests is deadlock-free)
The algorithm allows the sum of maximum resource needs of all current threads to be greater than the total resources, as long as there is some way for all threads to finish without deadlock. For example, a thread can proceed if: total available resources - # allocated >= max remaining that might be needed by this thread.
In practice, the Banker's algorithm is rarely used since it requires predicting maximum resource requirements in advance.
Resource ordering
Resource ordering involves making everyone use the same ordering when accessing resources. For example, all threads must grab semaphores in the same order to prevent circular wait.
Current practice
- Microsoft SQL Server: The Database Engine automatically detects deadlock cycles. It chooses one session as a deadlock victim and terminates the current transaction with an error to break the deadlock.
- Oracle: Similar to SQL Server, plus multitable deadlocks can usually be avoided if transactions accessing the same tables lock those tables in the same order. For example, when both a master and detail table are updated, the master table is locked first, then the detail table.
- Windows internals (Linux no different): Unless they changed things significantly in Vista, the NT kernel architecture is described as a "deadlock minefield." With the multi-threaded re-entrant kernel, there is plenty of deadlock potential. Lock ordering is great in theory, and NT was originally designed with mutex levels, but they had to be abandoned due to complex interactions between memory management, the cache manager, and file systems.
β Key Takeaways
The most critical points from this lecture are: deadlock occurs when each process in a set waits for an event only another in the set can cause, requiring all four conditions (mutual exclusion, no preemption, hold and wait, circular wait) simultaneously. Detection involves scanning resource graphs for cycles, while prevention requires eliminating at least one condition. The Banker's algorithm avoids deadlock by granting requests only if a safe sequence exists, but is rarely used in practice due to prediction difficulties. Real systems like SQL Server and Oracle automatically detect and resolve deadlocks by terminating a victim transaction, and commercial OS kernels still face significant deadlock challenges despite theoretical solutions.
π§ Quick Revision Questions
-
What are the four necessary and sufficient conditions for a deadlock to occur?
-
How does a resource graph help in detecting deadlocks?
-
In the Banker's algorithm, what condition must be satisfied before a thread can proceed with its resource request?
-
What is the key difference between deadlock prevention and deadlock avoidance?
-
Why is resource ordering (locking resources in a consistent order) an effective strategy against deadlock, and why did NT kernel have to abandon it?