27
CSE 3320 Operating Systems Synchronization Jia Rao Department of Computer Science and Engineering http://ranger.uta.edu/~jrao

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:

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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