operating system concepts - uni-hamburg.de · 28/10/15 os concepts 1 . what is an os • hard to...
TRANSCRIPT
Introduc)ontoOpera)ngSystemConcepts
Prak)kumKernelProgrammingUniversityofHamburgScien)ficCompu)ng
Wintersemester2015/2016
Konstan)nosChasapisKonstan)nos.chasapis@informa)k.uni-hamburg.de
Outline
• WhatisanOpera)ngSystem• HistoryofOpera)ngSystems• ComputerHardware• TypesofOpera)ngSystems• ProcessManagement• MemoryManagement• StorageManagement*disclaimer:someofthetopicspresentedhereareincomplete.28/10/15 OS Concepts 1
WhatisanOS• Hardtodefine• Abstractsasetofhardwareresources– Highlevelinterfaceinsteadofmachinecode
• e.g.filestorageontopofblockdevices• Resourcemanagement
– Mul)plexing(sharing)resources• e.g.assignCPU)metoapplica)ons
Hardware
Operating System
Libraries
Applications
28/10/15 OS Concepts 2
1stGenera)on
• VacuumTubes(1945-55)– ~20,000vacuumtubeswereused– Programmingwasdoneinabsolutemachinecode
– Assemblylanguagewasunknown– Eachprogramusedthemachineexclusively
– MostfamousENIAC• Announcedin1946• Solvelargeclassnumericalproblems
28/10/15 OS Concepts 3
2ndGenera)on• Transistorsandbatchsystems(1955-65)– Designers/Builders/Operators/Programmers/Maintainers
– Programmersfirstwrotetheprogramonpaper,thenpuncheditoncards
– Cardreaderstoreadtheprogramsource
– Outputstoredontapesandalsoprinted– 1stuseofcompilers(FORTRAN)
28/10/15 OS Concepts 4
3rdGenera)on• ICsandMul)programming(1965-1980)
– IBM360Mainframe• Mul)programming• Severalprogramsinmemoryatoncewithseparatememory,overlapI/Owithcomputa)on
– Timesharing• Eachuserhasanonlineterminal• CTSS(Compa)bleTimeSharingSystem)• MULTICS(MULTiplexInforma)onandCompu)ngSystem)• UNIX,astripped-downversionofMULTICS• BSC(BerkeleySodwareDistribu)on)
28/10/15 OS Concepts 5
4thGenera)on
• PersonalComputers(1980-today)– SYSTEMV,1stcommercialUNIXopera)ngSystem(1983)
– LSI(LargeScaleIntegra)on)– IBMPC(early1980s)
• Intel80286CPU• DOS(DiskOpera)ngSystem)• MS-DOS(MicrosodDOS)
– LISA• FirstcomputerwithGUI• Protectedmemory,preemp)vemul)tasking
28/10/15 OS Concepts 6
5thGenera)on
• Smartphones(1990-today)• SymbianOS• RIM’sBlackberryOS• WindowsMobile• Android• iOS
28/10/15 OS Concepts 7
ModernOpera)ngSystems
28/10/15 OS Concepts 8
Outline
• WhatisanOpera)ngSystem• HistoryofOpera)ngSystems• ComputerHardware• TypesofOpera)ngSystems• ProcessManagement• MemoryManagement• StorageManagement
28/10/15 OS Concepts 9
ComputerHardware
• Processor• Mainmemory• I/Odevices• Disk• Busses
28/10/15 OS Concepts 10
Processor(CPU)
• Specificinstruc)onset
• Basiccycle• fetch• decode• execute• Mul)plecores• Mul)plelevelsofcachememory
https://commons.wikimedia.org/wiki/File:AMD_Bulldozer_block_diagram_%288_core_CPU%29.PNG 28/10/15 OS Concepts 11
MainMemory
• Randomaccessmemory(RAM)– Largebutslow– Vola)le,losesdatainpowerloss
• Sta)c(SRAM)• Fastbutexpensive• UsedasCPUcache
– Dynamic(DRAM)• Storebitsincapacitors• Requirerefreshtoretainstate• LargerthanSRAMbutslower
– Non-Vola)le,keepstatewithoutpower• Notyetmaturetechnology
RAM Disk
CPU Cache Registers
28/10/15 OS Concepts 12
Disks
• Mechanicaldevice• Highcapacity
– 200xsizeofRAM• Slow
– 1000xslowerthanRAM• Magne)crecording• Movingparts
– Rota)ngplaiers– Head
• Seek)me28/10/15 OS Concepts 13
I/ODevices
• Usuallytwoparts– Theactualdevice– Controller
• Chip(s)thatphysicallycontrolsthedevice• “Talks”totheopera)ngsystem(OS)• Devicedriver
– SodwarethatconnectsOSwiththecontroller
28/10/15 OS Concepts 14
I/ODevices(cont.)
• Formsofcommunica)on– Busywai)ng
• OSwaitsun)ldeviceresponse– Interrupts
• DeviceinformtheOSthat“something”happened– Polling
• TheOSregularlychecksthedevice– DMA(DirectMemoryAccess)
• Specialhardware• DatamovementfrommemorytocontrollerwithoutgoingthroughtheCPU
28/10/15 OS Concepts 15
Busses• Connectcomputercomponents
– Parallel(mul)plewires)• Carrydatawordsinparallel
– Serial(lanes)• Carrydatainserialformineachlane
• Differentspeeds• Differentfunc)onality• Examples
– PCIe(PeripheralComponentInterconnectexpress)– SATA(SerialATA)
28/10/15 OS Concepts 16
Outline
• WhatisanOpera)ngSystem• HistoryofOpera)ngSystems• ComputerHardware• TypesofOpera)ngSystems• ProcessManagement• MemoryManagement• StorageManagement
28/10/15 OS Concepts 17
TypesofOSs• Mul)-user
– Mul)pleusersaccessthecomputersimultaneously• Single-tasking
– Onlyonerunningprogram• Mul)-tasking
– Allowsmorethanoneprogramtoruninparallel• Twotypes:
– Pre-emp)ve,theOSinterruptstherunningprogramandassignstheCPUtothenext
– Co-opera)ve,eachprocessgive)metotheothers• Real-)me
– Aimsatexecu)ngreal-)meapplica)ons
28/10/15 OS Concepts 18
TypesofOSs(cont.)
• Distributed– Managesagroupofindependentcomputersandmakesthemappeartobeasinglecomputer
• Templated– Asinglevirtualmachineimageasaguestopera)ngsystem,thensavingitasatoolformul)plerunningvirtualmachines
• Embedded– Designedtobeusedinembeddedcomputersystems
28/10/15 OS Concepts 19
Monolithickernel• Singleimagethatrunsinasingleaddressspace
– Asetofprimi)veopera)onsareimplementedintheopera)ngsystemlevel
• Processmanagement• Memorymanagement• DeviceDrivers
– Trivial(IPC)InterProcessCommunica)on– Easytodesign– Difficulttomaintainandextend– Examples:
• MULTICS,SunOS,Linux,BSD
28/10/15 OS Concepts 20
Micro-kernel• TheminimumamountofsodwarethatprovidesthemechanismsneededtoimplementanOS– Alsoknownasμ-kernel– Provides
• Built-inIPC• Lowleveladdressspacemanagement• Threadmanagement
– Easytoextend– Performancepenal)es(requiresIPCcalls)– Examples
• Symbian,MacOS,WinNT28/10/15 OS Concepts 21
Monolithicvs.μ-kernelEverythingthatrunsinkernelmodedefinestheOS
Source: http://en.wikipedia.org/wiki/Microkernel#mediaviewer/File:OS-structure.svg
28/10/15 OS Concepts 22
Outline
• WhatisanOpera)ngSystem• HistoryofOpera)ngSystems• ComputerHardware• TypesofOpera)ngSystems• ProcessManagement• MemoryManagement• StorageManagement
28/10/15 OS Concepts 23
Processes• Anabstrac)onofarunningprogram• Everyprocess“thinks”thatitrunsalone
– Assumededicatedresources(addressspace,CPU)• Hasasinglecontrolflow(programcounter)
– Whichopera)oniscurrentlyexecuted• Programmer’srole
– Defineswhattheprocesswilldo• Opera)ngsystemrole
– Processmanagement28/10/15 OS Concepts 24
ProcessManagement• Processcrea)on
– System(ini)aliza)on,systemcall)– User(executesanewprogram)
• Processtermina)on– Normalexit– Error(voluntary),Fatalerror(involuntary)– Killedbyanotherprocess
• ProcessScheduling– AssignCPUtotheprocesses– Determinewhichprocesswillexecutenext– Switchbetweendifferentprocesses(contextswitch)
28/10/15 OS Concepts 25
ProcessHierarchies
• Oneprocessinvokesthecrea)onofanother– Parentprocess,ini)atedthecrea)on– Childprocess,thecreatedprocess
• Canalsocreateprocesses• Daemonprocess
– Runsinthebackground– Notcontrolledbytheuser– Crea)on
• Createanewprocess(child)• Killyourparent• Con)nuewiththeexecu)on
28/10/15 OS Concepts 26
ProcessStates
• Running– CurrentlyusingtheCPU
• Ready– Readytoexecute
• Blocked– Unabletorun– Waitsun)l“something”externaltohappen
Running
Blocked Ready
28/10/15 OS Concepts 27
ProcessScheduling
• Holdprocessesinqueues– jobqueue,allprocesses– runningprocesses,abletorun– devicequeue,wai)ngforI/Ocomple)on
28/10/15 OS Concepts 28
ProcessScheduling
• Scheduler– Processthatmigratesprocessesbetweenqueues
• PolicyConsidera)ons– Priori)za)on,whichwillexecutefirst– Fairness,everyprocessgetsafairamountofCPU– Starva)on,doesnotgetCPU)me– MaximizeCPUu)liza)on
28/10/15 OS Concepts 29
ContextSwitch
• SwitchtheCPUtoanotherprocess/thread– Saveprogram“context”
• Registers• Stack• Memory
– Impliesoverhead
28/10/15 OS Concepts 30
Threads
• Whynothavingmorethanonecontrolflowinsideaprocess?– Sameproblemsolvedbymorethanone
• Threadsare“mini”orlightweightprocesses
28/10/15 OS Concepts 31
Threadvs.Process• Perthread
– Stack– Registers– Privatecontrolflow(Programcounter)
• Perprocess– Addressspace– Globalvariables– Filedescriptors– Childprocesses– Signals,signalhandling– Accoun)nginforma)on
28/10/15 OS Concepts 32
Threadsconcurrency
• Manythreadsrunninginparallel• Cooperatetosolveasingleproblem
– Splittheprobleminsub-problems• Requirecoordina)on/synchroniza)on
– ControlCPUusage– Controlaccesstosharedresources
28/10/15 OS Concepts 33
ArrayMul)plica)on• Assumethatwehavetomul)plytwoarrays• Singlethreadedsolu)on
– Startfromthebeginningofthematrixandcalculateeverycelloneata)me
• Mul)threadedsolu)on– Splitthearrayinsub-arrays– Assigneachsub-arraytoadifferentthread– Runthreadsinparallel– Waitun)lallthreadsfinishthecalcula)ons– Outputmatrixisready
28/10/15 OS Concepts 34
Synchronous/Asynchronousexecu)on
• Synchronousexecu)on– Opera)onreturnswhentheexecu)onfinishes– Example
• Iwillcon)nuecookingwhenthewaterhasboiled• Asynchronousexecu)on
– Opera)onreturnsimmediatelyaderexecu)onini)aliza)on
– Checklaterorno)fywhentheexecu)onfinishes– Example
• Iputthewatertoboilandcon)nuewiththerestoftherecipe
• Ader5minutesIcheckifthewaterhasboiled
28/10/15 OS Concepts 35
Webserverexample• Webservertasks
– Acceptsrequestsforawebpage– Fetchesthedatafromthestoragesystem– Repliesbysendingthedata
• Singlethreadsolu)on– Eachrequestisexecutedserially– Onlyasinglerequestisbeingprocessedata)me– Everyrequesthastowaitfortheprevioustocompletealltasks
– I/Oisslow,lotsof)mewai)ng– Cannotu)lizemul)plecores
28/10/15 OS Concepts 36
Webserverexample• Mul)-threaded
– Createathreadforeachrequest– Mul)plerequestscanbeservedinparallel– Threadexecu)on
• Acceptrequest• I/Orequest,fetchdatafromdisk• Thisrequestcallcanbeasynchronous• Replyrequest,senddata
– Mul)-coreu)liza)on– OverlapI/Owithcomputa)on
28/10/15 OS Concepts 37
Synchroniza)onissues• Cri)calregion
– Partoftheprogramthataccessessharedresources• Globalvariables• Sharedmemory• Filedescriptors
– Itissafetobeexecutedbyonlyoneprocess/threadata)me
• Racecondi)on– Thesuccessfulexecu)ondependsonthesequenceor)mingoftheotherthreads/processes
28/10/15 OS Concepts 38
Synchroniza)onissues• Assumetwotasksruninparallel
• Task1readsaglobalvariableandupdatesthevalue
• Task2usesthesamevariableasTask1
28/10/15 OS Concepts 39
MutualExclusion• Preventparallelexecu)onsofthe“cri)calregion”
• Basicmethodsformutualexclusion– Sleepandwakeup
• Onetaskexplicitlyputstheotheronetosleepwhenitentersthecri)calregion
• Wakesitupwhenitexitsthecri)calregion– Semaphores
• Specialintegervariablestocountsleepsandwakeups– Mutex
• Simplifiedsemaphoreonlytwostateslock,unlock
28/10/15 OS Concepts 40
Producer/Consumer
procedure producer() { while (true) { item = produceItem(); if (count == N) sleep(); insert(item); count = count + 1; if (count == 1) wakeup(consumer); } }
procedure consumer() { while (true) { if (count == 0) sleep(); item = remove(); itemCount = Count - 1; if (count == N - 1) wakeup(producer); consumeItem(item); } }
#define N 100 int count=0;
28/10/15 OS Concepts 41
Deadlock
• Oneprocesswaitsfortheother• Competeforresources
– P1needsresourcesfromP2– P2needsresourcesfromP1
• Alwaysallocatewiththesameorder• Hardtodebug
28/10/15 OS Concepts 42
Outline
• WhatisanOpera)ngSystem• HistoryofOpera)ngSystems• ComputerHardware• TypesofOpera)ngSystems• ProcessManagement• MemoryManagement• StorageManagement
28/10/15 OS Concepts 43
AccessPhysicalMemory
• Useabsolutephysicaladdresses• Nomemoryabstrac)on• Runmul)pleprocesses
– OSsavestheen)rememory– OSloadsthenextprocess’smemory– Runsthenextprocess
28/10/15 OS Concepts 44
MemoryAbstrac)on• Allowmul)pleprocessestoexistinmemory• Protec)on
– Askforpermissiontoaccessanaddressinmemory• Reloca)on
– Allocatephysicalmemorydynamically– Relocatetheprocesstoadifferentregion
• Sharing– Controlwhichdatawillbesharedbetweenprocesses
• Dis)nguishphysicaltologicalmemory– Managethememoryhierarchy
28/10/15 OS Concepts 45
AddressSpace
• Setofaddressthataprocesscanuse– Memoryabstrac)on– Mapeachprocess’saddressspaceintodifferentpartsofphysicalmemory
• Useoftworegisters– Base,startaddressoftheprogram– Limit,lengthoftheprogram– OnlyOSmodifiestheseregisters
• Processmemoryaccess– Logicaladdress+Baseregister
28/10/15 OS Concepts 46
Swapping
• Ifmainmemoryisnotenough– Usedisktotemporarilystoreprocessdata
• Movingfrommemorytodiskandviceversaiscalled“swapping”
• Slowprocess– InvolvesdiskI/O
28/10/15 OS Concepts 47
VirtualMemory
• Maplogicaladdressestophysicaladdresses• Eachprocesshasaprivateaddressspace• Addressspacedividedtopages• Pagesmappedtophysicalmemory• OSkeepsapagetable
– Transla)onfromvirtualtophysicalpages
28/10/15 OS Concepts 48
Outline
• WhatisanOpera)ngSystem• HistoryofOpera)ngSystems• ComputerHardware• TypesofOpera)ngSystems• ProcessManagement• MemoryManagement• StorageManagement
28/10/15 OS Concepts 49
StorageManagement
• Applica)onsneedpermanentdatastorage– Persistency
• Hardwareprovidesblocklevelaccesses– Usefixed-sizeblocks– Everyblockhasitsownaddress– Transfersinmul)plesofblocks– Seagateprovidesobjectbasedaccess(key/value)
• OSprovidesmethodstostoreandretrievedatafromdisks
28/10/15 OS Concepts 50
FileSystem• Movedatafrommemorytodiskandviceversa• Exportfilesabstrac)on
– Namedatacollec)onswithnames->filename– Maplogicalcon)nuousunitstoarbitrarydiskblocks– Accesstheminbytelevel
• Exportfilesystemtree(hierarchy)– Groupfilescollec)onindirectories
28/10/15 OS Concepts 51
FileSystem
• Diskspacemanagement– Keepstrackofusedspace– Freespacethatisnotusedbytheusers
• Holdsystemstate– Systemmightbeininconsistentstateduringanunexpectedfailure
• Updatesystemvariables– Journalusedtokeeppreviouslyconsistentstate– Recoverfromsystemcrash
28/10/15 OS Concepts 52
FileSystem-Dataprotec)on
• Eachusercontrolsfiles/directoriespermission• Linuxpermissionthreegroups
– owner,thefile/directoryowner– group,thegroupthattheuserbelongs– allusers,therestoftheusers
• Threebasicpermissiontypes– read,canreadthefile– write,canmodifythefile– execute,canexecuteafile(denotesexecutables)
28/10/15 OS Concepts 53
Ques)ons?
Konstan)nosChasapis
Konstan)nos.chasapis@informa)k.uni-hamburg.de
OS Concepts 28/10/15 54