
OperatingSystemsCh5Process Communication & Synchronization-Part 21
Operating Systems Ch5 Process Communication & Synchronization -Part 2 1

IPC problem: Race condition2
2 P P P IPC problem: Race condition

Evil source:the shared objectsdataProcessProcessPipe is implemented with thethought that there may beread()write()more than oneprocessaccessing it"at the same time"For shared memory and files,Filestructureinthekernelconcurrent access may yieldunpredictable outcomesHard Disk3
Evil source: the shared objects • Pipe is implemented with the thought that there may be more than one process accessing it “at the same time” • For shared memory and files, concurrent access may yield unpredictable outcomes 3 Process Process read() write() File structure in the kernel data Hard Disk

Understanding the problem..The ScenarioHigh-levellanguageforProgramAattach to the shared memory X;12Shared memoryadd 1o to X;3exit;Value= 10High-levellanguageforProgramBminus 10;add 10;1attach to the sharedmemoryX;2minus10 to X;ProcessBProcessA3exit;It maybe10,0or20,canyoubelieve it?Guess whatthefinal result shouldbe?4
Understanding the problem. 4 Process A Process B Shared memory Value = 10 add 10; minus 10; The Scenario High-level language for Program A 1 attach to the shared memory X; 2 add 10 to X; 3 exit; Guess what the final result should be? High-level language for Program B 1 attach to the shared memory X; 2 minus 10 to X; 3 exit; It may be 10, 0 or 20, can you believe it?

Understanding the problem..The ScenarioHigh-levellanguageforProgramA1attachtotheshared memory X;2add10toX;Shared memory3exit;Value=10High-levellanguageforProgramBminus 10;add 10;1attach totheshared memory X:2minus10toX;ProcessAProcessB3exit;Remember the flow of executing a program and the system hierarchy?5
Understanding the problem. 5 Process A Process B Shared memory Value = 10 add 10; minus 10; The Scenario High-level language for Program A 1 attach to the shared memory X; 2 add 10 to X; 3 exit; High-level language for Program B 1 attach to the shared memory X; 2 minus 10 to X; 3 exit; Remember the flow of executing a program and the system hierarchy?

Understanding the problem..The ScenarioHigh-levellanguageforProgramAattach to the shared memory X;12Sharedmemoryadd 1o to X;This operation3exit;is not atomicValue= 10Partial low-level languageforProgramAminus 10;add 10;1attach to the shared memory X;ProcessAProcessB2.1load memory X to register A;2.2add 1o to register A;2.3write register A to memory X3exit;Guess what? Thiscodeblockisevil!6
Understanding the problem. 6 Process A Process B Shared memory Value = 10 add 10; minus 10; The Scenario High-level language for Program A 1 attach to the shared memory X; 2 add 10 to X; 3 exit; Partial low-level language for Program A 1 attach to the shared memory X; . 2.1 load memory X to register A; 2.2 add 10 to register A; 2.3 write register A to memory X; . 3 exit; Guess what? This code block is evil! This operation is not atomic

Understanding the problem..Shared memory-XValue = 10State:State:RegisterRegister AAReadyValue=0Value=0Ready2.1load memory X to2.1load memory X toregister A;register A;2.22.2add 1o to register A;minus 1o from register A;2.32.3write register A towrite register A tomemory X;memory X;ProcessAProcessBThe initial setting7
Understanding the problem. 7 Process B Shared memory - X Value = 10 State: Ready Register A Value = 0 Process A State: Ready Register A Value = 0 2.1 load memory X to register A; 2.2 add 10 to register A; 2.3 write register A to memory X; 2.1 load memory X to register A; 2.2 minus 10 from register A; 2.3 write register A to memory X; The initial setting

ExecutionFlow#18
8 Execution Flow #1

Problem not yet arise..Shared memory-XValue = 10State:State:Register ARegister AValue =10RunningValue=0Ready2.1load memory X to2.1load memory Xtoregister A;register A;2.22.2minus 1o from register A;add 1o to register A;2.3write register A to2.3write register A tomemory X;memory X;ProcessAProcessBIntelCore"17ExecutionFlow#1,Step19
Problem not yet arise. 9 Process B Shared memory - X Value = 10 State: Running Register A Value = 10 Process A 2.1 load memory X to register A; 2.2 minus 10 from register A; 2.3 write register A to memory X; State: Ready Register A Value = 0 2.1 load memory X to register A; 2.2 add 10 to register A; 2.3 write register A to memory X; Execution Flow #1, Step 1 1

Problem not yet arise..Shared memory-XValue = 10State:State:Register ARegister ARunningValue=20Value=0Ready2.1load memory X to2.1load memory X toregister A;register A;2.2minus 1o from register A;2.2add 1o to register A;2.3write register A to2.3write register A tomemory X;memory X;ProcessAProcessBIntelCore"17ExecutionFlow#1,Step210
Problem not yet arise. 10 Process B Shared memory - X Value = 10 State: Running Register A Value = 20 Process A 2.1 load memory X to register A; 2.2 minus 10 from register A; 2.3 write register A to memory X; State: Ready Register A Value = 0 2.1 load memory X to register A; 2.2 add 10 to register A; 2.3 write register A to memory X; Execution Flow #1, Step 2 1 2