Process Interactions o Competition:The Critical Problem x=0; cobegin p1:… x=x+1 1 p2:… x=x+1: coend o x should be 2 after both processes execute 6
Process Interactions Competition: The Critical Problem x = 0; cobegin p1: … x = x + 1; … // p2: … x = x + 1; … coend x should be 2 after both processes execute 6
The Critical Problem o Interleaved execution (due to parallel processing or context switching): p1:R1= X p2: R1=R1+1; R2 x; x=R1; R2=R2 +1 x=R2; ox has only been incremented once The first update (=R1)is lost
The Critical Problem Interleaved execution (due to parallel processing or context switching): p1: R1 = x; p2: … R1 = R1 + 1; R2 = x; x = R1 ; R2 = R2 + 1; … x = R2; x has only been incremented once The first update (x=R1) is lost. 7
Race condition oSeveral processes access and manipulate the same data concurrently and the outcome of the execution depends on the particular order in which the access takes place
Race condition Several processes access and manipulate the same data concurrently and the outcome of the execution depends on the particular order in which the access takes place
Background o Concurrent access to shared data may result in data inconsistency o Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes Suppose that we wanted to provide a solution to the consumer-producer problem that fills all the buffers.We can do so by having an integer count that keeps track of the number of full buffers. 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
Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes Suppose that we wanted to provide a solution to the consumer-producer problem that fills all the buffers. We can do so by having an integer count that keeps track of the number of full buffers. 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