Professional Documents
Culture Documents
www.utdallas.edu
Objectives
Understand
The Critical-Section Problem And its hardware and software solutions
www.utdallas.edu
Consumer-Producer Problem
Classic example of process coordination Two processes sharing a buffer One places items into the buffer (producer)
Must wait if the buffer is full
www.utdallas.edu
Producer Process
while (true) { /* produce an item in nextProduced */ while (count == BUFFER_SIZE) ; // do nothing buffer [in] = nextProduced; in = (in + 1) % BUFFER_SIZE; count++; }
www.utdallas.edu
Consumer Process
while (true) { while (count == 0) ; // do nothing nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; count--; /*consume item in nextConsumed */ } What can go wrong with this solution?
The University of Texas at Dallas www.utdallas.edu
5
Race Condition
count++ could be implemented as register1 = count register1 = register1 + 1 count = register1 count-- could be implemented as register2 = count register2 = register2 - 1 count = register2 Consider this execution interleaving with count = 5 initially: S0: producer executes register1 = count {register1 = 5} S1: producer executes register1 = register1 + 1 {register1 = 6} S2: consumer executes register2 = count {register2 = 5} S3: consumer executes register2 = register2 - 1 {register2 = 4} S4: producer executes count = register1 {count = 6 } S5: consumer executes count = register2 {count = 4}
6
www.utdallas.edu
Race Condition
Occurs when multiple processes manipulate shared data concurrently and the result depends on the particular order of manipulation Data inconsistency may arise Solution idea
Mark code segment that manipulates shared data as critical section If a process is executing its critical section, no other processes can execute their critical sections
More formally, any method that solves the CriticalSection Problem must satisfy three requirements
The University of Texas at Dallas www.utdallas.edu
7
Each process executes at a nonzero speed No restriction on the relative speed of the N processes
The University of Texas at Dallas www.utdallas.edu
8
do { acquire lock critical section release lock remainder section } while (TRUE);
www.utdallas.edu
www.utdallas.edu
www.utdallas.edu
www.utdallas.edu
www.utdallas.edu
www.utdallas.edu
www.utdallas.edu
www.utdallas.edu
www.utdallas.edu
www.utdallas.edu
www.utdallas.edu
www.utdallas.edu
www.utdallas.edu
www.utdallas.edu
www.utdallas.edu
www.utdallas.edu
www.utdallas.edu
www.utdallas.edu
www.utdallas.edu
www.utdallas.edu
Process 0 Process 1 while (true) { while (true) { flag[0] = TRUE; flag[1] = TRUE; turn = 1; turn = 0; while (flag[1] && turn == 1) while (flag[0] && turn == 0) ; /* just wait*/ ; /* just wait*/ CRITICAL SECTION CRITICAL SECTION flag[0] = FALSE; flag[1] = FALSE; Access non-critical-section Access non-critical-section are areas } }
www.utdallas.edu
Synchronization Hardware
Many systems provide hardware support for critical section code more efficient and easier for programmers
www.utdallas.edu
32
TestAndndSet Instruction
Definition: boolean TestAndSet (boolean *target) { boolean rv = *target; *target = TRUE; return rv; }
www.utdallas.edu
33
www.utdallas.edu
Swap Instruction
Definition: void Swap (boolean *a, boolean *b) { boolean temp = *a; *a = *b; *b = temp; }
www.utdallas.edu
36
Semaphores
Much easier to use than hardware-based solutions Semaphore S integer variable Two standard operations to modify S:
wait() signal()
www.utdallas.edu
38
Semaphore Operations
wait (S) { while (S <= 0) ; // no-op, called busy waiting, spinlock S--; } signal (S) { S++; }
Later, we will see how to implement these operations with no busy waiting
The University of Texas at Dallas www.utdallas.edu
39
www.utdallas.edu
www.utdallas.edu
42
wait (S) { value--; if (value < 0) { add this process to waiting queue block(); } }
signal (S) { value++; if (value <= 0) { /*some processes are waiting*/ remove a process P from waiting queue wakeup(P); } }
The University of Texas at Dallas www.utdallas.edu
43
Semaphore Implementation
Must guarantee that no two processes can execute wait and signal on same semaphore at same time Thus, implementation becomes the critical section problem where wait and signal code are placed in critical section, and protected by
Disabling interrupts (uniprocessor systems only) Busy waiting or spinlocks (multiprocessor systems)
www.utdallas.edu
www.utdallas.edu
46
www.utdallas.edu
47
Bounded-Buffer Problem
N buffers, each can hold one item Semaphore mutex initialized to the value 1 Semaphore full initialized to the value 0 Semaphore empty initialized to the value N
www.utdallas.edu
48
Dining-Philosophers Problem
Models multiple processes sharing multiple resources Write a program for each philosopher s.t. no starvation/deadlock occurs Solution approach: Bowl of rice (data set) Array of semaphores: chopstick [5] initialized to 1
www.utdallas.edu
51
Pick chopsticks only if both are available Asymmetric: odd philosopher picks left chopstick first, even picks right first
The University of Texas at Dallas www.utdallas.edu
52
Readers-Writers Problem
Readers only read the data set; they do not perform any updates Writers can read and write
Problem allow multiple readers to read at the same time. Only one single writer can access the shared data at the same time. Shared Data
Data set Semaphore mutex initialized to 1 Semaphore wrt initialized to 1 Integer readcount initialized to 0
The University of Texas at Dallas
www.utdallas.edu
53
Condition Variables
Condition x, y; Two operations on a condition variable:
x.wait () a process that invokes the operation is suspended. x.signal () resumes one of processes (if any) that invoked x.wait ()
Condition variables are usually used to The University of Texas suspend/awake processes at Dallas
www.utdallas.edu
57
Monitors
Monitor: High-level abstraction that provides a convenient and effective mechanism for process synchronization
Compiler (not programmer) takes care of mutual exclusion
Only one process may be active within the monitor at a time monitor monitor_name { //shared variable declarations procedure P1 () { . } procedure Pn () {} Initialization code ( .) { } } The University of Texas at Dallas www.utdallas.edu
58
59
www.utdallas.edu
www.utdallas.edu
60
www.utdallas.edu
Each philosopher invokes the operations pickup() and putdown() in the following sequence: dp.pickup (i) EAT dp.putdown (i)
www.utdallas.edu
62
Monitors Implementation
It is up to the compiler to ensure mutual exclusion in monitors
Semaphores are usually used
Languages like Java, C# (not C), and Concurrent Pascal provide monitors-like mechanisms Java
public class SimpleClass { .... public synchronized void insert() { } public synchronized void remove() { } . }
Java guarantees that once a thread starts executing a synchronized method, no other thread can execute any other synchronized method in the class Java 1.5+ has semaphores, condition variables, mutex locks,
In java.util.concurrent package
Exercise: write java programs for the Producer-Consumer and dinning philosophers problems
www.utdallas.edu
63
Synchronization Examples
Windows XP
Linux Pthreads
www.utdallas.edu
64
Windows XP Synchronization
Masks interrupts to protect access to global resources on uniprocessor systems (inside kernel) Uses spinlocks on multiprocessor systems
Provides dispatcher objects for thread synchronization outside kernel, which can act as mutexes, semaphores, or events (condition variables)
www.utdallas.edu
65
Linux Synchronization
Linux:
disables interrupts to implement short critical sections (on single processor systems)
Linux provides:
semaphores Spinlocks (on SMP) Reader-writer locks
www.utdallas.edu
66
Pthreads Synchronization
Pthreads API is OSindependent It provides:
mutex locks condition variables
#include <pthread.h> pthread_mutex_t mutex;
67
pthread_mutex_init(&mutex, null);
pthread_mutex_lock(&mutex); pthread_mutex_unlock(&mutex);
extensions include:
semaphores read-write locks spin locks
Summary
Processor Synchronization
68
Multiple processes manipulating shared data and result depends on execution order
Critical section problem
Three requirements: mutual exclusion, progress, bounded waiting Software solution: Petersons Algorithm Hardware support: TestAndSet(), Swap()
Busy waiting (or spinlocks)
Semaphores:
wait(), signal() must be atomic moves the CS problem to kernelThe University of Texas at Dallas www.utdallas.edu