Race condition-the curse. The above scenario is called the race condition· A race condition means-the outcome of an execution depends on a particularorderin whichthe shared resourceisaccessedRemember: race condition is always a bad thing anddebugging race condition has no fun at all!- It may end up .... 99% of the executions are fine..1% of the executions are problematic21
Race condition – the curse • The above scenario is called the race condition. • A race condition means – the outcome of an execution depends on a particular order in which the shared resource is accessed. • Remember: race condition is always a bad thing and debugging race condition has no fun at all! – It may end up . • 99% of the executions are fine. • 1% of the executions are problematic. 21
Race condition -the curseForsharedmemoryandfiles,dataconcurrent access mayyieldProcessProcessunpredictableoutcomesRace conditionread()write()Common situation-ResourcesharingoccursfrequentlyinOs. EXP:Kernel DS maintaining a list of openedFile structurefiles, maintaining memory allocation,in the kernelprocess lists...-Multicorebringsanincreasedemphasison multithreading.Multiplethreadsshareglobalvariablesanddynamically allocated memoryHard DiskProcesssynchronizationisneeded22
Race condition – the curse • For shared memory and files, concurrent access may yield unpredictable outcomes – Race condition • Common situation – Resource sharing occurs frequently in OS • EXP: Kernel DS maintaining a list of opened files, maintaining memory allocation, process lists. – Multicore brings an increased emphasis on multithreading • Multiple threads share global variables and dynamically allocated memory • Process synchronization is needed 22 Process Process read() write() File structure in the kernel data Hard Disk
Topicsin Process Synchronizationldea:How to achieveDefine critical sectionCooperating Processesconcurrent accesses sufferHowtoimplementfromrace condition Four requirements Software-based proposalsSolution>Disabling interruptsProcess Sychronization>strictalternationpeterson's solutionGuaranteemutualexclusion7mutexlockSemaphore (best choice)LApplicationClassicproblemsSemaphore UsageProducer-consumerproblemAvoid deadlock Dining philosopherproblemReader-writerproblem23
Topics in Process Synchronization 23 Process Sychronization concurrent accesses suffer from race condition Guarantee mutual exclusion Idea: How to achieve Define critical section How to implement Four requirements Software-based proposals Disabling interrupts strict alternation peterson’s solution mutex lock Semaphore (best choice) Classic problems Producer-consumer problem Dining philosopher problem Reader-writer problem Cooperating Processes Semaphore Usage Avoid deadlock Solution Application
Inter-process communication (IpC)- Mutual exclusion what & how to achieve?Howto havepeace?24
24 P P P Inter-process communication (IPC) - Mutual exclusion - what & how to achieve? How to have peace?
MutualExclusionShared memoryTwoprocessesplayingwiththesameshared memoryis dangerous.minus 10;add10;Wewill facethecurse-racecondition.ProcessBProcessAThesolutioncanbesimple:WhenI'mplayingwiththesharedShared memorymemory,noone could touch it10;add 10;This is called mutual exclusionA set ofprocesseswouldnothavetheProcessBProcessAproblem of race condition if mutualexclusionisguaranteed.25
Mutual Exclusion 25 Process B Process A Shared memory add 10; minus 10; Process B Process A Shared memory add 10; minus 10; Two processes playing with the same shared memory is dangerous. We will face the curse - race condition. The solution can be simple: When I’m playing with the shared memory, no one could touch it. This is called mutual exclusion. A set of processes would not have the problem of race condition if mutual exclusion is guaranteed