OPERATING SYSTEMS 1 龚玲 lgong@sjtu.edu.cn
OPERATING SYSTEMS 龚玲 lgong@sjtu.edu.cn 1
Chapter 6:Process Synchronization
Chapter 6: Process Synchronization
REVIEW o Race Condition ohttp://wenku.baidu.com/course/study/77f1dcccda38376ba flfae94#665ea0c7aa00b52acfc7ca94
REVIEW Race Condition http://wenku.baidu.com/course/study/77f1dcccda38376ba f1fae94#665ea0c7aa00b52acfc7ca94
Goals for today o Background oThe Critical-Section Problem o Peterson's Solution o Synchronization Hardware
Goals for today Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware
Background-Race Conditions o Two processes want to access shared memory at the same time. Spooler directory 4 abc out =4 5 Process A prog.c 6 prog.n in =7 Process B 5
Background-Race Conditions Two processes want to access shared memory at the same time. 5
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
Producer while (count ==BUFFER_SIZE) ;/do nothing /add an item to the buffer ++count; buffer[in]item; in (in +1)%BUFFER _SIZE;
Producer