08-Process_Synchronization_I_2spp

08-Process_Synchronization_I_2spp - 1 CSE 421/521 -...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 CSE 421/521 - Operating Systems Fall 2011 Tevfik Ko ar University at Buffalo September 22 nd , 2011 Lecture - VIII Process Synchronization - I 2 Roadmap Process Synchronization Race Conditions Critical-Section Problem Solutions to Critical Section Different Implementations Semaphores Classic Problems of Synchronization 3 Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes Consider consumer-producer problem: Initially, count is set to 0 It is incremented by the producer after it produces a new buffer and is decremented by the consumer after it consumes a buffer. 4 Producer: while (true){ /* produce an item and put in nextProduced while (count == BUFFER_SIZE) ; // do nothing buffer [in] = nextProduced; in = (in + 1) % BUFFER_SIZE; count++; } while (1) { while (count == 0) ; // do nothing nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; count--; } /* consume the item in nextConsumed Consumer: Shared Variables: count=0, buffer Race Condition Race condition : The situation where several processes access and manipulate shared data concurrently. The final value of the shared data depends upon which process finishes last. To prevent race conditions, concurrent processes must be synchronized. Ensure that only one process at a time is manipulating the variable counter . The statements count++; count--; must be performed atomically . Atomic operation means an operation without interruption. 5 6 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 execute register1 = count {register1 = 5} S1: producer execute register1 = register1 + 1 {register1 = 6} S2: consumer execute register2 = count {register2 = 5} S3: consumer execute register2 = register2 - 1 {register2 = 4} S4: producer execute count = register1 {count = 6 } S5: consumer execute count = register2 {count = 4} Race Condition 7 char chin, chout; //shared void echo() { do { chin = getchar(); chout = chin; putchar(chout); } while (...); } A char chin, chout; //shared void echo() { do { chin = getchar(); chout = chin; putchar(chout); } while (...); } B > ./echo Hello world! Hello world! Single-threaded echo Multithreaded echo (lucky) > ./echo Hello world! H e l lo wor l d !...
View Full Document

This document was uploaded on 10/01/2011.

Page1 / 14

08-Process_Synchronization_I_2spp - 1 CSE 421/521 -...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online