North Carolina DEMLR Stormwater Permitting Program's Response ...
CITS2002 Systems Programming, Lecture 1, p1 · Operating system services Creating and terminating...
Transcript of CITS2002 Systems Programming, Lecture 1, p1 · Operating system services Creating and terminating...
CITS2002SystemsProgramming
1 next→ CITS2002 CITS2002schedule
WelcometoCITS2002SystemsProgrammingThisunit,firstpresentedin2012asCITS1002,isoneofthecoreunitsineachofUWA'sComputerScience,andEngineeringScience(SE)majors.
Theunitexplorestheroleofcontemporaryoperatingsystemsandtheirsupportforhigh-levelprogramminglanguages,howtheymanageefficientaccesstocomputerhardware,andhowacomputer'sresourcesmaybeaccessedandcontrolledbytheCprogramminglanguage.
TheunitwillbepresentedbyChrisMcDonald-whichispronounced'Chris',not'Dr',not'Professor'.
OurUWAHandbookentry
UnderstandingtherelationshipbetweenaprogramminglanguageandthecontemporaryoperatingsystemsonwhichitexecutesiscentraltodevelopingmanyskillsinComputerScience.ThisunitintroducesthestandardCprogramminglanguage,onwhichmanyotherprogramminglanguagesandsystemsarebased,throughastudyofcoreoperatingsystemservicesincludingprocesses,inputandoutput,memorymanagement,andfilesystems.
TheClanguageisintroducedthroughdiscussionsonbasictopicslikedatatypes,variables,expressions,controlstructures,scopingrules,functionsandparameterpassing.MoreadvancedtopicslikeC'srun-timeenvironment,systemcalls,dynamicmemoryallocation,andpointersarepresentedinthecontextofoperatingsystemservicesrelatedtoprocessexecution,memorymanagementandfilesystems.Theimportanceofprocessscheduling,memorymanagementandinterprocesscommunicationinmodernoperatingsystemsisdiscussedinthecontextofoperatingsystemsupportformultiprogramming.Laboratoryandtutorialworkplaceastrongfocusonthepracticalapplicationoffundamentalprogrammingconcepts,withexamplesdesignedtocompareandcontrastmanykeyfeaturesofcontemporaryoperatingsystems.
TheUWAHandbookentryforthisunitstronglyrecommendsthatyoutakeoneofthreeunitsofAdvisablePriorStudybeforetakingthisunit-CITS1001,CITS1401,orCITS2401.Studentswhotookthisunitinrecentyears,andhadchosentonottakeoneoftheunitsofAdvisablePriorStudy,foundthematerialinthisunitdifficult.Thisunitisnotsuitableforfirst-timeprogrammers.
CITS2002SystemsProgramming,Lecture1,p1,27thJuly2020.
CITS2002SystemsProgramming
←prev 2 next→ CITS2002 CITS2002schedule
TopicstobecoveredinCITS2002SystemsProgrammingIt'simportanttoknowwherewe'reheading,sohere'salistoftopicsthatwe'llbecovering:
AnintroductiontotheISO-C99programminglanguageThestructureofaCprogram,basicdatatypesandvariables,compilingandlinking.
AnintroductiontoOperatingSystemsAbriefhistoryofoperatingsystems,theroleofcontemporaryoperatingsystems,therelationshipbetweenprogramminglanguages,programs,andoperatingsystems.
AnoverviewofcomputerhardwarecomponentsTheprocessoranditsregisters,thememoryhierarchy,inputandoutput(I/O)andstoragecomponents.
CprogramsingreaterdetailArraysandcharacterstrings,user-definedtypesandstructures,howthecomputerhardwarerepresentsdata,functions,parameterpassingandreturnvalues.
OperatingsystemservicesCreatingandterminatingprocesses,aprogram'sruntimeenvironment,command-linearguments,accessingoperatingsystemservicesfromC.
ManagingmemoryAllocatingphysicalmemorytoprocesses,sharingmemorybetweenmultipleprocesses,allocatingandmanagingmemoryinCprograms.
FilesandtheiruseinprogramsThefilemanagementsystem,fileallocationmethods,fileanddirectoryoperationsandattributes,fileinputandoutput(I/O),rawandformattedI/O,unbufferedandbufferedI/Ofunctions.
Bytheendofthisunityou'llhavethisknowledge-itjustwon'tallbepresentedstrictlyinthisorder.
Hereisourunit'sschedule.
CITS2002SystemsProgramming,Lecture1,p2,27thJuly2020.
CITS2002SystemsProgramming
←prev 3 next→ CITS2002 CITS2002schedule
WhyteachC?Sinceitsbeginningsinearly1973,theCprogramminglanguagehasevolvedtobecomeoneoftheworld'smostpopular,andwidelydeployedprogramminglanguages.ThelanguagehasundergoneextensiveformalstandardizationtoproducetheANSI-Cstandardin1989,theISO-C99standardin1999,ISO-C11(revision)inDec2011,andISO-C18inJune2018(whichintroducesnonewlanguagefeatures,onlytechnicalcorrectionsandclarificationstodefectsinC11).
Cistheprogramminglanguageofchoiceformostsystems-level,engineering,andscientificprogramming:
mostoftheworld'spopularoperatingsystems,Linux,WindowsandmacOS,theirinterfacesandfile-systems,arewritteninC,theinfrastructureoftheInternet,includingmostofitsnetworkingprotocols,webservers,andemailsystems,arewritteninC,softwarelibrariesprovidinggraphicalinterfacesandtools,andefficientnumerical,statistical,encryption,andcompressionalgorithms,arewritteninC,thesoftwareformostembeddeddevices,includingthoseincars,aircraft,robots,smartappliances,sensors,mobilephones,andgameconsoles,iswritteninC,thesoftwareontheMarsPhoenixLanderiswritteninC,muchofthesafety-criticalsoftwareontheF-35jointstrikefighter,iswritteninC,butCwasnotusedontheApollo-11mission!
IsCstillrelevant?[ref:Tiobesurvey](TheTiobesurveyisbasedonsearch-enginequeries-isnotaboutthebestprogramminglanguageorthelanguageinwhichmostlinesofcode havebeenwritten.)
Though,ofcourse,popularityisapoormeasureofquality-otherwise,McDonald'sRestaurantswouldreceiveMichelinstars.
Otherinterestingsurveys:
Stackoverflow'sDeveloperSurveyResults2020Jetbrains'TheStateofDeveloperEcosystemin2020HackerRank's2020DeveloperSkillsReport
CITS2002SystemsProgramming,Lecture1,p3,27thJuly2020.
CITS2002SystemsProgramming
←prev 4 next→ CITS2002 CITS2002schedule
SowhatisC?
Inonebreath,Cisoftendescribedasagoodgeneralpurposelanguage,anexcellentsystemsprogramminglanguage,andjustaglorifiedassemblylanguage.Sohowcanitbeallthree?
Ccanbecorrectlydescribedasageneralpurposeprogramminglanguage-adescriptionalsogiventoJava,Python,Visual-Basic,C++,andC#.
Cisaproceduralprogramminglanguage,notanobject-orientedlanguagelikeJava,(partsof)Python,Objective-C,orC#.
Cprogramscanbe"good"programs,iftheyare:
welldesigned,clearlywritten,writtenforportability,welldocumented,usehighlevelprogrammingpractices,andwelltested.
Ofcourse,theabovepropertiesareindependentofC,andareofferedbymanyhighlevellanguages.
Chasprogrammingfeaturesprovidedbymostproceduralprogramminglanguages-stronglytypedvariables,constants,standard(orbase)datatypes,enumeratedtypes,user-definedtypes,aggregatestructures,standardcontrolflow,recursion,andprogrammodularization.Cdoesnotoffertuplesorsets,Java'sconceptofclassesorobjects,nestedfunctions,subrangetypes,andhasonlyrecentlyaddedaBooleandatatype.Cdoeshave,however,separatecompilation,conditionalcompilation,bitwiseoperators,pointerarithmetic,andlanguageindependentinputandoutput.
CITS2002SystemsProgramming,Lecture1,p4,27thJuly2020.
Aprogramminglanguagethatdoesn'taffectthewayyouthinkaboutprogrammingisn'tworthknowing.— AlanPerlis,1stTuringAwardwinner
“
CITS2002SystemsProgramming
←prev 5 next→ CITS2002 CITS2002schedule
ASystemsProgrammingLanguageCisfrequently,andcorrectly,describedasanexcellentsystemsprogramminglanguage.
Calsoprovidesanexcellentoperatingsysteminterfacethroughitswelldefined,hardwareandoperatingsystemindependent,standardlibrary.
TheClanguagebeganitsdevelopmentin1972,asaprogramminglanguageinwhichtore-writesignificantportionsontheUnixoperatingsystem:
UnixwasfirstwritteninassemblylanguagesforPDP-7andPDP-11computers.
In1973DennisRitchiewasworkingonaprogramminglanguageforoperatingsystemdevelopment.BasinghisideasuponBCPL,hedevelopedBandfinallycreatedonecalledC.(Yes,thereisalanguagenamed'D',butit'snotadescendantofC)
Bytheendof1973,theUNIXkernelwas85%writteninCwhichenabledittobeportedtoothermachinesforwhichaCcompilercouldbefashioned.
ThiswasagreatstepbecauseitnolongertiedtheoperatingsystemtothePDP-7asitwouldhavebeenifitremainedinassemblylanguage.In1976DennisRitchieandStephenJohnstonportedUnixtoanInterdata8/32machine.Sincethen,UnixandLinuxhavebeenportedtoover260differentprocessorarchitectures.
Today,wellinexcessof95%oftheUnix,Linux,macOS,andWindowsoperatingsystemkernelsandtheirstandardlibraryroutinesareallwrittenintheCprogramminglanguage-it'sextremelydifficulttofindanoperatingsystemnotwrittenineitherCoritsdescendantsC++orObjective-C.
CITS2002SystemsProgramming,Lecture1,p5,27thJuly2020.
CITS2002SystemsProgramming
←prev 6 next→ CITS2002 CITS2002schedule
PortabilityondifferentarchitecturesCcompilershavebeenbothdevelopedandportedtoalargenumberandtypeofcomputerarchitectures:
from4-bitand8-bitmicrocontrollers,throughtraditional16-,32-,and64-bitvirtualmemoryarchitecturesinmostPCsandworkstations,tolarger64-and128-bitsupercomputers.
Compilershavebeendevelopedfor:
traditionallargeinstructionsetarchitectures,suchasIntelx86,AMD,ARM,Motorola680x0,SunSPARCs,andDEC-Alpha,newerreducedinstructionsetarchitectures(RISC),suchasSGIMIPS,IBM/MotorolaPowerPC,mobilephones,hometheatreequipment,routersandaccess-points,andparallelandpipelinedarchitectures.
CITS2002SystemsProgramming,Lecture1,p6,27thJuly2020.
CITS2002SystemsProgramming
←prev 7 next→ CITS2002 CITS2002schedule
AllitrequiresisaportedCcompilerOnceaCcompilerhasbeendevelopedforanewarchitecture,theterabytesofCprogramsandlibrariesavailableonotherC-basedplatformscanalsobeportedtothenewarchitecture.
Whataboutassemblylanguages?ItisoftenquotedthatacompiledCprogramwillrunonly1-2%slowerthanthesameprogramhand-codedinthenativeassemblylanguageforthemachine.
Buttheobviousadvantageofhavingtheprogramcodedinareadable,highlevellanguage,providestheoverwhelmingadvantagesofmaintainabilityandportability.
Verylittleofanoperatingsystem,suchasWindows,macOS,orLinux,iswritteninanassemblylanguage-inmostcasesthemajorityiswritteninC.
Evenanoperatingsystem'sdevicedrivers,oftenconsideredthemosttime-criticalcodeinanoperatingsystemkernel,todaycontainassemblylanguagenumberedinonlythehundredsoflines.
CITS2002SystemsProgramming,Lecture1,p7,27thJuly2020.
CITS2002SystemsProgramming
←prev 8 next→ CITS2002 CITS2002schedule
TheunreadabilityofCprogramsCisdescribedasnothingmorethanaglorifiedassemblylanguage,meaningthatCprogramscanbewritteninsuchanunreadablefashionthattheylooklikeyourmonitorissetatthewrongspeed.
(infactthere'sahumorouscontestheldeachyear,TheInternationalObfuscatedCCodeContesttodesignfullyworkingbutindecipherablecode,andtheUnderhandedCContestwhosegoalistowritecodethatisasreadable,clear,innocentandstraightforwardaspossible,andyetitmustfailtoperformatitsapparentfunction).
PerhapsC'sbiggestproblemisthatthelanguagewasdesignedbyprogrammerswho,folkloresays,werenotveryproficienttypists.
Cmakesextensiveuseofpunctuationcharactersinthesyntaxofitsoperatorsandcontrolflow.Infact,onlythepunctuationcharacters
@`and$
arenotusedinC'ssyntax!(andDEC-Conceusedthe$character,andObjective-Cnowusesthe@).
Itisnotsurprising,then,thatifCprogramsarenotformattedbothconsistentlyandwithsufficientwhitespacebetweenoperators,andifveryshortidentifiernamesareused,aCprogramwillbeverydifficulttoread.
Topartiallyaddresstheseproblems,anumberoftext-editors,integrateddevelopmentenvironments(IDEs),andbeautificationprograms(suchasindent)canautomaticallyreformatourCcodeaccordingtoconsistentspecifications.
CITS2002SystemsProgramming,Lecture1,p8,27thJuly2020.
CITS2002SystemsProgramming
←prev 9 next→ CITS2002 CITS2002schedule
CriticismsofC'sexecutionmodelCiscriticizedforbeingtooforgivinginitstype-checkingatcompiletime.
Itispossibletocastaninstanceofsometypesintoothertypes,evenifthetwoinstanceshaveconsiderablydifferenttypes.
Apointertoaninstanceofonetypemaybecoercedintoapointertoaninstanceofanothertype,therebypermittingtheitem'scontentstobeinterpreteddifferently.
BadlywrittenCprogramsmakeincorrectassumptionsaboutthesizeofitemstheyaremanaging.Integersof8-,16-,and32-bitscanholddifferentrangesofvalues.Poorchoices,orunderspecificationcaneasilyleadtoerrors.
Cprovidesnoruntimeprotectionagainstarithmeticerrors.
Thereisnoexceptionhandlingmechanism,anderrorssuchasdivision-by-zeroandarithmeticoverflowandunderflow,arenotcaughtandreportedatrun-time.
Coffersnoruntimecheckingofpopularandpowerfulconstructslikepointervariablesandarrayindices.
Subjecttoconstraintsimposedbytheoperatingsystem'smemorymanagementroutines,apointermaypointalmostanywhereinaprocess'addressspaceandseeminglyrandomaddressesmaybereadorwrittento.
AlthoughallarrayindicesinCbeginat0,itispossibletoaccessanarray'selementswithnegativeindicesorindicesbeyondthedeclaredendofthearray.
Thereareoccasionswheneachoftheseoperationsmakesense,buttheyarerare.
Cdoesnotholdthehandoflazyprogrammers.
Weavoidallofthesepotentialproblemsbylearningthelanguagewell,andemployingsafeprogrammingpractices.
CITS2002SystemsProgramming,Lecture1,p9,27thJuly2020.
CITS2002SystemsProgramming
←prev 10 next→ CITS2002 CITS2002schedule
Whatisthebestprogramminglanguage?Thequestion,evenarguments,ofwhetherC,Java,Visual-Basic,C++,orC#isthebestgeneralpurposeprogramminglanguageispointless.
Theimportantquestionis:"whichlanguageismostsuitedforthetaskathand?"
Thisunitwillanswerthequestions:
"whenisCthebestlanguagetouse?"and"howdowebestuseC'sfeaturesforsystemsprogramming?"
ThroughasequenceofunitsofferedbyComputerScience&SoftwareEngineeringyoucanbecomeproficientinawidevarietyofprogramminglanguages-procedural,object-oriented,functional,logic,set-based,andformal-andknowthemostappropriateonetoselectforanyproject.
CITS2002SystemsProgramming,Lecture1,p10,27thJuly2020.
CandC++areonlythefoundationbecausetheyhappenedtobecomepopularduetoabunchofmiscellaneousfactors,notbecausetheyareinherentlygreatinventionsinthemselves.Also,they(andtheirstandardlibraries)evolvedovertimetotheircurrentstate.
It'slikesayingEnglishandSpanisharethemostimportantlanguagesbecausetheyarefundamentallythe"best-invented"ones,notbecauseoftheaccidentsoffatethatwerecolonialexpansion,WWII,andtheInternet.— Anon
“
CITS2002SystemsProgramming
←prev 11 next→ CITS2002 CITS2002schedule
TheStandardizationofC-K&RC
DespiteC'slonghistory,beingfirstdesignedintheearly1970s,itunderwentconsiderablylittlechangeuntilthelate1980s.
Thisisaverylengthyperiodoftimewhentalkingaboutaprogramminglanguage'sevolution.
TheoriginalClanguagewasmostlydesignedbyDennisRitchieandthendescribedbyBrianKernighanandDennisRitchieintheirimaginativelytitledbookTheCProgrammingLanguage.
Thelanguagedescribedinthisseminalbook,describedasthe"K&R"book,isnowdescribedas"K&R"or"old"C.
228pages.
CITS2002SystemsProgramming,Lecture1,p11,27thJuly2020.
CITS2002SystemsProgramming
←prev 12 next→ CITS2002 CITS2002schedule
TheStandardizationofC-ANSI-C(K&R-2)
Inthelate1980s,anumberofstandardsformingbodies,andinparticulartheAmericanNationalStandardsAssociationX3J11Committee,commencedworkonrigorouslydefiningboththeClanguageandthecommonlyprovidedstandardClibraryroutines.TheresultsoftheirlengthymeetingsaretermedtheANSI-X3J11standard,orinformallyasANSI-C,C89,orC90.
TheformaldefinitionofANSI-Cintroducedsurprisinglyfewmodificationstotheold"K&R"languageandonlyafewadditions.
MostoftheadditionsweretheresultofsimilarenhancementsthatweretypicallyprovidedbydifferentvendorsofCcompilers,andthesehadgenerallybeenconsideredasessentialextensionstooldC.TheANSI-ClanguageisextremelysimilartooldC.Thecommitteeonlyintroducedanewbasedatatype,modifiedthesyntaxoffunctionprototypes,addedfunctionalitytothepreprocessor,andformalizedtheadditionofconstructssuchasconstantsandenumeratedtypes.
272pages.
CITS2002SystemsProgramming,Lecture1,p12,27thJuly2020.
CITS2002SystemsProgramming
←prev 13 next→ CITS2002 CITS2002schedule
TheStandardizationofC-ANSI/ISO-C99andISO/IEC9899:2011(C11)
AnewrevisionoftheClanguage,namedANSI/ISO-C99(knownasC99),wascompletedin1999.
Manyfeatureswere"cleanedup",includingtheadditionofBooleanandcomplexdatatypes,singlelinecomments,andvariablelengtharrays,andtheremovalofmanyunsafefeatures,andill-definedconstructs.
753pages.
ArevisionofC99,ISO/IEC9899:2011(knownasC11),wascompletedinDecember2011.
InthisunitwewillfocusexclusivelyonANSI/ISO-C99,andonlymentionotherversionsofCwhenthedifferencesaresignificant.
CITS2002SystemsProgramming,Lecture1,p13,27thJuly2020.
CITS2002SystemsProgramming
←prev 14 next→ CITS2002 CITS2002schedule
WhatStandardizationProvidesThesequiteformalstandardsspecifytheformandestablishestheinterpretationofprogramswrittenintheCprogramminglanguage.Theyspecify:
therepresentationofCprograms;thesyntaxandconstraintsoftheClanguage;thesemanticrulesforinterpretingCprograms;therepresentationofinputdatatobeprocessedbyCprograms;therepresentationofoutputdataproducedbyCprograms;therestrictionsandlimitsimposedbyaconformingimplementationofC.
Theydonotspecify:
themechanismbywhichCprogramsaretransformedforusebyadata-processingsystem;themechanismbywhichCprogramsareinvokedforusebyadata-processingsystem;themechanismbywhichinputdataaretransformedforusebyaCprogram;themechanismbywhichoutputdataaretransformedafterbeingproducedbyaCprogram;thesizeorcomplexityofaprogramanditsdatathatwillexceedthecapacityofanyspecificdata-processingsystemorthecapacityofaparticularprocessor;allminimalrequirementsofadata-processingsystemthatiscapableofsupportingaconformingimplementation.
CITS2002SystemsProgramming,Lecture1,p14,27thJuly2020.
CITS2002SystemsProgramming
←prev 15 CITS2002 CITS2002schedule
What's(deliberately)missingfromtheClanguage?Atfirstglance,theClanguageappearstobemissingsomecommonlyrequiredfeaturesthatotherlanguages,suchasJava,provideintheirstandards.
Forexample,Cdoesnotprovidefeaturesforgraphics,networking,cryptography,ormultimedia.
Instead,Cpermits,enables,andencouragesadditional3rd-partylibraries(bothopen-sourceandcommercial)toprovidethesefacilities.Thereasonforthese"omissions"isthatCrigorouslydefineswhatitdoesprovide,andrigorouslydefineshowCmustinteractwithexternallibraries.
Herearesomewell-respected3rd-partylibraries,frequentlyemployedinlargeCprograms:
Functiondomain 3rd-partylibrariesoperatingsystemservices(files,directories,processes,inter-processcommunication)
OS-specificlibraries,e.g.glibc,Win32,Carbon
web-basedprogramming libcgi,libxml,libcurl
datastructuresandalgorithms thegenericdatastructureslibrary(GDSL)
GUIandgraphicsdevelopment OpenGL,GTK,Qt,UIKit,Win32,Tcl/Tk
imageprocessing(GIFs,JPGs,etc) gd
networking Berkeleysockets,AT&T'sTLI
security,cryptography openssl,libmp
scientificcomputing NAG,Blas3,GNUscientificlibrary(gsl)
concurrency,parallelandGPUprogramming pthreads,OpenMPI,openLinda,CUDA,OpenCL
CITS2002SystemsProgramming,Lecture1,p15,27thJuly2020.