Lecture 9: Process Synchronization Joe McCarthy CSS 430: Operating Systems - Process Synchronization 1 Chapter 6: Process Synchronization • • • • • • • • • Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems of Synchronization Monitors Synchronization Examples Atomic Transactions Material derived, in part, from Operating Systems Concepts with Java, 8th Ed. © 2009 Silberschatz, Galvin & Gagne CSS 430: Operating Systems - Process Synchronization 2 Circular Buffer (Queue) CSS 430: Operating Systems - Process Synchronization 3 Producer-Consumer Problem public void insert () { // Producer E item; while (count == BUFFER_SIZE) ; // busy wait buffer[in] = item; in = (in + 1) % BUFFER_SIZE; ++count; } public E remove() { // Consumer E item; while (count == 0) ; // busy wait item = buffer[out]; out = (out + 1) % BUFFER_SIZE; --count; return item; } CSS 430: Operating Systems - Process Synchronization 4 Producer-Consumer Problem public void insert () { // Producer E item; while (count == BUFFER_SIZE) ; // busy wait buffer[in] = item; in = (in + 1) % BUFFER_SIZE; ++count; } public E remove() { // Consumer E item; while (count == 0) ; // busy wait item = buffer[out]; out = (out + 1) % BUFFER_SIZE; --count; return item; } CSS 430: Operating Systems - Process Synchronization LDA COUNT INCA STA COUNT LDA COUNT DECA STA COUNT 5 Producer-Consumer Problem import java.util.Date; public class Factory { public static void main( String[] args ) { // create the message queue Channel<Date> queue = new MessageQueue<Date>(); LDA COUNT INCA STA COUNT // create the producer and consumer threads Thread producer = new Thread( new Producer(queue) ); Thread consumer = new Thread( new Consumer(queue) ); // start the threads producer.start(); consumer.start(); } LDA COUNT DECA STA COUNT } [from Chapter 3: Processes] CSS 430: Operating Systems - Process Synchronization 6 Producer-Consumer Problem import java.util.Date; public class Factory { public static void main( String[] args ) { // create the message queue Channel<Date> queue = new MessageQueue<Date>(); Integer count = new Integer( 0 ); // create the producer and consumer threads Thread producer = new Thread( new Producer(queue, count) ); Thread consumer = new Thread( new Consumer(queue, count) ); // start the threads producer.start(); consumer.start(); } } LDA COUNT INCA STA COUNT LDA COUNT DECA STA COUNT [from Chapter 3: Processes] CSS 430: Operating Systems - Process Synchronization 7 Producer-Consumer Problem LDA COUNT INCA STA COUNT LDA COUNT DECA STA COUNT CSS 430: Operating Systems - Process Synchronization 8 Producer-Consumer Problem producer consumer LDA COUNT INCA STA COUNT LDA COUNT INCA LDA COUNT DECA STA COUNT STA COUNT CSS 430: Operating Systems - Process Synchronization LDA COUNT DECA STA COUNT 9 Producer-Consumer Problem producer consumer LDA COUNT INCA STA COUNT LDA COUNT INCA LDA COUNT DECA STA COUNT STA COUNT LDA COUNT DECA STA COUNT What’s the value of count? CSS 430: Operating Systems - Process Synchronization 10 Race Condition LDA COUNT INCA STA COUNT Who will get there first? LDA COUNT DECA STA COUNT CSS 430: Operating Systems - Process Synchronization 11 Race Condition LDA COUNT INCA STA COUNT Who will get there first? LDA COUNT DECA STA COUNT http://en.wikipedia.org/wiki/R ace_condition CSS 430: Operating Systems - Process Synchronization 12 Critical Section • Definition: segment of code that accesses a shared resource • Problem: multiple processes have concurrent access to shared resource • Solution: prevent multiple processes from having concurrent access to shared resource – No two processes are executing in their critical section at the same time. CSS 430: Operating Systems - Process Synchronization 13 General Structure • Protocol: – – – – Request entry to critical section Execute critical section Exit critical section Do other stuff (remainder section) CSS 430: Operating Systems - Process Synchronization 14 Solution to Critical-Section Problem 1. Mutual Exclusion - If process Pi is executing in its critical section, then no other processes can be executing in their critical sections. 2. Progress - If no process is executing in its critical section and there exist some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely. 3. Bounded Waiting - A bound must exist on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted. Assume that each process executes at a nonzero speed No assumption concerning relative speed of the N processes CSS 430: Operating Systems - Process Synchronization 15 A simple solution • Assume: – 2 processes, P0 & P1 – 2 shared variables int turn; // if 0, P0’s turn; if 1, P1’s turn boolean flag[2]; // if flag[i], Pi wants to enter CS – LOAD (LDA) & STORE (STA) are atomic • i.e., assignment statements are indivisible • How would you implement this? CSS 430: Operating Systems - Process Synchronization 16 Peterson’s Solution entry section exit section while ( true ) { flag[i] = true; turn = j; while ( flag[j] && turn == j ) ; // busy wait // critical section // … flag[i] = false; // remainder section // … } CSS 430: Operating Systems - Process Synchronization 17 Peterson’s Solution entry section exit section while ( true ) { flag[i] = true; turn = j; while ( flag[j] && turn == j ) ; // busy wait // critical section // … flag[i] = false; // remainder section // … } Software-based solution CSS 430: Operating Systems - Process Synchronization 18 Synchronization via Locks Hardware-based solution CSS 430: Operating Systems - Process Synchronization 19 Synchronization via Hardware • Disable interrupts during CS – Preempt preemption – Feasible on uniprocessors – What about multiprocessors? • Atomic machine instructions – Indivisible (non-interruptable) – Examples: • Test & modify word • Swap two words CSS 430: Operating Systems - Process Synchronization 20 S/W abstraction for H/W Synch. public class HardwareData { private boolean value = false; public HardwareData( boolean initialValue ) { this.value = initialValue; } public boolean get() { return value; } public void set( boolean newValue ) { this.value = newValue; } public boolean getAndSet( boolean newValue ) { boolean oldValue = this.get(); this.set( newValue ); return oldValue; } public void swap( HardwareData other ) { boolean temp = this.get(); this.set( other.get() ); other.set( temp ); } } /usr/apps/CSS430/examples/os-book/ch6/hardware CSS 430: Operating Systems - Process Synchronization 21 Using getAndSet() public class Worker1 implements Runnable { private String name; private HardwareData mutex; public Worker1( String name, HardwareData mutex ) { this.name = name; this.mutex = mutex; } public void run() { // mutex initialized when Worker instantiated while ( true ) { System.out.println( name + " wants to enter CS" ); while ( mutex.getAndSet( true ) ) Thread.yield(); System.out.println( name + " is in critical section" ); MutualExclusionUtilities.criticalSection( name ); System.out.println( name + " is out of critical section" ); mutex.set( false ); MutualExclusionUtilities.remainderSection( name ); } } } Do you [fore]see any problems? /usr/apps/CSS430/examples/os-book/ch6/hardware CSS 430: Operating Systems - Process Synchronization 22 Using getAndSet() public class Worker1 implements Runnable { Does this enforce private String name; mutual exclusion? private HardwareData mutex; public Worker1( String name, HardwareData mutex ) { this.name = name; this.mutex = mutex; } public void run() { // mutex initialized when Worker instantiated while ( true ) { System.out.println( name + " wants to enter CS" ); while ( mutex.getAndSet( true ) ) Thread.yield(); System.out.println( name + " is in critical section" ); MutualExclusionUtilities.criticalSection( name ); System.out.println( name + " is out of critical section" ); mutex.set( false ); MutualExclusionUtilities.remainderSection( name ); } } } Do you [fore]see any problems? /usr/apps/CSS430/examples/os-book/ch6/hardware CSS 430: Operating Systems - Process Synchronization 23 Using swap() public class Worker2 implements Runnable { private String name; private HardwareData mutex; public Worker2( String name, HardwareData mutex ) { this.name = name; this.mutex = mutex; } public void run() { key = new HardwareData( true ); while ( true ) { System.out.println( name + " wants to enter CS" ); key.set( true ); do { mutex.swap( key ); } while ( key.get() ); System.out.println( name + " is in critical section" ); MutualExclusionUtilities.criticalSection( name ); System.out.println( name + " is out of critical section" ); mutex.set( false ); MutualExclusionUtilities.remainderSection( name ); } } } /usr/apps/CSS430/examples/os-book/ch6/hardware CSS 430: Operating Systems - Process Synchronization 24 Using swap() public class Worker2 implements Runnable { private String name; private HardwareData mutex; public Worker2( String name, HardwareData mutex ) { this.name = name; this.mutex = mutex; } public void run() { key = new HardwareData( true ); while ( true ) { System.out.println( name + " wants to enter CS" ); key.set( true ); do { mutex.swap( key ); } while ( key.get() ); System.out.println( name + " is in critical section" ); MutualExclusionUtilities.criticalSection( name ); System.out.println( name + " is out of critical section" ); mutex.set( false ); MutualExclusionUtilities.remainderSection( name ); } } } Do you [fore]see any new problems? /usr/apps/CSS430/examples/os-book/ch6/hardware CSS 430: Operating Systems - Process Synchronization 25 Using swap() public class Worker2 implements Runnable { private String name; private HardwareData mutex; public Worker2( String name, HardwareData mutex ) { this.name = name; this.mutex = mutex; } public void run() { key = new HardwareData( true ); Does not while ( true ) { work & play System.out.println( name + " wants to enter CS" ); key.set( true ); well w/ others do { mutex.swap( key ); } while ( key.get() ); System.out.println( name + " is in critical section" ); MutualExclusionUtilities.criticalSection( name ); System.out.println( name + " is out of critical section" ); mutex.set( false ); MutualExclusionUtilities.remainderSection( name ); } } } Do you [fore]see any new problems? /usr/apps/CSS430/examples/os-book/ch6/hardware CSS 430: Operating Systems - Process Synchronization 26 Using swap() public class Worker2 implements Runnable { private String name; private HardwareData mutex; public Worker2( String name, HardwareData mutex ) { this.name = name; this.mutex = mutex; } public void run() { key = new HardwareData( true ); Rewrite while ( true ) { using while System.out.println( name + " wants to enter CS" ); & yield() key.set( true ); while ( key.get() ) { Thread.yield(); mutex.swap( key ); } System.out.println( name + " is in critical section" ); MutualExclusionUtilities.criticalSection( name ); System.out.println( name + " is out of critical section" ); mutex.set( false ); MutualExclusionUtilities.remainderSection( name ); } } } /usr/apps/CSS430/examples/os-book/ch6/hardware CSS 430: Operating Systems - Process Synchronization 27 Using AtomicBoolean public class Worker1a implements Runnable { private String name; private AtomicBoolean mutex; public Worker1a( String name, AtomicBoolean mutex ) { this.name = name; this.mutex = mutex; } public void run() { // mutex initialized when Worker instantiated while ( true ) { System.out.println( name + " wants to enter CS" ); while ( mutex.getAndSet( true ) ) Thread.yield(); System.out.println( name + " is in critical section" ); MutualExclusionUtilities.criticalSection( name ); System.out.println( name + " is out of critical section" ); mutex.set( false ); MutualExclusionUtilities.remainderSection( name ); } } } /usr/apps/CSS430/examples/os-book/ch6/hardware CSS 430: Operating Systems - Process Synchronization 28 AtomicBooleanFactory import java.util.concurrent.atomic.AtomicBoolean; public class AtomicBooleanFactory { public static void main( String args[] ) { AtomicBoolean lock = new AtomicBoolean( false ); Thread[] worker = new Thread[5]; for ( int i = 0; i < 5; i++ ) { worker[i] = new Thread( new Worker1a( String.format( "worker %d", i ), lock ) ); worker[i].start(); } } } /usr/apps/CSS430/examples/os-book/ch6/hardware CSS 430: Operating Systems - Process Synchronization 29 Semaphores • Software-based synchronization mechanism – Avoids busy waiting • Semaphore S – integer variable – Value # of shared resources available – Binary (1), aka mutex locks, or Counting (> 1) • [Only] 2 indivisible operations modify S: – acquire() & release() – Originally: • P() [proberen, “test”] & V() [verhogen, “increment”] CSS 430: Operating Systems - Process Synchronization 30 Semaphores • Indivisible testing & modification of value CSS 430: Operating Systems - Process Synchronization 31 Semaphores for Synchronization • Critical section protection, other synchronization Process P0: Process P1: CSS 430: Operating Systems - Process Synchronization 32 getAndSet vs. Semaphores private AtomicBoolean mutex; … while ( mutex.getAndSet( true ) ) Thread.yield(); // critical section mutex.set( false ); // remainder section … CSS 430: Operating Systems - Process Synchronization 33 Busy waiting vs. Blocking CSS 430: Operating Systems - Process Synchronization 34 Busy waiting vs. Blocking CSS 430: Operating Systems - Process Synchronization 35 Blocking & Waking up CSS 430: Operating Systems - Process Synchronization 36 A Semaphore Implementation public class Semaphore { private int value; public Semaphore( int initialValue this.value = initialValue; } public synchronized void acquire() while ( value <= 0 ) { try { wait(); } catch ( InterruptedException } } value--; } public synchronized void release() ++value; notify(); } ) { { e ) { { } /usr/apps/CSS430/examples/os-book/ch6/semaphores CSS 430: Operating Systems - Process Synchronization 37 Java Thread Transitions http://etutorials.org/cert/java+certification/Chapt er+9.+Threads/9.5+Thread+Transitions/ CSS 430: Operating Systems - Process Synchronization 38 AtomicBoolean vs. Semaphore public class Worker1a implements Runnable { private String name; private AtomicBoolean mutex; public Worker1a( String name, AtomicBoolean mutex ) { this.name = name; this.mutex = mutex; } public void run() { // mutex initialized when Worker instantiated while ( true ) { System.out.println( name + " wants to enter CS" ); while ( mutex.getAndSet( true ) ) Thread.yield(); System.out.println( name + " is in critical section" ); MutualExclusionUtilities.criticalSection( name ); System.out.println( name + " is out of critical section" ); mutex.set( false ); MutualExclusionUtilities.remainderSection( name ); } } } /usr/apps/CSS430/examples/os-book/ch6/hardware CSS 430: Operating Systems - Process Synchronization 39 A Semaphore Example import java.util.concurrent.Semaphore; public class Worker implements Runnable { private Semaphore sem; private String name; public Worker( String name, Semaphore sem ) { this.name = name; this.sem = sem; } public void run() { while ( true ) { sem.acquire(); System.out.println( name + " is in critical section" ); MutualExclusionUtilities.criticalSection( name ); System.out.println( name + " is out of critical section" ); sem.release(); MutualExclusionUtilities.remainderSection( name ); } } } /usr/apps/CSS430/examples/os-book/ch6/semaphores CSS 430: Operating Systems - Process Synchronization 40 A Semaphore Example import java.util.concurrent.Semaphore; public class SemaphoreFactory { public static void main( String args[] ) { Semaphore sem = new Semaphore( 1 ); Thread[] bee = new Thread[5]; for ( int i = 0; i < 5; i++ ) bee[i] = new Thread( new Worker( String.format( "worker %d", i), sem ) ); for ( int i = 0; i < 5; i++ ) bee[i].start(); } } /usr/apps/CSS430/examples/os-book/ch6/semaphores CSS 430: Operating Systems - Process Synchronization 41 Deadlock & Starvation • Deadlock – two or more processes are waiting indefinitely for an event that can be caused by only one of the waiting processes • Let S and Q be two semaphores initialized to 1 • Starvation – indefinite blocking. A process may never be removed from the semaphore queue in which it is suspended. CSS 430: Operating Systems - Process Synchronization 42 Priority Inversion • 3 processes, 3 priorities: L < M < H • Scenario: – L has acquired shared resource R – H wants to acquire R – M preempts L (which still has R) • M has effectively preempted H • Solution? CSS 430: Operating Systems - Process Synchronization 43 Priority Inversion • 3 processes, 3 priorities: L < M < H • Scenario: – L has acquired shared resource R – H wants to acquire R – M preempts L (which still has R) • M has effectively preempted H • Solution: – Priority-inheritance protocol CSS 430: Operating Systems - Process Synchronization 44 Classic SynchronizationProblems • Representative of large class of problems • Used to test prospective solutions • Examples: – Bounded Buffer Problem – Readers and Writers Problem – Dining Philosophers Problem CSS 430: Operating Systems - Process Synchronization 45 Bounded Buffer Problem • N buffers, each can hold one item • Solution: 3 semaphores – mutex (1): provide mutual exclusion – full (0): # of full buffers – empty (N): # of empty buffers CSS 430: Operating Systems - Process Synchronization 46 Bounded Buffer Problem CSS 430: Operating Systems - Process Synchronization 47 Bounded Buffer insert() CSS 430: Operating Systems - Process Synchronization 48 Bounded Buffer remove() CSS 430: Operating Systems - Process Synchronization 49 Bounded Buffer producer CSS 430: Operating Systems - Process Synchronization 50 Bounded Buffer consumer CSS 430: Operating Systems - Process Synchronization 51 Bounded Buffer factory CSS 430: Operating Systems - Process Synchronization 52 Readers & Writers Problem • Data set shared among concurrent processes – Readers: only read the data set; do not modify it – Writers: can both read and write • Multiple readers can concurrently read • If one writer has access, no one else can have access • Problem: if reader(s) and writer(s) both want access, who gets to go (& when)? CSS 430: Operating Systems - Process Synchronization 53 Readers & Writers Problem • Potential solutions? CSS 430: Operating Systems - Process Synchronization 54 Readers & Writers Problem • Potential solutions: – New writers waits for readers – New readers wait for writers • Any problems with these “solutions”? CSS 430: Operating Systems - Process Synchronization 55 Readers & Writers Problem • Potential solutions: – New writers waits for readers – New readers wait for writers • Any problems with these “solutions”? – starvation • Approach: – – – – Database Semaphore mutex (1): protect readerCount Semaphore db (1): protect database updates Integer readerCount (0): # of current readers CSS 430: Operating Systems - Process Synchronization 56 Readers & Writers Problem Interface for read-write locks CSS 430: Operating Systems - Process Synchronization 57 Readers & Writers Problem • The structure of a writer CSS 430: Operating Systems - Process Synchronization 58 Readers & Writers Problem • The structure of a reader CSS 430: Operating Systems - Process Synchronization 59 Readers & Writers Problem • The database CSS 430: Operating Systems - Process Synchronization 60 Readers & Writers Problem • Reader methods CSS 430: Operating Systems - Process Synchronization 61 Readers & Writers Problem • Writer methods CSS 430: Operating Systems - Process Synchronization 62 Dining Philosophers Problem • Philosopher’s Life: a cycle of – Thinking (no interaction) – Getting Hungry • Pick up one chopstick at a time – Eating • Eat • Put down both chopsticks CSS 430: Operating Systems - Process Synchronization 63 Dining Philosophers Problem • Philosopher’s Life: a cycle of – Thinking (no interaction) – Getting Hungry • Pick up one chopstick at a time – Eating • Eat • Put down both chopsticks • Potential problems? CSS 430: Operating Systems - Process Synchronization 64 Dining Philosophers Problem • Philosopher’s Life: a cycle of – Thinking (no interaction) – Getting Hungry • Pick up one chopstick at a time – Eating • Eat • Put down both chopsticks • Potential problems: – Starvation and Deadlock CSS 430: Operating Systems - Process Synchronization 65 Dining Philosophers Problem • Philosopher’s Life: a cycle of – Thinking (no interaction) – Getting Hungry • Pick up one chopstick at a time – Eating • Eat • Put down both chopsticks • Potential problems: – Starvation and Deadlock Edsger Wybe Dijkstra CSS 430: Operating Systems - Process Synchronization 66 Dining Philosophers Problem • Potential solutions? CSS 430: Operating Systems - Process Synchronization 67 Dining Philosophers Problem • Potential solutions (deadlock): – Restrict # of philosophers at table – Allow pickup only if both available – Asymmetry: • Odd philosopher pick up left, then right • Even philosopher pick up right, then left • Note: deadlock-free != starvation-free • Approach: – Bowl of rice (data set) – Semaphore chopStick [5] initialized to 1 CSS 430: Operating Systems - Process Synchronization 68 Dining Philosophers Problem • The structure of Philosopher i: CSS 430: Operating Systems - Process Synchronization 69 Problems with Semaphores mutex.release(); mutex.acquire(); mutex.acquire(); // critical // section // critical // section // critical // section mutex.acquire(); mutex.acquire(); // … • Might arise from – Honest programming error – Intentionally uncooperative programming CSS 430: Operating Systems - Process Synchronization // … // critical // section mutex.release() 70 Monitors • A high-level abstraction that provides a convenient and effective mechanism for process synchronization • Only one process may be active within the monitor at a time CSS 430: Operating Systems - Process Synchronization 71 Syntax of a Monitor CSS 430: Operating Systems - Process Synchronization 72 Schematic view of a Monitor CSS 430: Operating Systems - Process Synchronization 73 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 () CSS 430: Operating Systems - Process Synchronization 74 Monitor with Condition Variables CSS 430: Operating Systems - Process Synchronization 75 Solution to Dining Philosophers CSS 430: Operating Systems - Process Synchronization 76 Solution to Dining Philosophers • Each philosopher I invokes the operations takeForks(i) and returnForks(i) in the following sequence: dp.takeForks (i) EAT dp.returnForks (i) CSS 430: Operating Systems - Process Synchronization 77 Java Synchronization • Java provides synchronization at the language-level – “Thread-safe” • Each Java object has an associated lock. • This lock is acquired by invoking a synchronized method. • This lock is released when exiting the synchronized method. • Threads waiting to acquire the object lock are placed in the entry set for the object lock. CSS 430: Operating Systems - Process Synchronization 78 Java Synchronization • When a thread invokes wait(): 1. It releases the object lock 2. Its state is set to Blocked 3. It is placed in the wait set for the object • When a thread invokes notify(): 1. A thread T from the wait set is selected 2. T is moved from the wait set to the entry set 3. The state of T is set to Runnable CSS 430: Operating Systems - Process Synchronization 79 Java Synchronization CSS 430: Operating Systems - Process Synchronization 80 Java Synchronization – wait/notify Synchronized remove() method – Correct! CSS 430: Operating Systems - Process Synchronization 81 Java Synchronization - Bounded Buffer CSS 430: Operating Systems - Process Synchronization 82 Java Synchronization • The call to notify() selects an aribitrary thread from the wait set. It is possible the selected thread is in fact not waiting upon the condition for which it was notified. • The call notifyAll() selects all threads in the wait set and moves them to the entry set. • In general, notifyAll() is a more conservative strategy than notify(). CSS 430: Operating Systems - Process Synchronization 83 Java Synchronization notify() may not notify the correct thread! CSS 430: Operating Systems - Process Synchronization 84 Java Synchronization - ReadersWriters CSS 430: Operating Systems - Process Synchronization 85 Java Synchronization - ReadersWriters CSS 430: Operating Systems - Process Synchronization 86 Java Synchronization - ReadersWriters Methods called by writers CSS 430: Operating Systems - Process Synchronization 87 Java Synchronization Rather than synchronizing an entire method, Block synchronization allows blocks of code to be declared as synchronized CSS 430: Operating Systems - Process Synchronization 88 Java Synchronization Block synchronization using wait()/notify() CSS 430: Operating Systems - Process Synchronization 89 Concurrency Features in Java 5 • Prior to Java 5, the only concurrency features in Java were Using synchronized/wait/notify. • Beginning with Java 5, new features were added to the API: Reentrant Locks Semaphores Condition Variables CSS 430: Operating Systems - Process Synchronization 90 Concurrency Features in Java 5 Reentrant Locks CSS 430: Operating Systems - Process Synchronization 91 Concurrency Features in Java 5 Semaphores CSS 430: Operating Systems - Process Synchronization 92 Concurrency Features in Java 5 A condition variable is created by first creating a ReentrantLock and invoking its newCondition() method: Once this is done, it is possible to invoke the await() and signal() methods CSS 430: Operating Systems - Process Synchronization 93 Concurrency Features in Java 5 CSS 430: Operating Systems - Process Synchronization 94