FinalReviewCSE120:PrinciplesofOpera:ngSystems
UCSanDiego:SummerSessionI,2009FrankUyeda
Announcements
• Project3milestonetonight• Project3dueonMonday8/3atmidnight.
– HardDeadline!– Noexcep:ons,slipdays,etc.
• Finaltomorrow(Saturday)from3p‐5p.– Youwillhave2hourstocompletethetest.– Coverseverythingwe’vetalkedabout(exceptSecurity,RAID&Logfilesystems)
2
PeerWise
• Whichtypeofdiskschedulingservicesrequestsinonedirec:on,thenreversesandservicesrequestsintheotherdirec:on?– FIFO– SSTF– SCAN– C‐SCAN
3
PeerWise
• RAIDis– Onlyafilesystem
– Afilesystemandastoragesystem– Onlyastoragesystem– Neitherafilesystem,norastoragesystem
4
ContentfortheFinal
• RoleofanOpera:ngSystem• ConcurrencyandSynchroniza:on
– Whatarethechallengestosupportmul:tasking?• CPUScheduling
– WhogetstheCPUandwhendotheygetit?• MemoryManagement
– Howdoprogramssharefinitememory?• FileSystemsandStorageManagement
– Howisdatastoredandwhathappenswhenthingsfail?• Selec:onofSpecialTopics(:mepermicng)
– Security,VirtualMachineMonitors,…..
5
PhysicalDiskStructure
• Diskcomponents– Plafers(2surfaces)– Tracks– Sectors– Cylinders– Arm– Heads(1perside)
• Logically,diskbrokendownintosectors– Addressedbycylinder,head,sector
6
Arm Heads Plafers
Cylinder
TopView
Track
Sector
DiskPerformance
• Diskrequestperformancedependsupon…..– I/Orequestoverhead:issuingthecommandtothedisk
• Processfileaccesstrapsintokernel,whichneedstoissuehwrequest– Seek:movingthediskarmtothecorrectcylinder
• Dependsonhowfastthediskarmcanmove(increasingveryslowly)– Rota:on:wai:ngforthesectortorotateunderthehead
• Dependsuponrota:onrateofdisk(increasing,butslowly)– Transfer:transferringdatafromsurfaceintodiskcontroller
electronics,sendingitbacktothehost• Dependsondensity(increasingquickly)• Fasterfortracksneartheouteredgeofthedisk–why?
• TheOStriestominimizethecostofallofthesesteps– Par:cularlyseeksandrota:on(why?)
7
DiskScheduling
• Becauseseeksaresoexpensive(milliseconds!),ithelpstoschedulediskrequeststhatarequeuedwai:ngforthedisk– FCFS/FIFO(donothing)
• Reasonablewhenloadislow• Longwai:ng:mesforlongrequestqueues
– SSTF(shortestseek:mefirst)• Minimizearmmovement(seek:me),maximizerequestrate• Favorsmiddletracks
– SCAN(elevator)• Servicerequestsinonedirec:onun:ldone,thenreverse• Discriminatesagainstthehighestandlowesttracks
– C‐SCAN• LikeSCAN,butonlygoinonedirec:on(typewriter)• Reducevarianceinseek:mes
8
DiskLayoutStrategies
Con:guousalloca:on• Idea:Allocatespaceforfilelike
doneforcon:guousmemoryorganiza:on
• Pros:Fastfileaccess
• Cons:Fragmenta:on,needscompac:on– Whathappenswhenyou
needtogrow?
v
…
DiskBlocks
Disk
File
9
DiskLayoutStrategies
LinkedAlloca:on• Idea:Linkedlistofblocks,
eachpoin:ngtonext
• Pros:Easytogrow;fastsequen:alaccess
• Cons:Slownon‐sequen:alaccess;whathappensifyouhaveonebadblock?
v
…
DiskBlocks
Disk
File
10
DiskLayoutStrategies
IndexedAlloca:on• Idea:Storeorderedlist
ofblockpointers
• Pros:Goodforrandomaccess,notbadforsequen:al
• Cons:Sizelimit,notasfastforsequen:alaccess
v
…
DiskBlocks
Disk
File
…
11
UnixInodes
• Unixusesanindexedalloca:onstructure– Aninode(indexnode)storesbothmetadata
andthepointerstodiskblocks• Metadataisinforma:onaboutthefile(protec:on,:mestamp,length,refcount,etc….)
• Eachinodecontains15blockpointers– First12aredirectblocks
(e.g.,4KBdiskblocks)– Thensingle,double,triple
indirectblocks Metadata
…
01
121314
…
DiskDataBlocks
…
…
?? 12
DirectoryInternals
• Adirectoryisalistofentries– <name,loca:on>
– Nameisjustthenameofthefileordirectory– Loca:ondependsuponhowfileisrepresentedondisk
• Listisusuallyunordered(effec:velyrandom)– Entriesusuallysortedbyprogramthatreadsdirectory
• Directoriestypicallystoredinfiles– Onlyneedtomanageonekindofsecondarystorageunit
13
ResolvingFileLoca:on/Data
• Inodesdescribewhereondisktheblocksforafileareplaced– Unixinodesarenotdirectories– Directoresarerepresentedinternallyasfiles
• Whatdoesthismeanforhowinodesarestored?
• Directoryentriesmapfilenamestoinodes– Wanttoaccess“/foo”
DataforFooisinhere
firstdatablockforfile“/foo”
inodeforfile“/foo”
Inode18Metadata
0
…
12
inodefordirectory“/”
Inode0Metadata
0
…
12
foo,18bar,451baz,123…
firstdatablockfordirectoryfile“/”
14
DataandInodePlacementProblem
• OriginalUnixFShadtwoplacementproblems:– Datablocksallocatedrandomlyinagingfilesystems– Inodesallocatedfarfromblocks
• BSDFFSSolu:on:CylinderGroups– Diskpar::onedintogroupsofcylinders– Datablocksinsamefileallocatedinsamecylinder– Filesinsamedirectoryallocatedinsamecylinder– Inodesforfilesallocatedinsamecylinderasfiledatablocks
15
MaximumFileSize:4KBBlocks• RecallUnixinodeshave:
– 12directblocks– 1singleindirectblock,1doubleindirectblock,
1tripleindirectblock• Howlargecanafilebewith4KBblocks?• Singleindirectblock:
– Assuming32‐bitaddresses,wehave4bytesperblockpointer,so4KB/4=1024Bblocks
– So…1024*1KB=1MB• Double‐indirectblock:
– 1024*1024*1KB=1GB• TripleIndirectblock:
– 1024*1024*1024*1KB=1TB• Total:~1TB
16
VirtualAddresses• Processesaccessmemoryusingavirtualaddress
– ThevirtualaddressisnotthesameasthephysicalRAMaddressinwhichitresides– TheOS(hardwareMMU)translatesthevirtualaddressintothephysicalRAMaddress– Whodeterminesthemappingforthetransla:on?
17
Stack
Heap
DataSegment
TextSegment
SP
PC0x00…….(Star:ngAddress)
0xFFF…..(EndingAddress)
AddressSpace
Stack
Heap
DataSegment
TextSegment
PhysicalMemory
MMU
…..
VirtualMemory
VirtualMemory
• Virtualmemoryenablesprogramstoexecutewithoutrequiringtheiren:readdressspaceresideinphysicalmemory
– Savesspace• Manyprogramsdonotneedalloftheircodeanddataatonce(orever),sothereisnoneedtoallocatememoryforit
– Allowsflexibilityforapplica:onandOS• Indirec:onallowsmovingprogramsaroundinmemory;OScanadjustamountofmemoryallocatedbaseduponitsrun‐:mebehavior
• Allowsprocessestoaddressmoreorlessmemorythanphysicallyinstalledinthemachine
– Isola:onandprotec:on• Oneprocesscannotaccessmemoryaddressesinothers
• Excep:on:sharedmemory,whichwe’vealreadycovered18
MemoryManagementRequirements
• Protec:on– Restrictwhichphysicaladdressesprocessescanuse,sotheycan’tstomponeachother
• Fasttransla:on– Accessingmemorymustbefast,regardlessoftheprotec:onscheme
• Fastcontextswitching– Overheadofupda:ngmemoryhardwareonacontextswitchmustbelow
• Requireshardwaresupportforefficientimplementa:on
19
MemoryAlloca:on
20
• Howshouldweallocatememorytoprocesses?
PhysicalMemory
MMU
FixedPar::ons
21
P1
P2
P3
P4
P5
PhysicalMemory
BaseRegisterP4’sBase
VirtualAddressOffset +
• Physicalmemoryisbrokenupintofixedpar::ons– Hardwarerequirements:baseregister
– Physicaladdress=virtualaddress+baseregister
VariablePar::ons
22
P1
P2
P3
PhysicalMemoryBaseRegister
P4’sBase
VirtualAddressOffset +
LimitRegisterP3’sBase
<Yes?
No?
Protec:onFault
Paging
• Pagingsolvestheexternalfragmenta:onproblembyusingfixedsizedunitsinbothphysicalandvirtualmemory
23
Page1
Page2
Page3
Page4
Page5
PhysicalMemory
Page1
Page2
PageN
VirtualMemory
…..
Paging
2424
Page1
Page2
Page3
PageP
PhysicalMemoryPage1
Page2
Process1’sVAS
…..
0x00000000
0x00040000
0x00000400
...
PageN
..
PageM
Page3
…..
Page1
Page2
Process2’sVAS
PageQ
Page3
MMU
TLB
PageTable
V PageFrame
1 0x04
1 0x01
1 0x05
1 0x07
V PageFrame
1 0x04
1 0x01
1 0x05
1 0x07
Recap:VirtualMemory
• MemoryManagementUnit(MMU)– Hardwareunitthattranslatesavirtualaddresstoaphysicaladdress
• Transla:onTable– Storedinmainmemory– EachPageTableEntrystorespermissionandbookkeepingbits.
• Transla:onLookasideBuffer(TLB)– HardwarecachefortheMMU’svirtual‐to‐physicaltransla:onstable
25
CPUTransla:on
TableMMU
Memory
VirtualAddress
PhysicalAddress
TLB
PageFrameNumberProtVRM
Recap:PageTables
26
Page1
Page2
Page3
PageN
PhysicalMemory…..
Pageframe Offset
PhysicalAddress
Pagenumber Offset
VirtualAddress
Pagetableentry
PageTable
0xFFFFFFFF
0x00000000
Problem:PageTablescanbelarge!
Recap:2LevelPageTable
27
Page1
Page2
Page3
PageN
PhysicalMemory…..
Pageframe Offset
PhysicalAddress
Secondary Offset
VirtualAddress
SecondaryPageTable(s)
0xFFFFFFFF
0x00000000
Master
Pagetableentry
MasterPageTable
Pageframe(PTE)Pageframe(PTE)
InvertedPageTable
28
Page1
Page2
Page3
PageN
PhysicalMemory…..
index Offset
PhysicalAddress
VPN Offset
VirtualAddress
0xFFFFFFFF
0x00000000
PID
<PID,VPN>
InvertedPageTable
index
InvertedPageTable
• Oneglobalpagetable– Onepagetableentryperphysicalpage.– EntrieskeyedbyPIDandvirtualpagenumber.– Physicalframenumber=indexinpagetable.
• Advantages– Boundedamountofmemoryforpagetable(s).
• 32‐bitaddressspace,4Kpages,4GBRAM,4BperPTE• 1‐levelpagetable:_____x#processes.• InvertedTable:______MB
• Disadvantages– Costlytransla:on
• Usinghashingcanhelp
29
Recap:Segmenta:on
30
Heap
Text
Data
Stack
Recap:Segmenta:on
31
PhysicalMemorySegmentTable
limit baseVirtualAddress
Segment# Offset
< +Yes?
No?
Protec:onFault
PagingvsSegmenta:onConsidera<on Paging Segmenta<on
Needtheprogrammerbeawarethatthistechniqueisbeingused?
Howmanylinearaddressspacesarethere?
Canthetotaladdressspaceexceedthesizeofphysicalmemory?
Canproceduresanddatabedis:nguishedandseparatelyprotected?
Issharingofproceduresbetweenusersfacilitated?
32Note:ImageadaptedfromTanenbaum,MOS3/e
MemoryManagementReview
• Forwhichofthefollowingdoweneedtosplitavirtualaddressintomul:pleparts?– A)PurePaging– B)PureSegmenta:on– C)FixedPar::ons– D)VariablePar::ons– E)Alloftheabove– F)Noneoftheabove
33
0x0A15D 121
VPN Offset
0x0A15D121
VirtualAddress
i.e.
DemandPagingExampleProcess’sVirtualAddressSpace PhysicalMemory
/swap~/program.coff
Disk
PC
…
…
PC
0x00 1 0x000x01 0 ‐‐‐0x02 0 ‐‐‐0x03 0 ‐‐‐
0x11 0 ‐‐‐0x10 0 ‐‐‐
…
VPN V PPNPageTable
0x01 1 0x01
34
DemandPagingExampleProcess’sVirtualAddressSpace PhysicalMemory
/swap~/program.coff
Disk
…
…
PC
0x00 1 0x000x01 0 ‐‐‐0x02 1 0x020x03 1 0x03
0x11 0 ‐‐‐0x10 1 0x09
…
VPN V PPNPageTable
0x01 1 0x01
0x10 0 0x00??
0x04 1 0x09
0x00
0x090x0A
35
Review:DemandPaging
• MemoryManagementUnit(MMU)– Hardwareunitthattranslatesavirtualaddresstoaphysicaladdress
• Transla:onTable– Storedinmainmemory
• Transla:onLookasideBuffer(TLB)– HardwarecachefortheMMU’svirtual‐to‐physicaltransla:onstable
36
CPUTransla:on
TableMMU
Memory
VirtualAddress
PhysicalAddress
TLB
PageFaultshandledsilentlybyOS
PageFault
Disk
PageFaults
• Whathappenswhenaprocessaccessesapagethathasbeenevicted(orisotherwisenotinmemory)?– 1.Whenitevictsapage,theOSsetsthePTEasinvalidand
storestheloca:onofthepageintheswapfileinthePTE– 2.Whenaprocessaccessesthepage,theinvalidPTEwill
causeatrap(pagefault)– 3.ThetrapwillruntheOSpagefaulthandler– 4.HandlerusestheinvalidPTEtolocatepageinswapfile– 5.Readspageintoaphysicalframe,updatesPTEtopointtoit– 6.Restartsprocess
• Butwheredoesitputit?Hastoevictsomethingelse– OSusuallykeepsapooloffreepagesaroundsothatalloca:ons
donotalwayscauseevic:ons
37
Example
• AssumeweareusingPagingand:– Memoryaccess=5us– TLBsearch=500ns
• Whatistheavg.memoryaccess:mewithouttheTLB?• Whatistheavg.memoryaccess:mewith50%TLBhitrate?• Whatistheavg.memoryaccess:mewith90%TLBhitrate?
38
CPUTransla:on
TableMMU
Memory
VirtualAddress
PhysicalAddress
TLB
PageSharingEmacsbuffer#1
PhysicalMemory
0x00 1 0x03
0x01 0 ‐‐‐0x10 1 0x120x11 1 0x15
VPN V PPN
0x03 1 0x11
0x00 1 0x03
0x01 0 ‐‐‐0x10 1 0x140x11 1 0x16
VPN V PPN
0x03 1 0x13
Emacsbuffer#2
..
Whatifsharedpageismodified?
0x04
39
Copy‐On‐Write
• OSesspendalotof:mecopyingdata– Systemcallargumentsbetweenuser/kernelspace– En:readdressspacestoimplementfork()
• UseCopy‐on‐Write(CoW)todeferlargecopiesaslongaspossible,hopingtoavoidthemaltogether– Insteadofcopyingpages,createsharedmappingsofparentpagesinchildvirtualaddressspace
– Sharedpagesareprotectedasread‐onlyinchild• Readshappenasusual• Writesgenerateaprotec:onfault,traptoOS,copypage,changepagemappinginclientpagetable,restartwriteinstruc:on
40
PageSharing:CoWEmacsbuffer#1
PhysicalMemory
0x00 1 0x03
0x01 0 ‐‐‐0x10 1 0x120x11 1 0x15
VPN V PPN
0x03 1 0x11
Emacsbuffer#2
..
0M
000
0x00 1 0x03
0x01 0 ‐‐‐0x10 1 0x120x11 1 0x16
VPN V PPN
0x03 1 0x13
0M
000
BuffersshareVPN0x12Processwritestopage
1
41
PageSharing:CoWEmacsbuffer#1
PhysicalMemory
0x00 1 0x03
0x01 0 ‐‐‐0x10 1 0x120x11 1 0x15
VPN V PPN
0x03 1 0x11
Emacsbuffer#2
..
0M
000
0x00 1 0x03
0x01 0 ‐‐‐0x10 1 0x140x11 1 0x16
VPN V PPN
0x03 1 0x13
0M
000
BuffersshareVPN0x12ProcesswritestopageNewpageisallocated
42
Locality
• Allpagingschemesdependonlocality– Processesreferencepagesinlocalizedpaferns
• Temporallocality– Loca:onsreferencedrecentlylikelytobereferencedagain
• Spa:allocality– Loca:onsnearrecentlyreferencedloca:onsarelikelytobereferencedsoon
• Althoughthecostofpagingishigh,ifitisinfrequentenoughitisacceptable– Processesusuallyexhibitbothkindsoflocalityduringtheirexecu:on,makingpagingprac:cal
43
PageReplacementSummary
• Pagereplacementalgorithms– Belady’s
• Replacepageusedfarthestinthefuture• op:malreplacement(min#faults)
– FIFO• replacepageloadedfurthestinpast• Belady’sanomaly
– LRU• replacepagereferencedfurthestinpast
– LRUClock• replacepagethatis“oldenough”
44
PageReplacementSummary
• Globalvslocalreplacementpolicies– Shouldaprocessreplaceitsownpage,orthatofanother?
• WorkingSet– keepthesetofpagesinmemorythathasminimalfaultrate(the“workingset”)
– PageFaultFrequency–grow/shrinkpagesetasafunc:onoffaultratetoavoidthrashing
45
PFFandMul:programming• Whatdoesthepagefaultfrequencyofprocessestellyou?
Degreeofmul:programming(#concurrentprocesses)
Thrashing!
System“goodput”(#ofprograms’instruc:onsexecuted)
46
WhatisanOpera:ngSystem?
Applica:ons
Opera:ngSystems
Hardware
)
VM
Abstrac:onstoeaseapplica:ondevelopmentManageresourceaccessamongapplica:ons
47
TypesofHardwareSupport
• Protec:on– OSwantstocontrolhardwareaccess– OSwantstokeeppar:culardata(registers,memory)privateandisolated
• Eventhandling– OSneedstobeawareofexternalevents– OSneedsawaytorespondtoexternalevents
• Performanceimprovements– Commonlyusedthreadsynchroniza:onprimi:ves
– Memoryaccess/Diskaccessspeed‐ups
48
Suppor:ngPrivilegedInstruc:ons
• HowdoesanOSorCPUuseprotec:onlevels?– ThereisaspecialregisterontheCPUindica:ngthemode
– CPUchecksmodebitwhenexecu:ngprivilegedinstruc:ons
– Userprogramsexecuteinusermode
– OSexecutesinkernelmode• Fornow,thinkofthekernelasequivalenttoanOS
49
Events
• Twotypesofevents– Interrupts:causedbyexternalevent– Excep:ons:causedbyaninstruc:on
• Eventscanbe:– Unexpected– Deliberate
Unexpected Deliberate
Excep<on(sync) Fault Syscalltrap
Interrupt(async) Interrupt So}wareInterrupt
50
SystemCalls
• Applica:oninvokesasystemcalltoexecuteaprivilegedopera:on– Raisesanexcep:on,whichinvokeskernelhandler– Passesparameterstohandler
• PredefinedSystemCallcode,anyotherarguments…
– Kernelhandlerdoestherest• savescallercontext(state)• callspropersystemcallrou:ne• restorescalledstateandreturns
51
Concurrency
• Uniprogramming– OSrunsasinglejobun:litiscompleted.
• Mul:programming– OShasaqueueofjobs– Selectsonejobtorun– IfthejobhastowaitforI/Oorhasrunforawhile,theOSsavesitsstateandputsitbackintothequeue.
– Repeatlast2stepsun:lalljobscomplete.
52
WhatisinaProcess?
• ProgramInforma:on– Addressspace,Code,Data,Stack,Registers(SP,PC,FP,..)
• Opera:ngSystemresources– Openfiles,opennetworksockets
• Execu:onstate– Running:currentlyusingtheCPU– Ready:readytousetheCPU(wai:ngforit)
• AnotherprocesshasCPU– Wai:ng:wai:ngforanevent(nottheCPU)suchasI/O
• Cannotmakeprogressun:lsignaled
53
ContextSwitchTwoprocesses:P0andP1
TheOSconsumesafairamountofprocessing:me
This:meisreferredtoascontextswitchingoverhead
Toomuchcontextswitchingoverheadisabadthing(why?)
Note:imagecourtesyofSilberschatz,200554
SchedulingQueuesTheOSkeepstrackofnon‐runningprocessesinschedulingqueues
head
tail
ReadyqueueFirefoxPCB XServerPCB IdlePCB
head
tail
DiskI/OPCBEmacsPCB BashPCB
TerminalPCBTheremaybemanywaitqueues
• seeNachos.threads.ThreadedQueue 55
ProcessCrea:oninUnix
• fork()systemcallisusedtocreateanewprocessint fork() – Createsandini:alizesanewPCB– Createsanewaddressspace– Fillschild’saddressspacewithacopyoftheparent’sen:re
addressspace– Ini:alizeskernelresourcestopointtoparent’s– PutsPCBonthereadyqueue
• So…..howdoweknowwhichoneiswhich?– EachprocesshasadifferentPID– fork()returnstwice:
• child’sPIDtotheparent• “0”tothechild
56
Unixfork()exampleint main(int argc, char *argv[]) { char *name = argv[0]; int child_pid = fork(); if (child_pid == 0) { printf(“Child of %s is %d\n”, name, getpid()); return 0; } else { printf(“My child is %d\n”, child_pid); return 0; { }
Whatdoesthisprogramprint?
Note:ThisexamplecourtesyofAlexSnoeren57
SharedMemoryvs.MessagePassing
ProcessA
ProcessB
PhysicalMemory
Kernel
helloworld
helloworld(stdout)
shared_mem
ProcessA
ProcessB
Kernel
PhysicalMemory
58
InterprocessCommunica:on
• Eachcoopera:ngmustknowthenameoftheother• SharedMemory
– Canperformfrequentandlargetransfers– Mustsynchronize
• MessagePassing– Noexplicitneedforsynchroniza:on
• Handledbyread(),write()syscalls.– Kerneloverheadpermessage
• Othercommonvaria:onsofIPC– Unixpipes(e.g.,“ls|wc”)– WindowsLocalProcedureCalls(LPCs)
59
Threads
• Separatetheconceptofaprocessfromitsexecu:onstate– Process:addressspace,privileges,resources– Execu:onstate:registers,PC,SP,FP
• Execu:onstateiscalledathreadofcontrol– Eachthreadbelongstoaprocess– Eachprocesshasatleastonethread– Threadsaretheunitsofscheduling(sortof)
• CPUrunsonethreadata:me• Threadsexecutewithinthecontextofaprocess
60
ThreadinaProcess
Stack
Heap
DataSegment
TextSegment
SP1
PC1
0x00…….(Star:ngAddress)
0xFFF…..(EndingAddress)
AddressSpace
61
ThreadsinaProcess
Stack(Thread1)
Heap
DataSegment
TextSegment
SP1
PC1
0x00…….(Star:ngAddress)
0xFFF…..(EndingAddress)
AddressSpace
Stack(Thread2)
Stack(Thread3)
SP2
SP3
PC2PC3
Thread3Thread2
Thread1
62
Mul:‐processWebServer
while (1) { int sock = accept(); if ((child_pid = fork())== 0) { Handle client request
Close socket and exit } else { Close socket } }
Usingfork()forconcurrency Before
A}er
63
Mul:‐threadedWebServer
… while (1) { int sock = accept(); thread_fork(handle_req, sock); } }
handle_req(int sock) { Handle client request close(sock) }
Usingthread_fork()forconcurrency Before
A}er
64
SchedulingThreads
• Processdecides(user‐level)– OSschedulers:llschedulesper‐process– ThreadsrunincontextofcurrentPCB
• Kerneldecides(kernel‐level)– Kernelmaintainsthreadcontrolblocks(TCB).– Kernelschedulesreadythreadstorun.
65
Non‐Preemp:veScheduling
• ThreadvoluntarilygivesuptheCPUwiththread_yield()
while (1) {
printf(“ping\n”);
thread_yield();
}
while (1) {
printf(“pong\n”);
thread_yield();
}
PingThread PongThread
Whatistheoutputofrunningthesetwothreads?
66
ThreadsSummary
• Parallelismusefulformanyapplica:ons– Processesareo}entooheavyweight– Separa:ngparalleltasksasthreadsimprovesefficiencyofmul:tasking
• Twoprimarytypesofthreads– Kernel‐levelthreads:canbeefficient,buthasoverhead– User‐levelthreads:muchfaster,butnotsupportedwellbyOS
• Arethereanydrawbackstothreads?– Difficulttoprogramcorrectly
• Nolongerassumeexclusiveuseoftheresources.– It’shardtogetthemtocooperate
67
WhenAreResourcesShared?
• Localvariablesarenotshared(private)– Refertodataonthestack– Eachthreadhasitsownstack– Shouldneverpass/shareapointertoalocalvariableonthestackforthreadT1toanotherthreadT2
• Globalvariablesandsta:cobjectsareshared– Storedinsta:cdatasegment– Accessiblebyanythread
• Dynamicobjects(heap)areshared– Allocatedfromheapwithmalloc/freeornew/delete
68
ThreadingAssump:ons
• Weassumethattheonlyatomicac:onsarereadsandwritesofwords– Notalwaysthecaseonsomearchitectures
• Weassumethatacontextswitchcanoccuratany:me.
• Weassumethatyoucandelayathreadaslongasyoulikeaslongasit’snotdelayedforever.
69
ClassicExample
• We’llrepresentthesitua:onbycrea:ngathreadforeachpersontodothewithdrawals
• ThesethreadsruninthesameBankprocess
• What’stheproblemwiththisimplementa:on?– Thinkaboutthepoten:alsequenceofexecu:onsforthesethreads
withdraw(account, amount) { balance = get_balance(account); balance = balance - amount; put_balance(account, balance); return balance; }
withdraw(account, amount) { balance = get_balance(account); balance = balance - amount; put_balance(account, balance); return balance; }
70
InterleavedSchedule• Theproblemisthattheexecu:onofthetwothreadscanbe
interleaved:
• Whatisthebalanceoftheaccount?• Thisisknownasaracecondi:on:
– Theoutcomeofacomputa:onisdependentonthe:mingorinterleavingofinstruc:ons.
put_balance(account, balance);
balance = get_balance(account); balance = balance - amount; put_balance(account, balance);
balance = get_balance(account); balance = balance – amount;
ContextswitchExecu:onsequence
seenbyCPU
71
Cri:calSec:onRequirements
1. Mutualexclusion– Ifonethreadisinthecri:calsec:on,thennootheris
2. Progress– IfsomethreadTisnotinthecri8calsec8on,thenT
cannotpreventsomeotherthreadSfromenteringthecri:calsec:on
3. Boundedwai:ng(nostarva:on)– IfsomethreadTiswai:ngonthecri:calsec:on,thenT
willeventuallyenterthecri:calsec:on4. Noassump:onsonperformance
– RequirementsmustbemetwithanynumberofCPUswitharbitraryrela:vespeeds
72
Locks
• Onewaytoimplementcri:calsec:onsisto“lockthedoor”onthewayin,andunlockitagainonthewayout
• Alockisanobjectinmemoryprovidingtwoopera:ons– acquire():beforeenteringthecri:calsec:on– release():a}erleavingacri:calsec:on
• Threadspaircallstoacquire()andrelease()– Betweenacquire()/release(),thethreadholdsthelock– acquire()doesnotreturnun:lanypreviousholderreleases
73
Locks
• Whatarethethreewaystoimplementlocks?• Whatarethepros/consofeachmethod?
74
ProblemswithSpinLocks
• Theproblemwithspinlocksisthattheyarewasteful– Ifathreadisspinningonalock,thenthethreadholdingthelock
cannotmakeprogress
• HowdidthelockholdergiveuptheCPUinthefirstplace?– Lockholdercallsyieldorsleep– Involuntarycontextswitch
• Onlywanttousespinlocksasprimi:vestobuildhigher‐levelsynchroniza:onconstructs
75
SummarizeWhereWeAre
• Goal:Usemutualexclusiontoprotectcri:calsec:onsofthataccesssharedresources
• Method:Uselocks(spinlocksordisableinterrupts)• Problem:Cri:calsec:onscanbelong
Spinlocks:• Threadswai:ngtoacquirelockspinintest‐and‐setloop• WastesCPUcycles• LongertheCS,thelongerthespin• Greaterthechanceforlockholdertobeinterrupted
DisablingInterrupts:• Shouldnotdisableinterruptsforlongperiodsof:me• Canmissordelayimportantevents(e.g.,:mer,I/O)
acquire(lock)…Cri8calSec8on…release(lock)
76
Higher‐LevelSynchroniza:on
• Needhigher‐levelsynchroniza:onprimi:vesthat:– Blockwaiters– Leaveinterruptsenabledwithinthecri:calsec:on
• Allsynchroniza:onrequiresatomicity• Sowe’lluseour“atomic”locksasprimi:vestoimplementthem
77
Semaphores
• Semaphoresareanotherdatastructurethatprovidesmutualexclusiontocri:calsec:ons– Blockwaiters,interruptsenabledwithincri:calsec:on
• Semaphoressupporttwoopera:ons:– wait(semaphore):blockun:lsemaphoreisopen,decrement
• Blockingthreadsareputonawaitqueue.– signal(semaphore):increment,allowanotherthreadtoenter
• Ifnothreadsarewai:ngonthequeue,thesignalisrememberedforthenextthread
• Inotherwords,signal()has“history”
• Semaphorescanalsobeusedasatomiccounters
78
SemaphoreTypes
• Semaphorescomeintwotypes– Determinedbysemaphore“value”(aka“count”)
• Mutexsemaphore(count=1)– Representssingleaccesstoaresource– Guaranteesmutualexclusiontoacri:calsec:on
• Coun:ngsemaphore(count=N)– Representsaresourcewithmanyunitsavailable,oraresourcethat
allowscertainkindsofunsynchronizedconcurrentaccess(e.g.,reading)
– Nthreadscanpassthesemaphore
79
SemaphoreSummary
• Semaphoredrawbacks– Theyareessen:allysharedglobalvariables
• Canpoten:allybeaccessedanywhereinprogram
– Noconnec:onbetweenthesemaphoreandthedatabeingcontrolledbythesemaphore
– Usedbothforcri:calsec:ons(mutualexclusion)andcoordina:on(scheduling)
• NotethatIhadtousecommentsinthecodetodis:nguish
– Nocontrolorguaranteeofproperusage• Some:meshardtouseandpronetobugs
– Anotherapproach:Useprogramminglanguagesupport
80
Monitors
• Amonitorisaprogramminglanguageconstructthatcontrolsaccesstoshareddata– Synchroniza:oncodeaddedbycompiler,enforcedatrun:me– Whyisthisanadvantage?
• Amonitorisamodulethatencapsulates– Shareddatastructures– Proceduresthatoperateontheshareddatastructures– Synchroniza:onbetweenconcurrentthreadsthatinvokethe
procedures
• Amonitorprotectsitsdatafromunstructuredaccess
• Itguaranteesthatthreadsaccessingitsdatathroughitsproceduresinteractonlyinlegi:mateways
81
MonitorSeman:cs
• Amonitorguaranteesmutualexclusion– Onlyonethreadcanexecuteanymonitorprocedureatany:me(thethreadis“inthemonitor”)
– Ifasecondthreadinvokesamonitorprocedurewhenafirstthreadisalreadyexecu:ngone,itblocks
• Sothemonitorhastohaveawaitqueue…
– Ifathreadwithinamonitorblocks,anotheronecanenter
82
Condi:onVariables
• Condi:onvariablesprovideamechanismtowaitforevents(a“rendezvouspoint”)
• Condi:onvariablessupportthreeopera:ons:– Wait:releasemonitorlock,waitforC/Vtobesignaled
• SoCVshavewaitqueuestoo– Signal:wakeuponewai:ngthread– Broadcast:wakeupallwai:ngthreads
83
HoarevsMesaMonitors
• Hoare(immediatelyswitchtowai:ngthread)if (empty)
wait(condition)
• Mesa(putwai:ngthreadonreadyqueue)while(empty)
wait(condition)
• Tradeoffs– Mesamonitorseasiertouse,moreefficient
• Fewercontextswitches,easytosupportbroadcast– Hoaremonitorsleavelesstochance
• Easiertoreasonabouttheprogram84
Condi:onVariablesandLocks
• Condi:onvariablesarealsousedwithoutmonitorsinconjunc:onwithblockinglocks
• Amonitoris“justlike”amodulewhosestateincludesacondi:onvariableandalock– Differenceissyntac:c;withmonitors,compileraddsthecode
• Itis“justasif”eachprocedureinthemodulecallsacquire()onentryandrelease()onexit– Butcanbedoneanywhereinprocedure,atfinergranularity
• Withcondi:onvariables,themodulemethodsmaywaitandsignalonindependentcondi:ons
85
UsingCondi:onVariablesandLocks
• Alterna:onoftwothreads(ping‐pong)• Eachexecutesthefollowing:Lock lock; Condition cond;
void ping() { acquire(lock); while(1) { printf(“ping\n”); signal(cond, lock); wait(cond, lock); } release(lock); }
Mustacquirelockbeforeyoucanwait(similartoneedinginterruptsdisabledtocallsleepinNachos)
Waitatomicallyreleaseslockandblocksun:lsignal()
Waitatomicallyacquireslockbeforeitreturns
86
Synchroniza:onSummary
• Semaphores– wait()/signal()implementblockingmutualexclusion– Alsousedasatomiccounters(coun:ngsemaphores)– Canbeinconvenienttouse
• Monitors– Synchronizesexecu:onwithinproceduresthatmanipulate
encapsulateddatasharedamongprocedures• Onlyonethreadcanexecutewithinamonitorata:me
– Reliesuponhigh‐levellanguagesupport• Condi:onvariables
– Usedbythreadsasasynchroniza:onpointtowaitforevents– Insidemonitors,oroutsidewithlocks
87
SchedulingHorizons
• Schedulingworksattwolevelsinanopera:ngsystem– Long‐termschedulinghappensrela:velyinfrequently• Significantoverheadinswappingaprocessouttodisk
– Short‐termschedulinghappensrela:velyfrequently• Wanttominimizetheoverheadofscheduling
88
SchedulingGoals
• Schedulingalgorithmscanhavemanydifferentgoals:– CPUu:liza:on– I/Ou:liza:on– Jobthroughput(#jobs/unit:me)
– Turnaround:me(Tfinish–Tarrival)
– Wai:ng:me(Avg(Twait):avg:mespentonwaitqueues)
– Response:me(Avg(Tready):avg:mespentonreadyqueue)
• Batchsystems– Striveforjobthroughput,turnaround:me(supercomputers)
• Interac:vesystems– Strivetominimizeresponse:meforinterac:vejobs(PC)
89
Starva:on
• Starva:onoccurswhenajobcannotmakeprogressbecausesomeotherjobhastheresourceitrequires– We’veseenlocks,Monitors,Semaphores,etc.
– ThesamethingcanhappenwiththeCPU!
90
SchedulingAlgorithms
• First‐comefirst‐served(FCFS),first‐infirst‐out(FIFO)– Jobsarescheduledinorderofarrivaltoreadyqueue
• ShortestJobFirst(SJF)– ChoosethejobwiththesmallestexpectedCPUburst
• Personwithsmallestnumberofitemstobuy– Provablyop:malminimumaveragewai:ng:me
• RoundRobin– Readyqueueistreatedasacircularqueue(FIFO)– Eachjobisgivena:meslicecalledaquantum
• PriorityScheduling– Choosenextjobbasedonhighestpriority
91
CombiningAlgorithms
• Schedulingalgorithmscanbecombined– Havemul:plequeues– Useadifferentalgorithmforeachqueue– Moveprocessesamongqueues
• Mul:pleLevelFeedbackQueues(MLFQ)– Mul:plequeuesrepresen:ngdifferentjobtypes
• Interac:ve,CPU‐bound,batch,system,etc.
– Queueshavepriori:es,jobsonsamequeuescheduledRR– Jobscanmoveamongqueuesbaseduponexecu:onhistory
• Feedback:Switchfrominterac:vetoCPU‐boundbehavior
92
PriorityInversion
• Problemdescrip:on– Ahighprioritythreadisstuckwai:ngforalowprioritythreadto
finish– Inthiscase,themediumprioritythreadwasholdingupthelow‐
prioritythread
• Solu:on:PriorityInheritance– Allowathreadtoinheritthepriorityofanythreadthatis
wai:ngforit– Blocked,higherprioritythreads“donate”theirprioritytolower
prioritythreads• Dona:ononlyoccursaslongasthehigherprioritythreadisblocked–oncethisisnolongertrue,threadsretaintheiroriginalpriority
93
DeadlockDefini:on
• Defini:on:Deadlockexistsamongasetofprocessesifeveryprocessiswai:ngforaneventthatcanbecausedonlybyanotherprocessintheset.
94
Condi:onsforDeadlock
Deadlockcanexistifandonlyiffourcondi:onshold:• Mutualexclusion–Atleastoneresourcemustbeheld
inanon‐sharablemode(i.e.,onlyoneinstance)• Holdandwait–Theremustbeoneprocessholding
oneresourceandwai:ngforanotherresource• Nopreemp:on–resourcescannotbepreempted
(i.e.,cri:calsec:onscannotbeabortedexternally)• Circularwait–Theremustexistasetofprocesses{P1,
P2,P3,…,Pn}suchthatP1iswai:ngforaresourceheldbyP2,P2iswai:ngforP3,…,andPnforP1
95
DealingwithDeadlock
Therearefourwaystodealwithdeadlock:• Ignoreit
– Doyoufeellucky?• Preven:on
– Makeoneofthefourcondi:onsimpossible• Avoidance
– Controlalloca:onofresources(Banker’sAlgorithm)• Detec:onandrecovery
– Lookforacycleindependencies– Preemptorabort
96
Starva:onandDeadlock• Starva:on:
– Athreadnevermakesprogressbecauseotherthreadsareusingaresourceitneeds
• Deadlock:– Acircularresourcedependencybetweenmul:plethreadssuchthatnoneofthemcanmakeprogress.
• Starva:onisnotthesameasdeadlock
97