OSch6 - Chapter 6 Synchronization Synchronization 1 Outline...

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

View Full Document Right Arrow Icon
Synchronization 1 Synchronization 1 Chapter 6 Synchronization 2 Outline ! Background ! The Critical-Section Problem ! Peterson’s Solution ! Synchronization Hardware ! Semaphores ! Classic Problems of Synchronization ! Monitors
Background image of page 1

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

View Full DocumentRight Arrow Icon
Synchronization 2 3 Background ! A cooperating process is one that can affect or be affected by other processes executing in the system. " Cooperating # concurrent access to shared data. $ The data section of threads $ Shared memory among processes " May result in data inconsistency ! In this chapter, we discuss various mechanisms to ensure the orderly execution of cooperating processes. " Data consistency is maintained 4 Background (cont’d) ! In chapter 3, we present the concept of cooperating process by using the classical producer-consumer problem . " Producer process produces items on a shared, bounded buffer. " Consumer process consumes the produced information from the buffer. " The original solution allowed at most BUFFER_SIZE-1 items in the buffer. % modify the algorithm to use BUFFER_SIZE items % use counter sharable variable. while (TRUE) { /* produce an item and put in nextProduced */ while (count == BUFFER_SIZE) ; // do nothing buffer [in] = nextProduced; in = (in + 1) % BUFFER_SIZE; counter++; } while (TRUE) { while (count == 0) ; // do nothing nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; counter--; /* consume the item in nextConsumed */ } producer consumer
Background image of page 2
Synchronization 3 5 Background (cont’d) ! Although both the producer and consumer routines are correct separately, they may not function correctly when executed concurrently. " Suppose that the value of the variable counter is 5. " The producer and consumer processes execute the statements counter++ ” and “ counter-- ” concurrently. " Then the value of the variable of counter may be 4, 5, or 6!! ! Why? " The statement “ counter++ (or counter-- )” may be implemented in machine language as: register 1 = counter register 1 = register 1 + 1 counter = register 1 register 2 = counter register 2 = register 2 -1 counter = register 2 6 Background (cont’d) ! The concurrent execution of “ counter++ ” and “ counter-- ” is equivalent to a sequential execution where the lower-level statements presented previously are interleaved in some arbitrary order . ! For instance: T 0 : producer execute register 1 = count { register 1 = 5} T 1 : producer execute register 1 = register 1 + 1 { register 1 = 6} T 2 : consumer execute register 2 = count { register 2 = 5} T 3 : consumer execute register 2 = register 2 -1{ register 2 = 4} T 4 : producer execute count = register 1 { count = 6 } T 5 : consumer execute count = register 2 { count = 4 } ! We arrive at this incorrect state because we allowed both processes to manipulate the variable counter concurrently.
Background image of page 3

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

View Full DocumentRight Arrow Icon
Synchronization 4 7 Background (cont’d) ! Race condition: " Several processes access and manipulate the same data concurrently. " The outcome of the execution depends on the particular order in
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/01/2012 for the course CMP 426 taught by Professor Gwangs.jung during the Spring '12 term at CUNY Lehman.

Page1 / 39

OSch6 - Chapter 6 Synchronization Synchronization 1 Outline...

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