ch6 - Chapter 6: Process Synchronization Chapter Adapted to...

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

View Full Document Right Arrow Icon
Chapter 6: Process Synchronization Chapter 6: Process Synchronization Adapted to COP4610 by Robert van Engelen
Background image of page 1

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

View Full DocumentRight Arrow Icon
6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Background Background Concurrent access to shared data may result in data inconsistency Race condition - when two or more processes attempt to modify a shared variable and the outcome depends on the order in which the access takes place Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes Process synchronization and coordination
Background image of page 2
6.3 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Example: Consumer-Producer Problem Example: Consumer-Producer Problem Suppose that we wanted to provide a solution to the consumer-producer problem with a shared buffer We can do so by having an integer counter that keeps track of the number of items in the buffer Initially, counter is set to 0 The counter is incremented by the producer after it produces a new item The counter is decremented by the consumer after it consumes an item counter items Shared buffer Consumer takes items and decrements counter Producer adds items and increments counter
Background image of page 3

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

View Full DocumentRight Arrow Icon
6.4 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Producer Producer while (true) {              /* produce an item and put in nextProduced */      while (counter == BUFFER_SIZE) ; /* buffer is full: do nothing */        buffer [in] = nextProduced;  in = (in + 1) % BUFFER_SIZE;  counter++; }   
Background image of page 4
6.5 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Consumer Consumer    while (true)  {                     while (counter == 0)         ; /* buffer is empty: do nothing */              nextConsumed = buffer[out];     out = (out + 1) % BUFFER_SIZE;         counter--;         /* consume the item in nextConsumed */ }
Background image of page 5

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

View Full DocumentRight Arrow Icon
6.6 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Race Condition Race Condition counter++ could be implemented as register1 = counter  register1 = register1 + 1  counter = register1 counter-- could be implemented as register2 = counter  register2 = register2 - 1  counter = register2 Consider this execution interleaving with “counter = 5” initially: T 0 : producer execute register1 = counter {register1 = 5} T 1 : producer execute register1 = register1 + 1 {register1 = 6} T 2 : consumer execute register2 = counter {register2 = 5} T 3 : consumer execute register2 = register2 - 1 {register2 = 4} T 4 : producer execute counter = register1 {counter = 6 } T 5 : consumer execute counter = register2 {counter = 4}
Background image of page 6
6.7 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7
Background image of page 7

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

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

This note was uploaded on 10/11/2011 for the course COP 4610 taught by Professor Robertengelen during the Spring '11 term at FSU.

Page1 / 64

ch6 - Chapter 6: Process Synchronization Chapter Adapted to...

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

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