cse 3320 operating systems - rangerranger.uta.edu/~jrao/cse3320/spring2020/slides/lec7_ipc.pdf ·...
TRANSCRIPT
![Page 1: CSE 3320 Operating Systems - Rangerranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC7_IPC.pdf · • Pick one fork at a time • How to prevent deadlock & starvation o Deadlock:](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8b4759bf5c317fdd55c891/html5/thumbnails/1.jpg)
CSE3320OperatingSystems
Synchronization
Jia RaoDepartmentofComputerScience and Engineering
http://ranger.uta.edu/~jrao
![Page 2: CSE 3320 Operating Systems - Rangerranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC7_IPC.pdf · • Pick one fork at a time • How to prevent deadlock & starvation o Deadlock:](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8b4759bf5c317fdd55c891/html5/thumbnails/2.jpg)
RecapoftheLastClass
• Multiprocessorschedulingo Twoimplementationsofthereadyqueue
o Loadbalancing
o Parallelprogramscheduling} Synchronizationsonshareddataandexecutionphases
} Causalityamongthreads
Inter-processorthreadcommunications
![Page 3: CSE 3320 Operating Systems - Rangerranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC7_IPC.pdf · • Pick one fork at a time • How to prevent deadlock & starvation o Deadlock:](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8b4759bf5c317fdd55c891/html5/thumbnails/3.jpg)
Inter-ProcessCommunication(IPC)• Three fundamental issues:
o How one process can pass information to another
o How to make sure two or more processes do not get into each other’s way when engaging in critical activities
o How to maintain proper sequencing when dependencies present
Howaboutinter-threadcommunication?
![Page 4: CSE 3320 Operating Systems - Rangerranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC7_IPC.pdf · • Pick one fork at a time • How to prevent deadlock & starvation o Deadlock:](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8b4759bf5c317fdd55c891/html5/thumbnails/4.jpg)
RaceConditions° Raceconditions:whentwoormoreprocesses/threadsare
readingorwritingsomeshareddata andthefinalresultsdependonwhorunspreciselywhen• Interrupts,interleavedoperations/execution
printer daemon
Dir[in] = X;in ++;
Dir[in] = Y;in ++;
![Page 5: CSE 3320 Operating Systems - Rangerranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC7_IPC.pdf · • Pick one fork at a time • How to prevent deadlock & starvation o Deadlock:](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8b4759bf5c317fdd55c891/html5/thumbnails/5.jpg)
MutualExclusionandCriticalRegions° Mutualexclusion:makessureifoneprocessisusinga
sharedvariableorfile,theotherprocesseswillbeexcludedfromdoingthesamething• Mainchallenge/issuetoOS:todesignappropriateprimitive
operationsforachievingmutualexclusion
° Criticalregions:thepartoftheprogramwherethesharedmemoryisaccessed
° Fourconditionstoprovidemutualexclusion• Notwoprocessessimultaneouslyincriticalregion• NoassumptionsmadeaboutspeedsornumbersofCPUs• Noprocessrunningoutsideitscriticalregionmayblockanother
process• Noprocessmustwaitforevertoenteritscriticalregion
![Page 6: CSE 3320 Operating Systems - Rangerranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC7_IPC.pdf · • Pick one fork at a time • How to prevent deadlock & starvation o Deadlock:](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8b4759bf5c317fdd55c891/html5/thumbnails/6.jpg)
MutualExclusionUsingCriticalRegions
Mutual exclusion using critical regions
![Page 7: CSE 3320 Operating Systems - Rangerranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC7_IPC.pdf · • Pick one fork at a time • How to prevent deadlock & starvation o Deadlock:](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8b4759bf5c317fdd55c891/html5/thumbnails/7.jpg)
MutualExclusionwithBusyWaiting
° Disabling interrupts: ° OS technique, not users’
° multi-CPU?
° Lock variables: ° test-set is a two-step process, not atomic
° Busy waiting: ° continuously testing a variable until some value appears
(spin lock)
![Page 8: CSE 3320 Operating Systems - Rangerranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC7_IPC.pdf · • Pick one fork at a time • How to prevent deadlock & starvation o Deadlock:](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8b4759bf5c317fdd55c891/html5/thumbnails/8.jpg)
BusyWaiting:StrictAlternation
0 1
What if P1’s noncritical_region() has lots more work than P0’s?
Proposed strict alternation solution to critical region problem(a) Process 0. (b) Process 1.
![Page 9: CSE 3320 Operating Systems - Rangerranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC7_IPC.pdf · • Pick one fork at a time • How to prevent deadlock & starvation o Deadlock:](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8b4759bf5c317fdd55c891/html5/thumbnails/9.jpg)
BusyWaiting:Peterson’s
Peterson's solution for achieving mutual exclusion
sharing
Differentfromstrictalternation
![Page 10: CSE 3320 Operating Systems - Rangerranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC7_IPC.pdf · • Pick one fork at a time • How to prevent deadlock & starvation o Deadlock:](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8b4759bf5c317fdd55c891/html5/thumbnails/10.jpg)
BusyWaiting:TSL
Entering and leaving a critical region using the TSL instruction
° TSL(TestandSetLock)
• Indivisible(atomic)operation,how?Hardware(multi-processor)
• HowtouseTSLtopreventtwoprocessesfromsimultaneouslyenteringtheircriticalregions?
![Page 11: CSE 3320 Operating Systems - Rangerranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC7_IPC.pdf · • Pick one fork at a time • How to prevent deadlock & starvation o Deadlock:](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8b4759bf5c317fdd55c891/html5/thumbnails/11.jpg)
SleepandWakeup° IssueIwithPeterson’s&TS:howtoavoidCPU-costlybusywaiting?
° IssueII:priorityinversionproblem• Considertwoprocesses,Hwith(strict)highpriorityandLwith(strict)
lowpriority,LisinitscriticalregionandHbecomesready;doesLhavechancetoleaveitscriticalregion?
° SomeIPCprimitivesthatblockinsteadofwastingCPUtimewhentheyarenotallowedtoentertheircriticalregions• Sleepandwakeup
![Page 12: CSE 3320 Operating Systems - Rangerranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC7_IPC.pdf · • Pick one fork at a time • How to prevent deadlock & starvation o Deadlock:](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8b4759bf5c317fdd55c891/html5/thumbnails/12.jpg)
SleepandWakeup– Producer-ConsumerProblem
Q1:Whatifthewakeupsignalsenttoanon-sleep(ready)process?
Q2: what is a wakeup waiting bit? Is one enough?
![Page 13: CSE 3320 Operating Systems - Rangerranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC7_IPC.pdf · • Pick one fork at a time • How to prevent deadlock & starvation o Deadlock:](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8b4759bf5c317fdd55c891/html5/thumbnails/13.jpg)
SemaphoresandP&VOperations° Semaphores:avariabletoindicatethe#ofpendingwakeups
° Downoperation(P;request): lock
• Checksifasemaphoreis>0,- ifso,itdecrementsthevalueandjustcontinue
- Otherwise,theprocessisputtosleepwithoutcompletingthedownforthemoment
° Up operation(V;release):unlock
• Incrementsthevalueofthesemaphore- ifoneormoreprocessesaresleepingonthesemaphore,oneofthemischosenbythesystem
(randomly)andallowedtocompleteitsdown(semaphorewillstillbe0)
° P&Voperationsareatomic,howtoimplement?
• SingleCPU:systemcalls,disablinginterruptstemporally
• MultipleCPUs:TSLhelp
![Page 14: CSE 3320 Operating Systems - Rangerranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC7_IPC.pdf · • Pick one fork at a time • How to prevent deadlock & starvation o Deadlock:](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8b4759bf5c317fdd55c891/html5/thumbnails/14.jpg)
TheProducer-consumerProblemw/Semaphores
UC.ColoradoSprings
Binary semaphores: if each process does a down before entering its critical region and an up just leaving it, mutual exclusion is achieved
For mutual exclusion and synchronization
![Page 15: CSE 3320 Operating Systems - Rangerranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC7_IPC.pdf · • Pick one fork at a time • How to prevent deadlock & starvation o Deadlock:](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8b4759bf5c317fdd55c891/html5/thumbnails/15.jpg)
Mutexes° Mutex:
• avariablethatcanbeinoneoftwostates:unlockedorlocked
• Asimplifiedversionofthesemaphores[0,1]
Give other chance to run so as to save self;What is mutex_trylock()?
![Page 16: CSE 3320 Operating Systems - Rangerranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC7_IPC.pdf · • Pick one fork at a time • How to prevent deadlock & starvation o Deadlock:](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8b4759bf5c317fdd55c891/html5/thumbnails/16.jpg)
Mutexes – User-spaceMulti-threading° Whatisakeydifferencebetweenmutex_lock andenter_region inmulti-
threadingandmulti-processing?
• Foruser-spacemulti-threading,athreadhastoallowotherthreads torunandreleasethelocksoastoenteritscriticalregion,whichisimpossiblewithbusywaitingenter_region
Two processes entering and leaving a critical region using the TSL instruction
![Page 17: CSE 3320 Operating Systems - Rangerranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC7_IPC.pdf · • Pick one fork at a time • How to prevent deadlock & starvation o Deadlock:](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8b4759bf5c317fdd55c891/html5/thumbnails/17.jpg)
Monitors° Monitor:ahigher-levelsynchronizationprimitive
• Onlyoneprocesscanbeactiveinamonitoratanyinstant,withcompiler’shelp;thus,howabouttoputallthecriticalregionsintomonitorproceduresformutualexclusion?
But, how processes block when they cannot proceed?
Condition variables, and two operations: wait() and signal()
![Page 18: CSE 3320 Operating Systems - Rangerranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC7_IPC.pdf · • Pick one fork at a time • How to prevent deadlock & starvation o Deadlock:](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8b4759bf5c317fdd55c891/html5/thumbnails/18.jpg)
Monitors(2)
Conditions are not counters; wait() before signal()Outline of producer-consumer problem with monitors
o only one monitor procedure active at one time (a process doing signal must exit the monitor immediately); buffer has N slots
Wakeup and sleep signals can lost, but not Wait and signal signals, why ?
![Page 19: CSE 3320 Operating Systems - Rangerranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC7_IPC.pdf · • Pick one fork at a time • How to prevent deadlock & starvation o Deadlock:](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8b4759bf5c317fdd55c891/html5/thumbnails/19.jpg)
Monitor(3)
• Proso Makemutualexclusionautomatic
o Makeparallelprogramminglesserror-prone
• Conso Compilersupport
![Page 20: CSE 3320 Operating Systems - Rangerranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC7_IPC.pdf · • Pick one fork at a time • How to prevent deadlock & starvation o Deadlock:](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8b4759bf5c317fdd55c891/html5/thumbnails/20.jpg)
MessagePassing
Communication without sharing memory
The producer-consumer problem with N messages
![Page 21: CSE 3320 Operating Systems - Rangerranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC7_IPC.pdf · • Pick one fork at a time • How to prevent deadlock & starvation o Deadlock:](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8b4759bf5c317fdd55c891/html5/thumbnails/21.jpg)
Barriers
• Use of a barrier (for programs operate in phases, neither enters the next phase until all are finished with the current phase) for groups of processes to do synchronization(a) processes approaching a barrier(b) all processes but one blocked at barrier(c) last process arrives, all are let through
![Page 22: CSE 3320 Operating Systems - Rangerranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC7_IPC.pdf · • Pick one fork at a time • How to prevent deadlock & starvation o Deadlock:](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8b4759bf5c317fdd55c891/html5/thumbnails/22.jpg)
ClassIPCProblems:DiningPhilosophers• Philosophers eat/think• Eating needs 2 forks• Pick one fork at a time • How to prevent deadlock & starvation
o Deadlock: both are blocked on some resource
o Starvation: both are running, but no progress made
The problem is useful for modeling processes that are competing for exclusive access to a limited number of resources, such as I/O devices
![Page 23: CSE 3320 Operating Systems - Rangerranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC7_IPC.pdf · • Pick one fork at a time • How to prevent deadlock & starvation o Deadlock:](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8b4759bf5c317fdd55c891/html5/thumbnails/23.jpg)
DiningPhilosophers(2)
A non-solution to the dining philosophers problemWhat happens if all philosophers pick up their left forks simultaneously?
Or, all wait for the same amount of time, then check if the right available?
What if random waiting, then check if the right fork available?
What performance if down and up on mutex before acquiring/replacing a fork?
![Page 24: CSE 3320 Operating Systems - Rangerranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC7_IPC.pdf · • Pick one fork at a time • How to prevent deadlock & starvation o Deadlock:](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8b4759bf5c317fdd55c891/html5/thumbnails/24.jpg)
DiningPhilosophers(3):Solutionpart1
![Page 25: CSE 3320 Operating Systems - Rangerranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC7_IPC.pdf · • Pick one fork at a time • How to prevent deadlock & starvation o Deadlock:](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8b4759bf5c317fdd55c891/html5/thumbnails/25.jpg)
DiningPhilosophers(4):Solutionpart2
![Page 26: CSE 3320 Operating Systems - Rangerranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC7_IPC.pdf · • Pick one fork at a time • How to prevent deadlock & starvation o Deadlock:](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8b4759bf5c317fdd55c891/html5/thumbnails/26.jpg)
TheReadersandWritersProblem
UC.ColoradoSprings
![Page 27: CSE 3320 Operating Systems - Rangerranger.uta.edu/~jrao/CSE3320/spring2020/slides/LEC7_IPC.pdf · • Pick one fork at a time • How to prevent deadlock & starvation o Deadlock:](https://reader034.vdocuments.net/reader034/viewer/2022042712/5f8b4759bf5c317fdd55c891/html5/thumbnails/27.jpg)
Summary• Raceconditions
• Mutualexclusionandcriticalregions
• Twosimpleapproacheso DisablinginterruptandLockvariables
• Busywaitingo Strictalternation,Peterson’sandTSL
• SleepandWakeup
• Semaphores
• Mutexes
• ClassicalIPCproblems
• Additionalpracticeo ReadLinuxdocumentation:LINUX_SRC/Documentation/spinlocks.txt
o Findtheimplementationofdown andup inLINUX_SRC/kernel/semaphore.c
o Spinlockv.s.Mutex:http://stackoverflow.com/questions/5869825/when-should-one-use-a-spinlock-instead-of-mutex