/
Chapter1ATourofComputerSystemsAcomputersystemconsistsofhardwareandsyst Chapter1ATourofComputerSystemsAcomputersystemconsistsofhardwareandsyst

Chapter1ATourofComputerSystemsAcomputersystemconsistsofhardwareandsyst - PDF document

faustina-dinatale
faustina-dinatale . @faustina-dinatale
Follow
374 views
Uploaded On 2016-02-04

Chapter1ATourofComputerSystemsAcomputersystemconsistsofhardwareandsyst - PPT Presentation

2CHAPTER1ATOUROFCOMPUTERSYSTEMS codeintrohelloc 1 includestdiohx22x4962 23 intmain 4 5 printfhelloworldn 6 codeintrohellocFigure11Thehelloprogram11InformationIsBitsC ID: 222884

2CHAPTER1.ATOUROFCOMPUTERSYSTEMS code/intro/hello.c 1 #includestdio.h&#x-2.2;䥢 23 intmain() 4 { 5 printf("hello world\n"); 6 } code/intro/hello.cFigure1.1:Thehelloprogram.1.1InformationIsBits+C

Share:

Link:

Embed:

Download Presentation from below link

Download Pdf The PPT/PDF document "Chapter1ATourofComputerSystemsAcomputers..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Chapter1ATourofComputerSystemsAcomputersystemconsistsofhardwareandsystemssoftwarethatworktogethertorunapplicationpro-grams.Specicimplementationsofsystemschangeovertime,buttheunderlyingconceptsdonot.Allcomputersystemshavesimilarhardwareandsoftwarecomponentsthatperformsimilarfunctions.Thisbookiswrittenforprogrammerswhowanttogetbetterattheircraftbyunderstandinghowthesecompo-nentsworkandhowtheyaffectthecorrectnessandperformanceoftheirprograms.Youarepoisedforanexcitingjourney.Ifyoudedicateyourselftolearningtheconceptsinthisbook,thenyouwillbeonyourwaytobecomingarare“powerprogrammer,”enlightenedbyanunderstandingoftheunderlyingcomputersystemanditsimpactonyourapplicationprograms.Youaregoingtolearnpracticalskillssuchashowtoavoidstrangenumericalerrorscausedbythewaythatcomputersrepresentnumbers.YouwilllearnhowtooptimizeyourCcodebyusingclevertricksthatex-ploitthedesignsofmodernprocessorsandmemorysystems.Youwilllearnhowthecompilerimplementsprocedurecallsandhowtousethisknowledgetoavoidthesecurityholesfrombufferoverowvulnerabil-itiesthatplaguenetworkandInternetsoftware.Youwilllearnhowtorecognizeandavoidthenastyerrorsduringlinkingthatconfoundtheaverageprogrammer.YouwilllearnhowtowriteyourownUnixshell,yourowndynamicstorageallocationpackage,andevenyourownWebserver.Youwilllearnthepromisesandpitfallsofconcurrency,atopicofincreasingimportanceasmultipleprocessorcoresareintegratedontosinglechips.IntheirclassictextontheCprogramminglanguage[58],KernighanandRitchieintroducereaderstoCusingthehelloprogramshowninFigure1.1.Althoughhelloisaverysimpleprogram,everymajorpartofthesystemmustworkinconcertinorderforittoruntocompletion.Inasense,thegoalofthisbookistohelpyouunderstandwhathappensandwhy,whenyourunhelloonyoursystem.Webeginourstudyofsystemsbytracingthelifetimeofthehelloprogram,fromthetimeitiscreatedbyaprogrammer,untilitrunsonasystem,printsitssimplemessage,andterminates.Aswefollowthelifetimeoftheprogram,wewillbrieyintroducethekeyconcepts,terminology,andcomponentsthatcomeintoplay.Laterchapterswillexpandontheseideas.1 2CHAPTER1.ATOUROFCOMPUTERSYSTEMS code/intro/hello.c 1 #includestdio.h&#x-2.2;䥢 23 intmain() 4 { 5 printf("hello,world\n"); 6 } code/intro/hello.cFigure1.1:Thehelloprogram.1.1InformationIsBits+ContextOurhelloprogrambeginslifeasasourceprogram(orsourcele)thattheprogrammercreateswithaneditorandsavesinatextlecalledhello.c.Thesourceprogramisasequenceofbits,eachwithavalueof0or1,organizedin8-bitchunkscalledbytes.Eachbyterepresentssometextcharacterintheprogram.MostmodernsystemsrepresenttextcharactersusingtheASCIIstandardthatrepresentseachcharacterwithauniquebyte-sizedintegervalue.Forexample,Figure1.2showstheASCIIrepresentationofthehello.cprogram.#includesp&#x-2.2;䥢stdio.3510511099108117100101326011511610010511146h&#x-239; .21;\n\nintsp&#x-2.2;䥢main()\n{1046210101051101163210997105110404110123\nsp&#x-2.2;䥢sp&#x-2.2;䥢sp&#x-2.2;䥢sp&#x-2.2;䥢printf("hel10323232321121141051101161024034104101108lo,sp&#x-2.2;䥢world\n");\n}10811144321191111141081009211034415910125Figure1.2:TheASCIItextrepresentationofhello.c.Thehello.cprogramisstoredinaleasasequenceofbytes.Eachbytehasanintegervaluethatcorrespondstosomecharacter.Forexample,therstbytehastheintegervalue35,whichcorrespondstothecharacter`#'.Thesecondbytehastheintegervalue105,whichcorrespondstothecharacter`i',andsoon.Noticethateachtextlineisterminatedbytheinvisiblenewlinecharacter`\n',whichisrepresentedbytheintegervalue10.Filessuchashello.cthatconsistexclusivelyofASCIIcharactersareknownastextles.Allotherlesareknownasbinaryles.Therepresentationofhello.cillustratesafundamentalidea:Allinformationinasystem—includingdiskles,programsstoredinmemory,userdatastoredinmemory,anddatatransferredacrossanetwork—isrepresentedasabunchofbits.Theonlythingthatdistinguishesdifferentdataobjectsisthecontext 1.2.PROGRAMSARETRANSLATEDBYOTHERPROGRAMSINTODIFFERENTFORMS3inwhichweviewthem.Forexample,indifferentcontexts,thesamesequenceofbytesmightrepresentaninteger,oating-pointnumber,characterstring,ormachineinstruction.Asprogrammers,weneedtounderstandmachinerepresentationsofnumbersbecausetheyarenotthesameasintegersandrealnumbers.Theyareniteapproximationsthatcanbehaveinunexpectedways.ThisfundamentalideaisexploredindetailinChapter2.Aside:OriginsoftheCprogramminglanguage.Cwasdevelopedfrom1969to1973byDennisRitchieofBellLaboratories.TheAmericanNationalStandardsInstitute(ANSI)ratiedtheANSICstandardin1989,andthisstandardizationlaterbecametheresponsibilityoftheInternationalStandardsOrganization(ISO).ThestandardsdenetheClanguageandasetoflibraryfunctionsknownastheCstandardlibrary.KernighanandRitchiedescribeANSICintheirclassicbook,whichisknownaffectionatelyas“K&R”[58].InRitchie'swords[88],Cis“quirky,awed,andanenormoussuccess.”Sowhythesuccess?CwascloselytiedwiththeUnixoperatingsystem.CwasdevelopedfromthebeginningasthesystemprogramminglanguageforUnix.MostoftheUnixkernel,andallofitssupportingtoolsandlibraries,werewritteninC.AsUnixbecamepopularinuniversitiesinthelate1970sandearly1980s,manypeoplewereexposedtoCandfoundthattheylikedit.SinceUnixwaswrittenalmostentirelyinC,itcouldbeeasilyportedtonewmachines,whichcreatedanevenwideraudienceforbothCandUnix.Cisasmall,simplelanguage.Thedesignwascontrolledbyasingleperson,ratherthanacommittee,andtheresultwasaclean,consistentdesignwithlittlebaggage.TheK&Rbookdescribesthecompletelanguageandstandardlibrary,withnumerousexamplesandexercises,inonly261pages.ThesimplicityofCmadeitrelativelyeasytolearnandtoporttodifferentcomputers.Cwasdesignedforapracticalpurpose.CwasdesignedtoimplementtheUnixoperatingsystem.Later,otherpeoplefoundthattheycouldwritetheprogramstheywanted,withoutthelanguagegettingintheway.Cisthelanguageofchoiceforsystem-levelprogramming,andthereisahugeinstalledbaseofapplication-levelprogramsaswell.However,itisnotperfectforallprogrammersandallsituations.Cpointersareacommonsourceofconfusionandprogrammingerrors.Calsolacksexplicitsupportforusefulabstractionssuchasclasses,objects,andexceptions.NewerlanguagessuchasC++andJavaaddresstheseissuesforapplication-levelprograms.EndAside.1.2ProgramsAreTranslatedbyOtherProgramsintoDifferentFormsThehelloprogrambeginslifeasahigh-levelCprogrambecauseitcanbereadandunderstoodbyhumanbeingsinthatform.However,inordertorunhello.conthesystem,theindividualCstatementsmustbetranslatedbyotherprogramsintoasequenceoflow-levelmachine-languageinstructions.Theseinstructionsarethenpackagedinaformcalledanexecutableobjectprogramandstoredasabinarydiskle.Objectprogramsarealsoreferredtoasexecutableobjectles.OnaUnixsystem,thetranslationfromsourceletoobjectleisperformedbyacompilerdriver:unix�gcc-ohellohello.cHere,theGCCcompilerdriverreadsthesourcelehello.candtranslatesitintoanexecutableobjectlehello.ThetranslationisperformedinthesequenceoffourphasesshowninFigure1.3.Theprogramsthatperformthefourphases(preprocessor,compiler,assembler,andlinker)areknowncollectivelyasthecompilationsystem. 4CHAPTER1.ATOUROFCOMPUTERSYSTEMS  \n\n \r   !" $%%&'()&*+, /01123 45689:;= �?@@A BCDDEFIJKLMNKIOKPQMTSUIWXXMWZIJKLMNKIOKPQMTSUUZZMQ\]NKIOKPQMTSUILP\]MLNKIOKPQXaPKTMLJ\]MLNKIOKPQXaPK cdefghi Figure1.3:Thecompilationsystem.Preprocessingphase.Thepreprocessor(cpp)modiestheoriginalCprogramaccordingtodirectivesthatbeginwiththe#character.Forexample,the#includestdio.h&#x-500;commandinline1ofhello.ctellsthepreprocessortoreadthecontentsofthesystemheaderlestdio.handinsertitdirectlyintotheprogramtext.TheresultisanotherCprogram,typicallywiththe.isufx.Compilationphase.Thecompiler(cc1)translatesthetextlehello.iintothetextlehello.s,whichcontainsanassembly-languageprogram.Eachstatementinanassembly-languageprogramexactlydescribesonelow-levelmachine-languageinstructioninastandardtextform.Assemblylanguageisusefulbecauseitprovidesacommonoutputlanguagefordifferentcompilersfordifferenthigh-levellanguages.Forexample,CcompilersandFortrancompilersbothgenerateoutputlesinthesameassemblylanguage.Assemblyphase.Next,theassembler(as)translateshello.sintomachine-languageinstructions,packagestheminaformknownasarelocatableobjectprogram,andstorestheresultintheobjectlehello.o.Thehello.oleisabinarylewhosebytesencodemachinelanguageinstructionsratherthancharacters.Ifweweretoviewhello.owithatexteditor,itwouldappeartobegibberish.Linkingphase.Noticethatourhelloprogramcallstheprintffunction,whichispartofthestan-dardClibraryprovidedbyeveryCcompiler.Theprintffunctionresidesinaseparateprecom-piledobjectlecalledprintf.o,whichmustsomehowbemergedwithourhello.oprogram.Thelinker(ld)handlesthismerging.Theresultisthehellole,whichisanexecutableobjectle(orsimplyexecutable)thatisreadytobeloadedintomemoryandexecutedbythesystem.Aside:TheGNUproject.GCCisoneofmanyusefultoolsdevelopedbytheGNU(shortforGNU'sNotUnix)project.TheGNUprojectisatax-exemptcharitystartedbyRichardStallmanin1984,withtheambitiousgoalofdevelopingacompleteUnix-likesystemwhosesourcecodeisunencumberedbyrestrictionsonhowitcanbemodiedordistributed.TheGNUprojecthasdevelopedanenvironmentwithallthemajorcomponentsofaUnixoperatingsystem,exceptforthekernel,whichwasdevelopedseparatelybytheLinuxproject.TheGNUenvironmentincludestheEMACSeditor,GCCcompiler,GDBdebugger,assembler,linker,utilitiesformanipulatingbinaries,andothercomponents.TheGCCcompilerhasgrowntosupportmanydifferentlanguages,withtheabilitytogeneratecodeformanydifferentmachines.SupportedlanguagesincludeC,C++,Fortran,Java,Pascal,Objective-C,andAda.TheGNUprojectisaremarkableachievement,andyetitisoftenoverlooked.Themodernopen-sourcemovement(commonlyassociatedwithLinux)owesitsintellectualoriginstotheGNUproject'snotionoffreesoftware(“free”asin“freespeech”not“freebeer”).Further,LinuxowesmuchofitspopularitytotheGNUtools,whichprovidetheenvironmentfortheLinuxkernel.EndAside. 1.3.ITPAYSTOUNDERSTANDHOWCOMPILATIONSYSTEMSWORK51.3ItPaystoUnderstandHowCompilationSystemsWorkForsimpleprogramssuchashello.c,wecanrelyonthecompilationsystemtoproducecorrectandefcientmachinecode.However,therearesomeimportantreasonswhyprogrammersneedtounderstandhowcompilationsystemswork:Optimizingprogramperformance.Moderncompilersaresophisticatedtoolsthatusuallyproducegoodcode.Asprogrammers,wedonotneedtoknowtheinnerworkingsofthecompilerinordertowriteefcientcode.However,inordertomakegoodcodingdecisionsinourCprograms,wedoneedabasicunderstandingofmachine-levelcodeandhowthecompilertranslatesdifferentCstatementsintomachinecode.Forexample,isaswitchstatementalwaysmoreefcientthanasequenceofif-elsestatements?Howmuchoverheadisincurredbyafunctioncall?Isawhileloopmoreefcientthanaforloop?Arepointerreferencesmoreefcientthanarrayindexes?Whydoesourlooprunsomuchfasterifwesumintoalocalvariableinsteadofanargumentthatispassedbyreference?Howcanafunctionrunfasterwhenwesimplyrearrangetheparenthesesinanarithmeticexpression?InChapter3,wewillintroducetworelatedmachinelanguages:IA32,the32-bitcodethathasbe-comeubiquitousonmachinesrunningLinux,Windows,andmorerecentlytheMacintoshoperatingsystems,andx86-64,a64-bitextensionfoundinmorerecentmicroprocessors.WedescribehowcompilerstranslatedifferentCconstructsintotheselanguages.InChapter5,youwilllearnhowtotunetheperformanceofyourCprogramsbymakingsimpletransformationstotheCcodethathelpthecompilerdoitsjobbetter.InChapter6youwilllearnaboutthehierarchicalnatureofthememorysystem,howCcompilersstoredataarraysinmemory,andhowyourCprogramscanexploitthisknowledgetorunmoreefciently.Understandinglink-timeerrors.Inourexperience,someofthemostperplexingprogramminger-rorsarerelatedtotheoperationofthelinker,especiallywhenyouaretryingtobuildlargesoftwaresystems.Forexample,whatdoesitmeanwhenthelinkerreportsthatitcannotresolveareference?Whatisthedifferencebetweenastaticvariableandaglobalvariable?WhathappensifyoudenetwoglobalvariablesindifferentCleswiththesamename?Whatisthedifferencebetweenastaticlibraryandadynamiclibrary?Whydoesitmatterwhatorderwelistlibrariesonthecommandline?Andscariestofall,whydosomelinker-relatederrorsnotappearuntilruntime?YouwilllearntheanswerstothesekindsofquestionsinChapter7Avoidingsecurityholes.Formanyyears,bufferoverowvulnerabilitieshaveaccountedforthema-jorityofsecurityholesinnetworkandInternetservers.Thesevulnerabilitiesexistbecausetoofewprogrammersunderstandtheneedtocarefullyrestrictthequantityandformsofdatatheyacceptfromuntrustedsources.Arststepinlearningsecureprogrammingistounderstandtheconsequencesofthewaydataandcontrolinformationarestoredontheprogramstack.WecoverthestackdisciplineandbufferoverowvulnerabilitiesinChapter3aspartofourstudyofassemblylanguage.Wewillalsolearnaboutmethodsthatcanbeusedbytheprogrammer,compiler,andoperatingsystemtoreducethethreatofattack. 6CHAPTER1.ATOUROFCOMPUTERSYSTEMS1.4ProcessorsReadandInterpretInstructionsStoredinMemoryAtthispoint,ourhello.csourceprogramhasbeentranslatedbythecompilationsystemintoanexe-cutableobjectlecalledhellothatisstoredondisk.ToruntheexecutableleonaUnixsystem,wetypeitsnametoanapplicationprogramknownasashell:unix�./hellohello,worldunix�Theshellisacommand-lineinterpreterthatprintsaprompt,waitsforyoutotypeacommandline,andthenperformsthecommand.Iftherstwordofthecommandlinedoesnotcorrespondtoabuilt-inshellcom-mand,thentheshellassumesthatitisthenameofanexecutablelethatitshouldloadandrun.Sointhiscase,theshellloadsandrunsthehelloprogramandthenwaitsforittoterminate.Thehelloprogramprintsitsmessagetothescreenandthenterminates.Theshellthenprintsapromptandwaitsforthenextinputcommandline.1.4.1HardwareOrganizationofaSystemTounderstandwhathappenstoourhelloprogramwhenwerunit,weneedtounderstandthehardwareorganizationofatypicalsystem,whichisshowninFigure1.4.ThisparticularpictureismodeledafterthefamilyofIntelPentiumsystems,butallsystemshaveasimilarlookandfeel.Don'tworryaboutthecomplexityofthisgurejustnow.Wewillgettoitsvariousdetailsinstagesthroughoutthecourseofthebook. Mainmemory I/O bridge Bus interface ALURegister file CPUSystem bus Memory bus Disk controller Graphicsadapter USBcontroller MouseKeyboard Display Disk I/O bus Expansion slots forother devices suchas network adapters hello executable stored on disk PC Figure1.4:Hardwareorganizationofatypicalsystem.CPU:CentralProcessingUnit,ALU:Arith-metic/LogicUnit,PC:Programcounter,USB:UniversalSerialBus. 1.4.PROCESSORSREADANDINTERPRETINSTRUCTIONSSTOREDINMEMORY7BusesRunningthroughoutthesystemisacollectionofelectricalconduitscalledbusesthatcarrybytesofinfor-mationbackandforthbetweenthecomponents.Busesaretypicallydesignedtotransferxed-sizedchunksofbytesknownaswords.Thenumberofbytesinaword(thewordsize)isafundamentalsystemparameterthatvariesacrosssystems.Mostmachinestodayhavewordsizesofeither4bytes(32bits)or8bytes(64bits).Forthesakeofourdiscussionhere,wewillassumeawordsizeof4bytes,andwewillassumethatbusestransferonlyonewordatatime.I/ODevicesInput/output(I/O)devicesarethesystem'sconnectiontotheexternalworld.OurexamplesystemhasfourI/Odevices:akeyboardandmouseforuserinput,adisplayforuseroutput,andadiskdrive(orsimplydisk)forlong-termstorageofdataandprograms.Initially,theexecutablehelloprogramresidesonthedisk.EachI/OdeviceisconnectedtotheI/Obusbyeitheracontrolleroranadapter.Thedistinctionbetweenthetwoismainlyoneofpackaging.Controllersarechipsetsinthedeviceitselforonthesystem'smainprintedcircuitboard(oftencalledthemotherboard).Anadapterisacardthatplugsintoaslotonthemotherboard.Regardless,thepurposeofeachistotransferinformationbackandforthbetweentheI/ObusandanI/Odevice.Chapter6hasmoretosayabouthowI/Odevicessuchasdiskswork.InChapter10,youwilllearnhowtousetheUnixI/Ointerfacetoaccessdevicesfromyourapplicationprograms.Wefocusontheespeciallyinterestingclassofdevicesknownasnetworks,butthetechniquesgeneralizetootherkindsofdevicesaswell.MainMemoryThemainmemoryisatemporarystoragedevicethatholdsbothaprogramandthedataitmanipulateswhiletheprocessorisexecutingtheprogram.Physically,mainmemoryconsistsofacollectionofdynamicrandomaccessmemory(DRAM)chips.Logically,memoryisorganizedasalineararrayofbytes,eachwithitsownuniqueaddress(arrayindex)startingatzero.Ingeneral,eachofthemachineinstructionsthatconstituteaprogramcanconsistofavariablenumberofbytes.ThesizesofdataitemsthatcorrespondtoCprogramvariablesvaryaccordingtotype.Forexample,onanIA32machinerunningLinux,dataoftypeshortrequirestwobytes,typesint,float,andlongfourbytes,andtypedoubleeightbytes.Chapter6hasmoretosayabouthowmemorytechnologiessuchasDRAMchipswork,andhowtheyarecombinedtoformmainmemory.ProcessorThecentralprocessingunit(CPU),orsimplyprocessor,istheenginethatinterprets(orexecutes)instruc-tionsstoredinmainmemory.Atitscoreisaword-sizedstoragedevice(orregister)calledtheprogramcounter(PC).Atanypointintime,thePCpointsat(containstheaddressof)somemachine-language 8CHAPTER1.ATOUROFCOMPUTERSYSTEMSinstructioninmainmemory.1Fromthetimethatpowerisappliedtothesystem,untilthetimethatthepowerisshutoff,aprocessorrepeatedlyexecutestheinstructionpointedatbytheprogramcounterandupdatestheprogramcountertopointtothenextinstruction.Aprocessorappearstooperateaccordingtoaverysimpleinstructionexecutionmodel,denedbyitsinstructionsetarchitecture.Inthismodel,instructionsexecuteinstrictsequence,andexecutingasingleinstructioninvolvesperformingaseriesofsteps.Theprocessorreadstheinstructionfrommemorypointedatbytheprogramcounter(PC),interpretsthebitsintheinstruction,performssomesimpleoperationdictatedbytheinstruction,andthenupdatesthePCtopointtothenextinstruction,whichmayormaynotbecontiguousinmemorytotheinstructionthatwasjustexecuted.Thereareonlyafewofthesesimpleoperations,andtheyrevolvearoundmainmemory,theregisterle,andthearithmetic/logicunit(ALU).Theregisterleisasmallstoragedevicethatconsistsofacollectionofword-sizedregisters,eachwithitsownuniquename.TheALUcomputesnewdataandaddressvalues.HerearesomeexamplesofthesimpleoperationsthattheCPUmightcarryoutattherequestofaninstruction:Load:Copyabyteorawordfrommainmemoryintoaregister,overwritingthepreviouscontentsoftheregister.Store:Copyabyteorawordfromaregistertoalocationinmainmemory,overwritingthepreviouscontentsofthatlocation.Operate:CopythecontentsoftworegisterstotheALU,performanarithmeticoperationonthetwowords,andstoretheresultinaregister,overwritingthepreviouscontentsofthatregister.Jump:Extractawordfromtheinstructionitselfandcopythatwordintotheprogramcounter(PC),overwritingthepreviousvalueofthePC.Wesaythataprocessorappearstobeasimpleimplementationofitsinstructionsetarchitecture,butinfactmodernprocessorsusefarmorecomplexmechanismstospeedupprogramexecution.Thus,wecandis-tinguishtheprocessor'sinstructionsetarchitecture,describingtheeffectofeachmachine-codeinstruction,fromitsmicroarchitecture,describinghowtheprocessorisactuallyimplemented.WhenwestudymachinecodeinChapter3,wewillconsidertheabstractionprovidedbythemachine'sinstructionsetarchitecture.Chapter4hasmoretosayabouthowprocessorsareactuallyimplemented.1.4.2RunningthehelloProgramGiventhissimpleviewofasystem'shardwareorganizationandoperation,wecanbegintounderstandwhathappenswhenwerunourexampleprogram.Wemustomitalotofdetailsherethatwillbelledinlater,butfornowwewillbecontentwiththebigpicture.Initially,theshellprogramisexecutingitsinstructions,waitingforustotypeacommand.Aswetypethecharacters“./hello”atthekeyboard,theshellprogramreadseachoneintoaregister,andthenstoresitinmemory,asshowninFigure1.5. 1PCisalsoacommonlyusedacronymfor“personalcomputer”.However,thedistinctionbetweenthetwoshouldbeclearfromthecontext. 1.5.CACHESMATTER9 Mainmemory I/O bridge Bus interface ALURegister file CPUSystem bus Memory bus Disk controller Graphicsadapter USBcontroller MouseKeyboard Display Disk I/O bus Expansion slots forother devices suchas network adapters PC "hello"Usertypes"hello" Figure1.5:Readingthehellocommandfromthekeyboard.Whenwehittheenterkeyonthekeyboard,theshellknowsthatwehavenishedtypingthecommand.Theshellthenloadstheexecutablehellolebyexecutingasequenceofinstructionsthatcopiesthecodeanddatainthehelloobjectlefromdisktomainmemory.Thedataincludethestringofcharacters“hello,world\n”thatwilleventuallybeprintedout.Usingatechniqueknownasdirectmemoryaccess(DMA,discussedinChapter6),thedatatravelsdirectlyfromdisktomainmemory,withoutpassingthroughtheprocessor.ThisstepisshowninFigure1.6.Oncethecodeanddatainthehelloobjectleareloadedintomemory,theprocessorbeginsexecutingthemachine-languageinstructionsinthehelloprogram'smainroutine.Theseinstructionscopythebytesinthe“hello,world\n”stringfrommemorytotheregisterle,andfromtheretothedisplaydevice,wheretheyaredisplayedonthescreen.ThisstepisshowninFigure1.7.1.5CachesMatterAnimportantlessonfromthissimpleexampleisthatasystemspendsalotoftimemovinginformationfromoneplacetoanother.Themachineinstructionsinthehelloprogramareoriginallystoredondisk.Whentheprogramisloaded,theyarecopiedtomainmemory.Astheprocessorrunstheprogram,instructionsarecopiedfrommainmemoryintotheprocessor.Similarly,thedatastring“hello,world\n”,originallyondisk,iscopiedtomainmemory,andthencopiedfrommainmemorytothedisplaydevice.Fromaprogrammer'sperspective,muchofthiscopyingisoverheadthatslowsdownthe“realwork”oftheprogram.Thus,amajorgoalforsystemdesignersistomakethesecopyoperationsrunasfastaspossible.Becauseofphysicallaws,largerstoragedevicesareslowerthansmallerstoragedevices.Andfasterdevicesaremoreexpensivetobuildthantheirslowercounterparts.Forexample,thediskdriveonatypicalsystemmightbe1000timeslargerthanthemainmemory,butitmighttaketheprocessor10,000,000timeslonger 10CHAPTER1.ATOUROFCOMPUTERSYSTEMS Mainmemory I/O bridge Bus interface ALURegister file CPUSystem bus Memory bus Disk controller Graphicsadapter USBcontroller MouseKeyboard Display Disk I/O bus Expansion slots forother devices suchas network adapters hello executable stored on disk PC hello code"hello,world\n" Figure1.6:Loadingtheexecutablefromdiskintomainmemory. Mainmemory I/O bridge Bus interface ALURegister file CPUSystem bus Memory bus Disk controller Graphicsadapter USBcontroller MouseKeyboardDisplay Disk I/O bus Expansion slots forother devices suchas network adapters hello executable stored on disk PC hello code"hello,world\n""hello,world\n" Figure1.7:Writingtheoutputstringfrommemorytothedisplay. 1.6.STORAGEDEVICESFORMAHIERARCHY11toreadawordfromdiskthanfrommemory.Similarly,atypicalregisterlestoresonlyafewhundredbytesofinformation,asopposedtobillionsofbytesinthemainmemory.However,theprocessorcanreaddatafromtheregisterlealmost100timesfasterthanfrommemory.Evenmoretroublesome,assemiconductortechnologyprogressesovertheyears,thisprocessor-memorygapcontinuestoincrease.Itiseasierandcheapertomakeprocessorsrunfasterthanitistomakemainmemoryrunfaster.Todealwiththeprocessor-memorygap,systemdesignersincludesmallerfasterstoragedevicescalledcachememories(orsimplycaches)thatserveastemporarystagingareasforinformationthattheprocessorislikelytoneedinthenearfuture.Figure1.8showsthecachememoriesinatypicalsystem.AnL1cacheontheprocessorchipholdstensofthousandsofbytesandcanbeaccessednearlyasfastastheregisterle.AlargerL2cachewithhundredsofthousandstomillionsofbytesisconnectedtotheprocessorbyaspecialbus.Itmighttake5timeslongerfortheprocesstoaccesstheL2cachethantheL1cache,butthisisstill5to10timesfasterthanaccessingthemainmemory.TheL1andL2cachesareimplementedwithahardwaretechnologyknownasstaticrandomaccessmemory(SRAM).Newerandmorepowerfulsystemsevenhavethreelevelsofcache:L1,L2,andL3.Theideabehindcachingisthatasystemcangettheeffectofbothaverylargememoryandaveryfastonebyexploitinglocality,thetendencyforprogramstoaccessdataandcodeinlocalizedregions.Bysettingupcachestoholddatathatislikelytobeaccessedoften,wecanperformmostmemoryoperationsusingthefastcaches. Mainmemory I/Obridge Bus interface ALURegister file CPU chipSystem bus Memory bus Cachememories Figure1.8:Cachememories.Oneofthemostimportantlessonsinthisbookisthatapplicationprogrammerswhoareawareofcachememoriescanexploitthemtoimprovetheperformanceoftheirprogramsbyanorderofmagnitude.YouwilllearnmoreabouttheseimportantdevicesandhowtoexploittheminChapter6.1.6StorageDevicesFormaHierarchyThisnotionofinsertingasmaller,fasterstoragedevice(e.g.,cachememory)betweentheprocessorandalargerslowerdevice(e.g.,mainmemory)turnsouttobeageneralidea.Infact,thestoragedevicesineverycomputersystemareorganizedasamemoryhierarchysimilartoFigure1.9.Aswemovefromthetopofthehierarchytothebottom,thedevicesbecomeslower,larger,andlesscostlyperbyte.Theregisterleoccupiesthetoplevelinthehierarchy,whichisknownaslevel0orL0.WeshowthreelevelsofcachingL1toL3,occupyingmemoryhierarchylevels1to3.Mainmemoryoccupieslevel4,andsoon.Themainideaofamemoryhierarchyisthatstorageatonelevelservesasacacheforstorageatthenextlowerlevel.Thus,theregisterleisacachefortheL1cache.CachesL1andL2arecachesforL2and 12CHAPTER1.ATOUROFCOMPUTERSYSTEMS RegsL1 cache (SRAM)Main memory(DRAM)Local secondary storage(local disks) Larger,slower, andcheaper (per byte)storagedevices Remote secondary storage(distributed file systems, Web servers) Local disks hold files retrieved from disks on remote network servers. Main memory holds disk blocks retrieved from local disks. L2 cache (SRAM)L1 cache holds cache lines retrieved from the L2 cache. CPU registers hold words retrieved from cache memory. L2 cache holds cache linesretrieved from L3 cache L0:L1:L2:L3:L4:L5:Smaller,faster,andcostlier(per byte)storagedevices L3 cache (SRAM)L3 cache holds cache linesretrieved from memory. L6: Figure1.9:Anexampleofamemoryhierarchy.L3,respectively.TheL3cacheisacacheforthemainmemory,whichisacacheforthedisk.Onsomenetworkedsystemswithdistributedlesystems,thelocaldiskservesasacachefordatastoredonthedisksofothersystems.Justasprogrammerscanexploitknowledgeofthedifferentcachestoimproveperformance,programmerscanexploittheirunderstandingoftheentirememoryhierarchy.Chapter6willhavemuchmoretosayaboutthis.1.7TheOperatingSystemManagestheHardwareBacktoourhelloexample.Whentheshellloadedandranthehelloprogram,andwhenthehelloprogramprinteditsmessage,neitherprogramaccessedthekeyboard,display,disk,ormainmemorydirectly.Rather,theyreliedontheservicesprovidedbytheoperatingsystem.Wecanthinkoftheoperatingsystemasalayerofsoftwareinterposedbetweentheapplicationprogramandthehardware,asshowninFigure1.10.Allattemptsbyanapplicationprogramtomanipulatethehardwaremustgothroughtheoperatingsystem. Application programs Processor Main memory I/O devices Operating system SoftwareHardware Figure1.10:Layeredviewofacomputersystem. 1.7.THEOPERATINGSYSTEMMANAGESTHEHARDWARE13Theoperatingsystemhastwoprimarypurposes:(1)toprotectthehardwarefrommisusebyrunawayappli-cations,and(2)toprovideapplicationswithsimpleanduniformmechanismsformanipulatingcomplicatedandoftenwildlydifferentlow-levelhardwaredevices.TheoperatingsystemachievesbothgoalsviathefundamentalabstractionsshowninFigure1.11:processes,virtualmemory,andles.Asthisguresug-gests,lesareabstractionsforI/Odevices,virtualmemoryisanabstractionforboththemainmemoryanddiskI/Odevices,andprocessesareabstractionsfortheprocessor,mainmemory,andI/Odevices.Wewilldiscusseachinturn. lmnoppml qrstuvuw {|€~‚ „…†‡ˆˆ‡ˆ ‰Š‹Œ Ž‘’“”•–•— Figure1.11:Abstractionsprovidedbyanoperatingsystem.Aside:UnixandPosix.The1960swasaneraofhuge,complexoperatingsystems,suchasIBM'sOS/360andHoneywell'sMulticssystems.WhileOS/360wasoneofthemostsuccessfulsoftwareprojectsinhistory,Multicsdraggedonforyearsandneverachievedwide-scaleuse.BellLaboratorieswasanoriginalpartnerintheMulticsproject,butdroppedoutin1969becauseofconcernoverthecomplexityoftheprojectandthelackofprogress.InreactiontotheirunpleasantMulticsexperience,agroupofBellLabsresearchers—KenThompson,DennisRitchie,DougMcIlroy,andJoeOssanna—beganworkin1969onasimpleroperatingsystemforaDECPDP-7computer,writtenentirelyinmachinelanguage.Manyoftheideasinthenewsystem,suchasthehierarchicallesystemandthenotionofashellasauser-levelprocess,wereborrowedfromMulticsbutimplementedinasmaller,simplerpackage.In1970,BrianKernighandubbedthenewsystem“Unix”asapunonthecomplexityof“Multics.”ThekernelwasrewritteninCin1973,andUnixwasannouncedtotheoutsideworldin1974[89].BecauseBellLabsmadethesourcecodeavailabletoschoolswithgenerousterms,Unixdevelopedalargefollowingatuniversities.ThemostinuentialworkwasdoneattheUniversityofCaliforniaatBerkeleyinthelate1970sandearly1980s,withBerkeleyresearchersaddingvirtualmemoryandtheInternetprotocolsinaseriesofreleasescalledUnix4.xBSD(BerkeleySoftwareDistribution).Concurrently,BellLabswasreleasingtheirownversions,whichbecameknownasSystemVUnix.Versionsfromothervendors,suchastheSunMicrosystemsSolarissystem,werederivedfromtheseoriginalBSDandSystemVversions.Troublearoseinthemid1980sasUnixvendorstriedtodifferentiatethemselvesbyaddingnewandoftenincom-patiblefeatures.Tocombatthistrend,IEEE(InstituteforElectricalandElectronicsEngineers)sponsoredanefforttostandardizeUnix,laterdubbed“Posix”byRichardStallman.Theresultwasafamilyofstandards,knownasthePosixstandards,thatcoversuchissuesastheClanguageinterfaceforUnixsystemcalls,shellprogramsandutilities,threads,andnetworkprogramming.AsmoresystemscomplymorefullywiththePosixstandards,thedifferencesbetweenUnixversionsaregraduallydisappearing.EndAside.1.7.1ProcessesWhenaprogramsuchashellorunsonamodernsystem,theoperatingsystemprovidestheillusionthattheprogramistheonlyonerunningonthesystem.Theprogramappearstohaveexclusiveuseofboththeprocessor,mainmemory,andI/Odevices.Theprocessorappearstoexecutetheinstructionsintheprogram, 14CHAPTER1.ATOUROFCOMPUTERSYSTEMSoneaftertheother,withoutinterruption.Andthecodeanddataoftheprogramappeartobetheonlyobjectsinthesystem'smemory.Theseillusionsareprovidedbythenotionofaprocess,oneofthemostimportantandsuccessfulideasincomputerscience.Aprocessistheoperatingsystem'sabstractionforarunningprogram.Multipleprocessescanrunconcur-rentlyonthesamesystem,andeachprocessappearstohaveexclusiveuseofthehardware.Byconcurrently,wemeanthattheinstructionsofoneprocessareinterleavedwiththeinstructionsofanotherprocess.Inmostsystems,therearemoreprocessestorunthanthereareCPUstorunthem.Traditionalsystemscouldonlyexecuteoneprogramatatime,whilenewermulti-coreprocessorscanexecuteseveralprogramssimulta-neously.Ineithercase,asingleCPUcanappeartoexecutemultipleprocessesconcurrentlybyhavingtheprocessorswitchamongthem.Theoperatingsystemperformsthisinterleavingwithamechanismknownascontextswitching.Tosimplifytherestofthisdiscussion,weconsideronlyauniprocessorsystemcontainingasingleCPU.WewillreturntothediscussionofmultiprocessorsystemsinSection1.9.1.Theoperatingsystemkeepstrackofallthestateinformationthattheprocessneedsinordertorun.Thisstate,whichisknownasthecontext,includesinformationsuchasthecurrentvaluesofthePC,theregisterle,andthecontentsofmainmemory.Atanypointintime,auniprocessorsystemcanonlyexecutethecodeforasingleprocess.Whentheoperatingsystemdecidestotransfercontrolfromthecurrentprocesstosomenewprocess,itperformsacontextswitchbysavingthecontextofthecurrentprocess,restoringthecontextofthenewprocess,andthenpassingcontroltothenewprocess.Thenewprocesspicksupexactlywhereitleftoff.Figure1.12showsthebasicideaforourexamplehelloscenario. Process AProcess B User codeKernel codeUser codeKernel codeUser code Time Context switch Context switch read Disk interrupt Return from read Figure1.12:Processcontextswitching.Therearetwoconcurrentprocessesinourexamplescenario:theshellprocessandthehelloprocess.Initially,theshellprocessisrunningalone,waitingforinputonthecommandline.Whenweaskittorunthehelloprogram,theshellcarriesoutourrequestbyinvokingaspecialfunctionknownasasystemcallthatpassescontroltotheoperatingsystem.Theoperatingsystemsavestheshell'scontext,createsanewhelloprocessanditscontext,andthenpassescontroltothenewhelloprocess.Afterhelloterminates,theoperatingsystemrestoresthecontextoftheshellprocessandpassescontrolbacktoit,whereitwaitsforthenextcommandlineinput.Implementingtheprocessabstractionrequiresclosecooperationbetweenboththelow-levelhardwareandtheoperatingsystemsoftware.Wewillexplorehowthisworks,andhowapplicationscancreateandcontroltheirownprocesses,inChapter8. 1.7.THEOPERATINGSYSTEMMANAGESTHEHARDWARE151.7.2ThreadsAlthoughwenormallythinkofaprocessashavingasinglecontrolow,inmodernsystemsaprocesscanactuallyconsistofmultipleexecutionunits,calledthreads,eachrunninginthecontextoftheprocessandsharingthesamecodeandglobaldata.Threadsareanincreasinglyimportantprogrammingmodelbecauseoftherequirementforconcurrencyinnetworkservers,becauseitiseasiertosharedatabetweenmultiplethreadsthanbetweenmultipleprocesses,andbecausethreadsaretypicallymoreefcientthanprocesses.Multi-threadingisalsoonewaytomakeprogramsrunfasterwhenmultipleprocessorsareavailable,aswewilldiscussinSection1.9.1.Youwilllearnthebasicconceptsofconcurrency,includinghowtowritethreadedprograms,inChapter12.1.7.3VirtualMemoryVirtualmemoryisanabstractionthatprovideseachprocesswiththeillusionthatithasexclusiveuseofthemainmemory.Eachprocesshasthesameuniformviewofmemory,whichisknownasitsvirtualaddressspace.ThevirtualaddressspaceforLinuxprocessesisshowninFigure1.13.(OtherUnixsystemsuseasimilarlayout.)InLinux,thetopmostregionoftheaddressspaceisreservedforcodeanddataintheoperatingsystemthatiscommontoallprocesses.Thelowerregionoftheaddressspaceholdsthecodeanddatadenedbytheuser'sprocess.Notethataddressesinthegureincreasefromthebottomtothetop. š›œšžŸ ¡¤› §¨©ª¨¬­­§ª§¯°©±²©ª³´¬ª§°¶ª¬ª°§³ ¹º»¼½¾¿¾ÀÁÂÃÄ»¾ÅÈÉÊÊË ÏÐÑÒÓÔÕÔÑÐÐ×ÑØÙÒÚÛÐÜ Þßàáâãäåäçäèéßêçßìáíß îïðñóôõñð ÷øúûüýþÿúøùý  \n \r !"#$%&,-.*. Figure1.13:Processvirtualaddressspace.Thevirtualaddressspaceseenbyeachprocessconsistsofanumberofwell-denedareas,eachwithaspecicpurpose.Youwilllearnmoreabouttheseareaslaterinthebook,butitwillbehelpfultolookbrieyateach,startingwiththelowestaddressesandworkingourwayup: 16CHAPTER1.ATOUROFCOMPUTERSYSTEMSProgramcodeanddata.Codebeginsatthesamexedaddressforallprocesses,followedbydatalocationsthatcorrespondtoglobalCvariables.Thecodeanddataareasareinitializeddirectlyfromthecontentsofanexecutableobjectle,inourcasethehelloexecutable.YouwilllearnmoreaboutthispartoftheaddressspacewhenwestudylinkingandloadinginChapter7.Heap.Thecodeanddataareasarefollowedimmediatelybytherun-timeheap.Unlikethecodeanddataareas,whicharexedinsizeoncetheprocessbeginsrunning,theheapexpandsandcontractsdynamicallyatruntimeasaresultofcallstoCstandardlibraryroutinessuchasmallocandfree.WewillstudyheapsindetailwhenwelearnaboutmanagingvirtualmemoryinChapter9.Sharedlibraries.NearthemiddleoftheaddressspaceisanareathatholdsthecodeanddataforsharedlibrariessuchastheCstandardlibraryandthemathlibrary.Thenotionofasharedlibraryisapowerful,butsomewhatdifcultconcept.YouwilllearnhowtheyworkwhenwestudydynamiclinkinginChapter7.Stack.Atthetopoftheuser'svirtualaddressspaceistheuserstackthatthecompilerusestoim-plementfunctioncalls.Liketheheap,theuserstackexpandsandcontractsdynamicallyduringtheexecutionoftheprogram.Inparticular,eachtimewecallafunction,thestackgrows.Eachtimewereturnfromafunction,itcontracts.YouwilllearnhowthecompilerusesthestackinChapter3.Kernelvirtualmemory.Thekernelisthepartoftheoperatingsystemthatisalwaysresidentinmemory.Thetopregionoftheaddressspaceisreservedforthekernel.Applicationprogramsarenotallowedtoreadorwritethecontentsofthisareaortodirectlycallfunctionsdenedinthekernelcode.Forvirtualmemorytowork,asophisticatedinteractionisrequiredbetweenthehardwareandtheoperatingsystemsoftware,includingahardwaretranslationofeveryaddressgeneratedbytheprocessor.Thebasicideaistostorethecontentsofaprocess'svirtualmemoryondisk,andthenusethemainmemoryasacacheforthedisk.Chapter9explainshowthisworksandwhyitissoimportanttotheoperationofmodernsystems.1.7.4FilesAleisasequenceofbytes,nothingmoreandnothingless.EveryI/Odevice,includingdisks,keyboards,displays,andevennetworks,ismodeledasale.Allinputandoutputinthesystemisperformedbyreadingandwritingles,usingasmallsetofsystemcallsknownasUnixI/O.ThissimpleandelegantnotionofaleisnonethelessverypowerfulbecauseitprovidesapplicationswithauniformviewofallofthevariedI/Odevicesthatmightbecontainedinthesystem.Forexample,appli-cationprogrammerswhomanipulatethecontentsofadiskleareblissfullyunawareofthespecicdisktechnology.Further,thesameprogramwillrunondifferentsystemsthatusedifferentdisktechnologies.YouwilllearnaboutUnixI/OinChapter10.Aside:TheLinuxproject.InAugust1991,aFinnishgraduatestudentnamedLinusTorvaldsmodestlyannouncedanewUnix-likeoperatingsystemkernel: 1.8.SYSTEMSCOMMUNICATEWITHOTHERSYSTEMSUSINGNETWORKS17From:torvalds@klaava.Helsinki.FI(LinusBenedictTorvalds)Newsgroups:comp.os.minixSubject:Whatwouldyouliketoseemostinminix?Summary:smallpollformynewoperatingsystemDate:25Aug9120:57:08GMTHelloeverybodyoutthereusingminix-I'mdoinga(free)operatingsystem(justahobby,won'tbebigandprofessionallikegnu)for386(486)ATclones.ThishasbeenbrewingsinceApril,andisstartingtogetready.I'dlikeanyfeedbackonthingspeoplelike/dislikeinminix,asmyOSresemblesitsomewhat(samephysicallayoutofthefile-system(duetopracticalreasons)amongotherthings).I'vecurrentlyportedbash(1.08)andgcc(1.40),andthingsseemtowork.ThisimpliesthatI'llgetsomethingpracticalwithinafewmonths,andI'dliketoknowwhatfeaturesmostpeoplewouldwant.Anysuggestionsarewelcome,butIwon'tpromiseI'llimplementthem:-)Linus(torvalds@kruuna.helsinki.fi)Therest,astheysay,ishistory.Linuxhasevolvedintoatechnicalandculturalphenomenon.BycombiningforceswiththeGNUproject,theLinuxprojecthasdevelopedacomplete,Posix-compliantversionoftheUnixoperatingsystem,includingthekernelandallofthesupportinginfrastructure.Linuxisavailableonawidearrayofcomputers,fromhand-helddevicestomainframecomputers.AgroupatIBMhasevenportedLinuxtoawristwatch!EndAside.1.8SystemsCommunicatewithOtherSystemsUsingNetworksUptothispointinourtourofsystems,wehavetreatedasystemasanisolatedcollectionofhardwareandsoftware.Inpractice,modernsystemsareoftenlinkedtoothersystemsbynetworks.Fromthepointofviewofanindividualsystem,thenetworkcanbeviewedasjustanotherI/Odevice,asshowninFigure1.14.Whenthesystemcopiesasequenceofbytesfrommainmemorytothenetworkadapter,thedataowsacrossthenetworktoanothermachine,insteadof,say,toalocaldiskdrive.Similarly,thesystemcanreaddatasentfromothermachinesandcopythisdatatoitsmainmemory.WiththeadventofglobalnetworkssuchastheInternet,copyinginformationfromonemachinetoanotherhasbecomeoneofthemostimportantusesofcomputersystems.Forexample,applicationssuchasemail,instantmessaging,theWorldWideWeb,FTP,andtelnetareallbasedontheabilitytocopyinformationoveranetwork.Returningtoourhelloexample,wecouldusethefamiliartelnetapplicationtorunhelloonaremotemachine.Supposeweuseatelnetclientrunningonourlocalmachinetoconnecttoatelnetserveronaremotemachine.Afterwelogintotheremotemachineandrunashell,theremoteshelliswaitingtoreceiveaninputcommand.Fromthispoint,runningthehelloprogramremotelyinvolvesthevebasicstepsshowninFigure1.15.Afterwetypethe“hello”stringtothetelnetclientandhittheenterkey,theclientsendsthestringtothetelnetserver.Afterthetelnetserverreceivesthestringfromthenetwork,itpassesitalongtotheremoteshellprogram.Next,theremoteshellrunsthehelloprogram,andpassestheoutputlinebacktothetelnetserver.Finally,thetelnetserverforwardstheoutputstringacrossthenetworktothetelnetclient,whichprintstheoutputstringonourlocalterminal. 18CHAPTER1.ATOUROFCOMPUTERSYSTEMS Mainmemory I/O bridge Bus interface ALURegister file CPU chipSystem bus Memory bus Disk controller Graphicsadapter USBcontroller MouseKeyboard Monitor Disk I/O bus Expansion slots Networkadapter Network PC Figure1.14:AnetworkisanotherI/Odevice. 12356476548 9:;=:&#x-100;&#x.098;:A:ACDFGHIJKILMNPPSTWXTYTZZWZTSZU\ZU\ZSZW`Rabccdfgijglmnmnoopmirhsmntuvvwxyz{y|€‚ƒzƒ‚‚ƒ‚y‡‚ ˆ‰Œ‹Ž‹‘‹‹’“”•––—˜™—š–›žŸ £¤ ¥§¢¨©ª§¢¨ž­¯¯°±²°³¯´µ¸¹»¼½¸ÀÁ¸ÇÀÇ·ÈÉÊÊÍÎÏÐÑÐÓÔÍÕÖ Figure1.15:Usingtelnettorunhelloremotelyoveranetwork. 1.9.IMPORTANTTHEMES19Thistypeofexchangebetweenclientsandserversistypicalofallnetworkapplications.InChapter11youwilllearnhowtobuildnetworkapplications,andapplythisknowledgetobuildasimpleWebserver.1.9ImportantThemesThisconcludesourinitialwhirlwindtourofsystems.Animportantideatotakeawayfromthisdiscussionisthatasystemismorethanjusthardware.Itisacollectionofintertwinedhardwareandsystemssoftwarethatmustcooperateinordertoachievetheultimategoalofrunningapplicationprograms.Therestofthisbookwillllinsomedetailsaboutthehardwareandthesoftware,anditwillshowhow,byknowingthesedetails,youcanwriteprogramsthatarefastermorereliable,andmoresecure.Tocloseoutthischapter,wehighlightseveralimportantconceptsthatcutacrossallaspectsofcomputersystems.Wewilldiscusstheimportanceoftheseconceptsatmultipleplaceswithinthebook.1.9.1ConcurrencyandParallelismThroughoutthehistoryofdigitalcomputers,twodemandshavebeenconstantforcesdrivingimprovements:wewantthemtodomore,andwewantthemtorunfaster.Bothofthesefactorsimprovewhentheprocessordoesmorethingsatonce.Weusethetermconcurrencytorefertothegeneralconceptofasystemwithmultiple,simultaneousactivities,andthetermparallelismtorefertotheuseofconcurrencytomakeasystemrunfaster.Parallelismcanbeexploitedatmultiplelevelsofabstractioninacomputersystem.Wehighlightthreelevelshere,workingfromthehighesttothelowestlevelinthesystemhierarchy.Thread-LevelConcurrencyBuildingontheprocessabstraction,weareabletodevisesystemswheremultipleprogramsexecuteatthesametime,leadingtoconcurrency.Withthreads,wecanevenhavemultiplecontrolowsexecutingwithinasingleprocess.Supportforconcurrentexecutionhasbeenfoundincomputersystemssincetheadventoftime-sharingintheearly1960s.Traditionally,thisconcurrentexecutionwasonlysimulated,byhavingasinglecomputerrapidlyswitchamongitsexecutingprocesses,muchasajugglerkeepsmultipleballsyingthroughtheair.Thisformofconcurrencyallowsmultipleuserstointeractwithasystematthesametime,suchaswhenmanypeoplewanttogetpagesfromasinglewebserver.Italsoallowsasingleusertoengageinmultipletasksconcurrently,suchashavingawebbrowserinonewindow,awordprocessorinanother,andstreamingmusicplayingatthesametime.Untilrecently,mostactualcomputingwasdonebyasingleprocessor,evenifthatprocessorhadtoswitchamongmultipletasks.Thiscongurationisknownasauniprocessorsystem.Whenweconstructasystemconsistingofmultipleprocessorsallunderthecontrolofasingleoperatingsystemkernel,wehaveamultiprocessorsystem.Suchsystemshavebeenavailableforlarge-scalecom-putingsincethe1980s,buttheyhavemorerecentlybecomecommonplacewiththeadventofmulti-coreprocessorsandhyperthreading.Figure1.16showsataxonomyofthesedifferentprocessortypes.Multi-coreprocessorshaveseveralCPUs(referredtoas“cores”)integratedontoasingleintegrated-circuitchip.Figure1.17illustratestheorganizationofanIntelCorei7processor,wherethemicroprocessorchip 20CHAPTER1.ATOUROFCOMPUTERSYSTEMS Figure1.16:Categorizingdifferentprocessorcongurations.Multiprocessorsarebecomingprevalentwiththeadventofmulti-coreprocessorsandhyperthreading. Regs L1d-cache L1i-cache L2 unified cache Core 0 Regs L1d-cache L1i-cache L2 unified cache Core 3 L3 unified cache(shared by all cores) Main memory Processor package Figure1.17:IntelCorei7organization.Fourprocessorcoresareintegratedontoasinglechip. 1.9.IMPORTANTTHEMES21hasfourCPUcores,eachwithitsownL1andL2caches,butsharingthehigherlevelsofcacheaswellastheinterfacetomainmemory.Industryexpertspredictthattheywillbeabletohavedozens,andultimatelyhundredsofcoresonasinglechip.Hyperthreading,sometimescalledsimultaneousmulti-threadingisatechniquethatallowsasingleCPUtoexecutemultipleowsofcontrol.ItinvolveshavingmultiplecopiesofsomeoftheCPUhardware,suchasprogramcountersandregisterles,whilehavingonlysinglecopiesofotherpartsofthehardware,suchastheunitsthatperformoating-pointarithmetic.Whereasaconventionalprocessorrequiresaround20,000clockcyclestoshiftbetweendifferentthreads,ahyperthreadedprocessordecideswhichofitsthreadstoexecuteonacycle-by-cyclebasis.ItenablestheCPUtomakebetteradvantageofitsprocessingresources.Forexample,ifonethreadmustwaitforsomedatatobeloadedintoacache,theCPUcanproceedwiththeexecutionofadifferentthread.Asanexample,theIntelCorei7processorcanhaveeachcoreexecutingtwothreads,andsoafour-coresystemcanactuallyexecuteeightthreadsinparallel.Theuseofmultiprocessingcanimprovesystemperformanceintwoways.First,itreducestheneedtosimulateconcurrencywhenperformingmultipletasks.Asmentioned,evenapersonalcomputerbeingusedbyasinglepersonisexpectedtoperformmanyactivitiesconcurrently.Second,itcanrunasingleapplicationprogramfaster,butonlyifthatprogramisexpressedintermsofmultiplethreadsthatcaneffectivelyexecuteinparallel.Thus,althoughtheprinciplesofconcurrencyhavebeenformulatedandstudiedforover50years,theadventofmulti-coreandhyperthreadedsystemshasgreatlyincreasedthedesiretondwaystowriteapplicationprogramsthatcanexploitthethread-levelparallelismavailablewiththehardware.Chapter12willlookmuchmoredeeplyintoconcurrencyanditsusetoprovideasharingofprocessingresourcesandtoenablemoreparallelisminprogramexecution.Instruction-LevelParallelismAtamuchlowerlevelofabstraction,modernprocessorscanexecutemultipleinstructionsatonetime,apropertyknownasinstruction-levelparallelism.Forexample,earlymicroprocessors,suchasthe1978-vintageIntel8086,requiredmultiple(typically3–10)clockcyclestoexecuteasingleinstruction.Morerecentprocessorscansustainexecutionratesof2–4instructionsperclockcycle.Anygiveninstructionrequiresmuchlongerfromstarttonish,perhaps20cyclesormore,buttheprocessorusesanumberofclevertrickstoprocessasmanyas100instructionsatatime.InChapter4,wewillexploretheuseofpipelining,wheretheactionsrequiredtoexecuteaninstructionarepartitionedintodifferentsteps,andtheprocessorhardwareisorganizedasaseriesofstages,eachperformingoneofthesesteps.Thestagescanoperateinparallel,workingondifferentpartsofdifferentinstructions.Wewillseethatafairlysimplehardwaredesigncansustainanexecutionrateclosetooneinstructionperclockcycle.Processorsthatcansustainexecutionratesfasterthanoneinstructionpercycleareknownassuperscalarprocessors.Mostmodernprocessorssupportsuperscalaroperation.InChapter5wewilldescribeahigh-levelmodelofsuchprocessors.Wewillseethatapplicationprogrammerscanusethismodeltounderstandtheperformanceoftheirprograms.Theycanthenwriteprogramssuchthatthegeneratedcodeachieveshigherdegreesofinstruction-levelparallelismandthereforerunsfaster. 22CHAPTER1.ATOUROFCOMPUTERSYSTEMSSingle-Instruction,Multiple-Data(SIMD)ParallelismAtthelowestlevel,manymodernprocessorshavespecialhardwarethatallowsasingleinstructiontocausemultipleoperationstobeperformedinparallel,amodeknownassingle-instruction,multiple-data,or“SIMD”parallelism.Forexample,recentgenerationsofIntelandAMDprocessorshaveinstructionsthatcanaddfourpairsofsingle-precisionoating-pointnumbers(Cdatatypefloat)inparallel.TheseSIMDinstructionsareprovidedmostlytospeedupapplicationsthatprocessimage,sound,andvideodata.AlthoughsomecompilersattempttoautomaticallyextractSIMDparallelismfromCprograms,amorereliablemethodistowriteprogramsusingspecialvectordatatypessupportedincompilerssuchasGCC.WedescribethisstyleofprogramminginWebAsideOPT:SIMD,asasupplementtothemoregeneralpresentationonprogramoptimizationfoundinChapter5.1.9.2TheImportanceofAbstractionsinComputerSystemsTheuseofabstractionsisoneofthemostimportantconceptsincomputerscience.Forexample,oneaspectofgoodprogrammingpracticeistoformulateasimpleapplication-programinterface(API)forasetoffunctionsthatallowprogrammerstousethecodewithouthavingtodelveintoitsinnerworkings.Differentprogramminglanguagesprovidedifferentformsandlevelsofsupportforabstraction,suchasJavaclassdeclarationsandCfunctionprototypes. ØÙÚÛÜÜÙØ ÝÞßàáâáã çèëìíêî ðñòóôôóô õö÷øù úûüýþ  \n    !" $%#(&# "! Figure1.18:Someabstractionsprovidedbyacomputersystem.Amajorthemeincomputersystemsistoprovideabstractrepresentationsatdifferentlevelstohidethecomplexityoftheactualimplementations.Wehavealreadybeenintroducedtoseveraloftheabstractionsseenincomputersystems,asindicatedinFigure1.18.Ontheprocessorside,theinstructionsetarchitectureprovidesanabstractionoftheactualprocessorhardware.Withthisabstraction,amachine-codeprogrambehavesasifitwereexecutedonaprocessorthatperformsjustoneinstructionatatime.Theunderlyinghardwareisfarmoreelaborate,executingmultipleinstructionsinparallel,butalwaysinawaythatisconsistentwiththesimple,sequentialmodel.Bykeepingthesameexecutionmodel,differentprocessorimplementationscanexecutethesamemachinecode,whileofferingarangeofcostandperformance.Ontheoperatingsystemside,wehaveintroducedthreeabstractions:lesasanabstractionofI/O,virtualmemoryasanabstractionofprogrammemory,andprocessesasanabstractionofarunningprogram.Totheseabstractionsweaddanewone:thevirtualmachine,providinganabstractionoftheentirecomputer,includingtheoperatingsystem,theprocessor,andtheprograms.Theideaofavirtualmachinewasintro- 1.10.SUMMARY23ducedbyIBMinthe1960s,butithasbecomemoreprominentrecentlyasawaytomanagecomputersthatmustbeabletorunprogramsdesignedformultipleoperatingsystems(suchasMicrosoftWindows,MacOSandLinux)ordifferentversionsofthesameoperatingsystem.Wewillreturntotheseabstractionsinsubsequentsectionsofthebook.1.10SummaryAcomputersystemconsistsofhardwareandsystemssoftwarethatcooperatetorunapplicationprograms.Informationinsidethecomputerisrepresentedasgroupsofbitsthatareinterpretedindifferentways,de-pendingonthecontext.Programsaretranslatedbyotherprogramsintodifferentforms,beginningasASCIItextandthentranslatedbycompilersandlinkersintobinaryexecutableles.Processorsreadandinterpretbinaryinstructionsthatarestoredinmainmemory.Sincecomputersspendmostoftheirtimecopyingdatabetweenmemory,I/Odevices,andtheCPUregisters,thestoragedevicesinasystemarearrangedinahierarchy,withtheCPUregistersatthetop,followedbymultiplelevelsofhardwarecachememories,DRAMmainmemory,anddiskstorage.Storagedevicesthatarehigherinthehierarchyarefasterandmorecostlyperbitthanthoselowerinthehierarchy.Storagedevicesthatarehigherinthehierarchyserveascachesfordevicesthatarelowerinthehierarchy.ProgrammerscanoptimizetheperformanceoftheirCprogramsbyunderstandingandexploitingthememoryhierarchy.Theoperatingsystemkernelservesasanintermediarybetweentheapplicationandthehardware.Itpro-videsthreefundamentalabstractions:(1)FilesareabstractionsforI/Odevices.(2)Virtualmemoryisanabstractionforbothmainmemoryanddisks.(3)Processesareabstractionsfortheprocessor,mainmemory,andI/Odevices.Finally,networksprovidewaysforcomputersystemstocommunicatewithoneanother.Fromtheviewpointofaparticularsystem,thenetworkisjustanotherI/Odevice.BibliographicNotesRitchiehaswritteninterestingrsthandaccountsoftheearlydaysofCandUnix[87,88].RitchieandThompsonpresentedtherstpublishedaccountofUnix[89].Silberschatz,Galvin,andGagne[98]pro-videacomprehensivehistoryofthedifferentavorsofUnix.TheGNU(www.gnu.org)andLinux(www.linux.org)Webpageshaveloadsofcurrentandhistoricalinformation.ThePosixstandardsareavailableonlineat(www.unix.org).

Related Contents


Next Show more