This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: CS-350: Fundamentals of Computing Systems Page 1 of 13Lecture Notes Azer Bestavros. All rights reserved. Reproduction or copying (electronic or otherwise) is expressly forbidden except for students enrolled in CS-350. Concurrency and Synchronization: Two-Process Mutual Exclusion So far our consideration of resource management has made a fundamental assumptionthat the processes competing for a shared resource (e.g., CPU, disk, or network) do not intend to change the state of the resource they share. They merely need to use that resource. In computing systems, it is tempting to think of resources as devices. But in general, a resource could also be a data structure or a piece of software. 15+1+1 = 16? Consider, for example, a data structure that consists of a simple integer. Lets assume that this integer is used by a set of concurrent processes to count the number of events observed by the various processes. Now consider what could happen when multiple concurrent processes attempt to access that counter by adding one to it. To do so, a process will need to read the memory location in which that integer counter is stored, add one to that value, and then store that value back in memory. If we only have one such process, then clearly adding one to the counter will always yield the desirable result of incrementing the value of the counter by one. Now consider what could happen if we allow multiple (say two) processes to access the shared counter data structure in an unrestricted manner? For the sake of discussion, lets assume that the counter C had a value of 15. First we note that, due to the fact the two processes are executing concurrently, the execution of their individual instructions could be interleaved. Consider the following interleaved execution of the set of three identical instructions for process P1 and for process P2. StepProcess P1Process P21 Read C into local variable X; 2 Read C into local variable X; 3 X = X+1; 4 Store in C the value of X; 5 X = X+1; 6 Store in C the value of X; Figure 1 Example execution of two concurrent processes accessing a shared counter C resulting in an incorrect result. What would be the value stored in the shared variable C? Clearly, the intention was for P1 to increment by 1 and for P2 to increment by 1, which should yield a result of 17 (assuming an CS-350: Fundamentals of Computing Systems Page 2 of 13Lecture Notes Azer Bestavros. All rights reserved. Reproduction or copying (electronic or otherwise) is expressly forbidden except for students enrolled in CS-350. initial value of 15). The above interleaved execution of P1 and P2 does not yield that answer, but rather it yields an answer of 16....
View Full Document
- Spring '09