lecture03 rachit
Transcript of lecture03 rachit
Contextfortoday’slecture
• Lastlecture(andearlypartsoftoday’slecture):
• StudysomeofthebuildingblocksofanOS
• Understand“why”weneedthesebuildingblocks
• Andwhataretheconceptualchallengesindesigningthem
• Today,andnextcoupleoflectures
• UnderstandtheabstractionsofferedbytheOS
• Threads,Process,Virtualmemory,Files,Sockets,Signals,..
• Whytheyaredesignedthewaytheyaredesigned
• Whatarethetradeoffsindifferentdesigndecisions
• Someinterestingdetailsonhowtheyareimplemented
Recall:WhatdoesanOSdo?
• Enablesconvenient“abstractions”forapplicationstoaccesshardware• CPU:threads
• Memory:virtualmemory
• Storagedevices:files
• Network:sockets
• Server:collectionofresourcesneededbyanapplication(processes,VM,..)
• Manageshardwareresources• Resourceallocation,sharingandisolation
• Implementscommonservicesforapplications• Security,protectionandauthentication
• Reliability
• Communication
• Input/outputoperations
• Programexecution
• ….
Recall:FourFundamentalOSConcepts
• Thread:ExecutionContext• Asingle,sequentialexecutioncontext
• Addressspace(withtranslation)• Program'sviewofmemoryisdistinctfromphysicalmemory
• Process:aninstanceofarunningprogram
• AddressSpace+OneormoreThreads+…
• Protection/Isolation• Onlythe“system”canaccesscertainresources
• Combinedwithtranslation,isolatesprogramsfromeachother
Recall:Threads• Definition:Asingle,sequentialexecutioncontext
• A“virtual”core• Executesasequenceofinstructions,inorder,onaphysicalcore
• Onlyonethinghappensatatime
• Challenges:• synchronization(correctness),scheduling(performance)
vCPU3vCPU2vCPU1
Shared Memory
vCPU1 vCPU2 vCPU3 vCPU1 vCPU2
Time
• Whythreads?
• Statisticalmultiplexing:improvedutilizationofphysicalcores
Recall:Virtualaddressspace
• Physicaladdressspace:wherethedataactuallyresides
• “Virtual”addressspace:wheretheprogramthinksthedataresides
• Whyvirtualaddressspace?
• Statisticalmultiplexing:improvedutilizationofphysicalmemory
• Protection/Isolation(notyetcovered)• ….
• Challenges?• Efficientaddresstranslation
Recall:Challengeofefficientaddresstranslation
• Programsusevirtualaddresses
• Asaprogramruns,virtualaddressestranslatedtophysicaladdresses
• Addresstranslationmustbeextremelylight-weight(inthecommoncase)
• Tokeeptheoverheadslow
• Twoideas:• Performaddresstranslationinhardware
• Maintainalookuptable(virtual—>physical)
• Toachieveefficiency:• Smallsizeoflookuptable(why?)
• Fastalgorithmstoperformalookup
Achievingefficiencyusing“pages”
• Dividevirtualaddressspacesintocontiguouschunksoffixedsize(sayX)• Calleachchunkapage(usuallyX=4096bytes)
• Mapeachpageto4KBofcontiguousphysicaladdressspace
• IfpagesizeisX,avirtualaddressvisat• (assumingaddresses/offsetsstartwith0)
• pagenumber:floor(v/X)
• Offset:v-X*floor(v/X)-1• E.g.,X=4096;v=4097isonpage1,offset0
• Pagesenableefficiency:• Smallerlookuptablesize
• ReducedbyafactorofX• Comparedtomappingeachindividualaddress
• Enablefasteralgorithmstoperformalookup(later)
Today:FourFundamentalOSConcepts
• Thread:ExecutionContext• Asingle,sequentialexecutioncontext
• Addressspace(withtranslation)• Program'sviewofmemoryisdistinctfromphysicalmemory
• Process:aninstanceofarunningprogram
• AddressSpace+OneormoreThreads+…
• Protection/Isolation• Onlythe“system”canaccesscertainresources
• Combinedwithtranslation,isolatesprogramsfromeachother
Process
• Definition:executionenvironmentwithrestrictedrights
• Oneormorethreads
• Executionstate:everythingthatcanaffect,orbeaffectedby,athread• Code,data,registers,callstack,files,sockets,etc.
• Partoftheprocessstateis“owned”byindividualthreads• Partissharedamongallthreadsintheprocess
• Eachprocesshasa“state”—Processcontrolblock(PCB)
• Executionstateforeachthread• Schedulinginformation
• Informationaboutmemoryusedbytheprocess
• Informationaboutfiles,sockets,etc.
• ….
EvolutionofOSprocessmodel
• Earlyoperatingsystems:singletasking
• Singleprocess,singlethread• “switch”applicationsoverlongtimescales
• Problem?
• Late1970s:multitasking
• Multipleprocesses,singlethreadperprocess
• Shareresourcesacrossprocesses• Problem?
• 1990s:multitasking,multithreading
• Multipleprocesses,multiplethreads
• Challenges?
SingleandMultithreadedProcesses
• Whyhavemultiplethreadswithinthesameprocess?
• Threadsencapsulateconcurrency
Today:FourFundamentalOSConcepts
• Thread:ExecutionContext• Asingle,sequentialexecutioncontext
• Addressspace(withtranslation)• Program'sviewofmemoryisdistinctfromphysicalmemory
• Process:aninstanceofarunningprogram
• AddressSpace+OneormoreThreads+…
• Protection/Isolation• Onlythe“system”canaccesscertainresources
• Combinedwithtranslation,isolatesprogramsfromeachother
Thecorechallengewithmultipleprocesses?
• Protection/Isolation/Sharing• Reliability:buggyprocessescanonlyhurtthemselves
• Security:aprocessdoesnothavetotrustotherprocesses• Fairness:agoodgranularitytoenforcefairutilizationofresources
• Mechanismstoenableisolation:
• Virtualization• Virtualcores,virtualaddressspace(inparticular)
• Dualmodeoperations
• OnlytheOScanaccesscertainresources
Dualmodeoperation
• Hardwareprovidesatleasttwomodesofoperations:
• Kernelmode(or“supervisor”/“protected”mode)
• Usermode
• Processes(i.e.,programsyourun)executeinusermode
• Certainoperationsareprohibitedwhenrunninginusermode
• E.g.,changingthepagetablepointer• Toperformprivilegedactions,processesrequestservicesfromtheOS
• Kernelexecutesinkernelmode
• Performsprivilegedactionstosupportrunningprocesses
• Configureshardwareforproperprotection(e.g.,addresstranslation)
• “Controlled”transitionsbetweenusermodeandkernelmode
• Systemcalls,interrupts,exceptions
UsertoKernelModeTransfers
• Syscalls• Processrequestsasystemservice,e.g.,exit
• Likeafunctioncall,but“outside”theprocess
• Interrupts• Externalasynchronousevent• e.g.,I/Ooperations
• Traporexception• Internalsynchronouseventinprocess• e.g.,protectionviolation(segmentationfault),divide-by-zero,…
Additionallayersofprotectionformodernsystems
• Inmanymodernlarge-scaledeployments
• RunacompleteOSina“virtualmachine”
• Packagealllibrariesassociatedwithanapplicationintoa“container”
• Moreonthislaterinthecourse
Divingonemoreleveldeeper:Threads
• Thread:Asingle,sequentialexecutioncontext• Asingleexecutionsequencethatcanbescheduledindependently
• Provideamechanismforconcurrencyandparallelism
• Protectionisanorthogonalconcept• Aprotectiondomaincancontainonethreadormore
Concurrencyvs.Parallelism
• Concurrencyisabouthandlingmultiplethings
• Parallelismisaboutdoingmultiplethingssimultaneously
• Example:Twothreadsonasingle-coresystemwithouthyperthreading…
• …executeconcurrently…
• …butnotinparallel
• Whatdoesitmeantoruntwothreadsconcurrently?
• Schedulerisfreetorunthreadsinanyorderandinterleaving
• Threadmayruntocompletionortime-sliceinchunks
NeedforThreads
• Considerthefollowingprogram:
main() { ComputePI(); PrintClassList(“classlist.txt”);}
•Whatoutputdoyouexpect?
•Wouldtheprogrameverprintoutclasslist?
• No!Why?
• ComputePIwouldneverfinish
WithThreads
• Versionofprogramwiththreads(loosesyntax):main() { create_thread(ComputePI()); create_thread(PrintClassList(“classlist.txt”));}
• Whatoutputdoyouexpect?
• Now,youwouldactuallyseetheclasslist
• Butonly“nowandthen”
• Illusion:infinitenumberofprocessors(potentiallyvaryingspeeds)
• create_thread:Spawnsanewthreadrunningthegivenprocedure• ShouldbehaveasifanotherCPUisrunningthegivenprocedure
ThreadsMask“Idle”periods
• Athreadisinoneofthefollowingthreestates:
• RUNNING—running
• READY—eligibletorun,butnotcurrentlyrunning
• BLOCKED—ineligibletorun
• Ifathreadcannotproceed(e.g.,waitingforanI/Orequesttobefinished)
• TheOSmarksitasBLOCKED
• Oncethethreadisready,theOSmarksitasREADY
• Cannowbescheduled
• Oncethethreadisscheduled,theOSmarksitasRUNNING
• Actuallyusingthephysicalcorenow
AnotherexampleforThreads
• Versionofprogramwiththreads(loosesyntax):main() { create_thread(RenderUserInterface); create_thread(PrintClassList(“classlist.txt”));}
• Whatisthebehaviorhere?
• Stillrespondtouserinput
• Whilereadingfileinthebackground
MultithreadedPrograms
• WhenyoucompileaCprogramandruntheexecutable
• Itcreatesaprocessthatisexecutingthatprogram
• Initially,thisnewprocesshasonethreadinitsownaddressspace
• Withcode,globals,etc.asspecifiedintheexecutable
• Howcanwemakeamultithreadedprocess?
• Aprocesscanissuessyscallstocreatenewthreads
• Thesenewthreadsarepartoftheprocess:
• Theyshareitsaddressspace
NewIdea:Fork-JoinPattern
• Mainthreadcreates(forks)collectionofsub-threadspassingthemargstoworkon…
• …andthenjoinswiththem,collectingresults.
create
exit
join
MemoryLayoutwithTwoThreads
• TwosetsofCPUregisters
• Twosetsofstacks
• Issues:
• Howdowepositionstacksrelativetoeachother?
• Whatmaximumsizeshouldwechooseforthestacks?
• Whathappensifthreadsviolatethis?
• Howmightyoucatchviolations?
Code
GlobalData
Heap
Stack1
Stack2
AddressSpace
0x000…
0xFFF…
ThreadAbstraction
• Illusion:infinitenumberofprocessors,potentiallyvaryingspeeds
• Reality:threadsexecutewithvariable“speed”
• Why?
• Dependsonschedulingpolicies
• Programsmustbedesignedtoworkwithanyschedule
CorrectnesswithConcurrentThreads
• Goal:CorrectnessbyDesign
• Whatmakesthisachallenginggoal?
• Non-determinism:
• Schedulercanrunthreadsinany(non-deterministic)order
• Why?
• Schedulercanswitchthreadsatanytime
• Why?
• IndependentThreads
• Nostatesharedwithotherthreads
• Deterministic,reproducibleconditions
• CooperatingThreads
• Sharedstatebetweenmultiplethreads
RaceConditions
• Initiallyx==0andy==0
• Whatarethepossiblevaluesofxbelowafterallthreadsfinish?
• Mustbe1.ThreadBdoesnotinterferewithThreadA.
Thread Ax = 1;
Thread B
y = 2;
RaceConditions
• Initiallyx==0andy==0
• Whatarethepossiblevaluesofxbelow?
• 1or3or5(non-deterministic)
• RaceCondition:ThreadA“races”againstThreadB!
Thread Ax = y + 1;
Thread B
y = 2;y = y * 2;
Definitions
• Synchronization:• Threadcoordination,usuallyregardingshareddata
• MutualExclusion:
• Ensuringonlyonethreaddoesaparticularthingatatime
• Typeofsynchronization
• CriticalSection:• Partofcodethatcanbeexecutedbyexactlyonethreadatonce• Resultofmutualexclusion
• Lock:• Anobjectthatcanbeheldbyonlyonethreadatatime
• Providesmutualexclusion