●●● ●●●● 6. Process synchronization 9989 ●●●0 Objectives ●●●● To introduce the critical-section problem, whose solutions can be used to ensure the consistency of shared data To present both software and hardware solutions of the critical-section problem o introduce the concept of an atomic transaction and describe mechanisms to ensure atomicity
2 6. Process synchronization ⚫ Objectives ⚫ To introduce the critical-section problem, whose solutions can be used to ensure the consistency of shared data ⚫ To present both software and hardware solutions of the critical-section problem ⚫ To introduce the concept of an atomic transaction and describe mechanisms to ensure atomicity
●●● ●●●● 6. Process synchronization 9988 ●●●● e 6.1 Background ●●●● o 6.2 The Critical-Section Problem °6.3 Peterson’sSo|uton 6.4 Synchronization Hardware 6.5 Semaphores o 6.6 Classic Problems of synchronization ●67 Monitors e 6.8 Synchronization Examples o 6.9 Atomic transactions
3 6. Process synchronization ⚫ 6.1 Background ⚫ 6.2 The Critical-Section Problem ⚫ 6.3 Peterson’s Solution ⚫ 6.4 Synchronization Hardware ⚫ 6.5 Semaphores ⚫ 6.6 Classic Problems of Synchronization ⚫ 6.7 Monitors ⚫ 6.8 Synchronization Examples ⚫ 6.9 Atomic Transactions
●●● ●●●● 6.1 Background ●●●●● ●●●● ●●0●● ●●●0 o Concurrent access to shared data may result in data inconsistency o A example: bounded buffer problem with shared data o Producer while(true)( produce an item in nextProduced * while(count== BUFFER SIZE); /do nothing buffer [in]= nextProduced; in=(in+1)% BUFFER SIZE count++
4 6.1 Background ⚫ Concurrent access to shared data may result in data inconsistency ⚫ A example: bounded buffer problem with shared data ⚫ Producer while (true) { /* produce an item in nextProduced */ while (count == BUFFER_SIZE); // do nothing buffer [in] = nextProduced; in = (in + 1) % BUFFER_SIZE; count++; }
●●● ●●●● 6.1 Background ●●●●● ●●●● ●●0●● ●●●● Consumer ●●●● while(true)t while(count==0); //do nothing nextConsumed= buffer[out] out=out+ 1)% BUFFER SIZE count--, /* consume the item in nextconsumed*/ o The statements /* count=5*/ count++e l/Producer count-- ∥/ Consumer
5 6.1 Background ⚫ Consumer while (true) { while (count == 0); // do nothing nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; count--; /* consume the item in nextConsumed*/ } ⚫ The statements /* count=5 */ count++; //Producer count--; // Consumer
●●● ●●●● 6.1 Background ●●●●● ●●●● ●●0●● ●●●● o count++ could be implemented as ●●●● register1 count register1 register1+ 1 count= register1 o count--could be implemented as register2= count register2 register2 -1 count= register2 Remember that the contents of the register will be saved and restored by the interrupt
6 6.1 Background ⚫ count++ could be implemented as register1 = count register1 = register1 + 1 count = register1 ⚫ count-- could be implemented as register2 = count register2 = register2 - 1 count = register2 Remember that the contents of the register will be saved and restored by the interrupt