OperatingSystemsIntro11/1/2016
Whatisanoperatingsystem?
TheOSisaninterfacelayerbetweenauser’sprogramsandhardware.
Itprovidesanabstractviewofthehardwarethatmakesiteasiertouse.
ProgramOperatingSystem
ComputerHardware
Howmanyprogramsdoyouthinkarerunningonmylaptopnow?A. 0-1
B. 2-9
C. 10-99
D. 100-999
E. 1000+
Sharingresources
Alloftheseprogramsneedtoshare:• Memory• Processortime• I/Odevices
Butwhenyouwriteaprogram,youdon’thavetothinkaboutthehundredsofotherprogramsthatcouldberunning.
BeforeOperatingSystems
OSprovidesvirtualization
• Ratherthanexposingrealhardware,introducea“virtual”,abstractnotionoftheresource
• Multiplevirtualprocessors• ByrapidlyswitchingCPUuse
• Multiplevirtualmemories• Bymemorypartitioningandre-addressing
• Virtualizeddevices• Bysimplifyinginterfaces,andusingotherresourcestoenhancefunction
We’llfocusontheOS‘kernel’
• “Operatingsystem”hasmanyinterpretations• E.g.,allsoftwareonmachineminusapplications
• Ourfocusisthekernel• What’snecessaryforeverythingelsetowork• Low-levelresourcecontrol
TheKernel
• Allprogramsdependonit• Loadsandrunsthem• Exportssystemcallstoprograms
• Workscloselywithhardware• Accessesdevices• Respondstointerrupts
• Allocatesbasicresources• CPUtime,memoryspace• ControlsI/Odevices:display,keyboard,disk,network
Kernelprovidescommonfunctions
• Somefunctionsusefultomanyprograms• I/Odevicecontrol• Memoryallocation
• Placethesefunctionsincentralplace(kernel)• Calledbyprograms(systemcalls)• Oraccessedimplicitly
• Whatfunctionsshouldbeincluded?• Howmanyprogramsneedtobenefit?• Mightkernelgettoobig?
MainAbstraction:TheProcess
• Abstractionofarunningprogram• “aprograminexecution”
• Dynamic• Hasstate,changesovertime• Whereasaprogramisstatic
• Basicoperations• Start/end• Suspend/resume
BasicResourcesforProcesses
Torun,processneedssomebasicresources:• CPU• Processingcycles(time)• Toexecuteinstructions
• Memory• Bytesorwords(space)• Tomaintainstate
• Otherresources(e.g.,I/O)• Network,disk,terminal,printer,etc.
MachineStateofaProcess
• CPUorprocessorcontext• PC(programcounter)• SP(stackpointer)• Generalpurposeregisters
• Memory• Code• GlobalVariables• Stackofactivationrecords/frames• Other(registers,memory,kernel-relatedstate)
Mustkeeptrackoftheseforeveryrunningprocess!
CPU• Abstractiongoal:makeeveryprocessthinkit’srunningontheCPUallthetime.• Alternatively:IfaprocesswasremovedfromtheCPUandthengivenitback,itshouldn’tbeabletotell
• Reality:putaprocessonCPU,letitrunforashorttime(~10ms),switchtoanother,… (contextswitching)
Timesharing:SharingtheCPU
• Multipleprocesses,singleCPU(uniprocessor)• Conceptually,eachprocessmakesprogressovertime• Inreality,eachperiodicallygetsquantumofCPUtime• IllusionofparallelprogressbyrapidlyswitchingCPU
P1 P2
P3
P1 P2 P3quantum
P1P2P3
time
Memory• Abstractiongoal:makeeveryprocessthinkithasthesamememorylayout.• MUCHsimplerforcompilerifthestackalwaysstartsat0xFFFFFFFF,etc.
Text
Data
Stack
OS
Heap
Memory• Abstractiongoal:makeeveryprocessthinkithasthesamememorylayout.• MUCHsimplerforcompilerifthestackalwaysstartsat0xFFFFFFFF,etc.
• Reality:there’sonlysomuchmemorytogoaround,andnotwoprocessesshouldusethesame(physical)memoryaddresses(unlessthey’resharingonpurpose).
Process1
Process3
Process3
OS
Process2
Process1
OS(withhelpfromhardware)willkeeptrackofwho’susingeachmemoryregion.
VirtualMemory:SharingStorage
• LikeCPUcache,memoryisacachefordisk.
• Processesneverneedtoknowwheretheirmemorytrulyis,OStranslatesvirtualaddressesintophysicaladdressesforthem.
P1 P2 P3
P1 P2P3
TheKernel
• So…how/whenshouldthekernelexecute?
• Whatwouldyoudoifyouweretobuildsuchathing?
Theoperatingsystemkernel…
A. Executesasaprocess.
B. Isalwaysexecuting,insupportofotherprocesses.
C. Shouldexecuteaslittleaspossible.
D. Morethanoneoftheabove.(Whichones?)
E. Noneoftheabove.
Processvs.Kernel
• Thekernelisthecodethatsupportsprocesses• Systemcalls:fork(),exit(),read(),write(),…• Systemmanagement:contextswitching,scheduling
• Whendoesthekernelrun?• Whensystemcallorhardwareinterruptoccurs
• Isthekernelaprocess?• No,itsupportsprocessesanddevices• Runsasanextensionofprocessmakingsystemcall• Runsinresponsetodeviceissuinginterrupt
ProcessandKernelModel
Text
Data
Stack
Process1
TextData
Stack
Process2
Text
Data
Stack
Processn
…
KernelSystemCalls
write
read
forkSystem
Management Scheduling
ContextSwitching
OS OS
HeapHeap
OS
HeapMakessystemcall.OSaccessesdevice,assignsresource,etc.
ProcessandKernelModel
Text
Data
Stack
Process1
TextData
Stack
Process2
Text
Data
Stack
Processn
…
KernelSystemCalls
write
read
forkSystem
Management Scheduling
ContextSwitching
OS OS
HeapHeap
OS
Heap
OShascontrol.Itcancontextswitch(andusuallydoesatthispoint).
ProcessandKernelModel
Text
Data
Stack
Process1
TextData
Stack
Process2
Text
Data
Stack
Processn
…
KernelSystemCalls
write
read
forkSystem
Management Scheduling
ContextSwitching
OS OS
HeapHeap
OS
Heap
OSreturnscontroltoaprocess(notusuallythesameone).
ProcessandKernelModel
Text
Data
Stack
Process1
TextData
Stack
Process2
Text
Data
Stack
Processn
…
KernelSystemCalls
write
read
forkSystem
Management Scheduling
ContextSwitching
OS OS
HeapHeap
OS
Heap Transitionisexpensive,butoftennecessary.
HowDoesKernelGetControl
• Toswitchprocesses,kernelmustgetcontrol• Runningprocesscangiveupcontrolvoluntarily• Processmakesablockingsystemcall,e.g.,read()• Toblock,callyield()togiveupCPU• Controlgoestokernel,whichdispatchesnewprocess
• Or,CPUisforciblytakenaway:preemption• Whilekernelisrunning,itsetsatimer• Whentimerexpires,interruptisgenerated• Hardwareforcescontroltogotokernel
Upnext…
• Howwecreate/manageprocesses.
• Howweprovidetheillusionofthesameenormousmemoryspaceforallprocesses.