Fundamentals of System Programming

Fundamentals of System Programming

Fundamentals of System Programming Keio University’s Shonan Fujisawa Campus Spring 2015 3 Fundamentals of System Programming original by Hiroyuki Kusumoto adapted by Rodney Van Meter October 2, 2015 Contents Course Introduction iii 0.1 TotheIndependentReader . v 0.2 GoalsandStructureoftheClass . v 0.3 LearningOutcomes........................... vi 0.4 TotheInstructor ............................viii 0.5 This Class in the Keio Shonan Fujisawa Campus Curriculum . viii 0.6 WhyC?................................. x 0.7 WhyaNewBook? ........................... xi 0.8 HowtoReadthisBook ........................ xi 0.9 AdditionalRecommendedBooks . xii I Core Concepts and the C Programming Language in a UNIX Environment 1 1 The Traditional “Hello, World!” 3 1.1 Concepts ................................ 3 1.2 JavaandC ............................... 3 1.2.1 Object-oriented languages and procedural languages .... 3 1.2.2 InterpreterandCompiler . 4 1.2.3 Similarities ........................... 7 1.2.4 AFewDifferences ....................... 7 1.3 Clanguagebriefsummary. 8 1.3.1 OverallStructureofaProgram . 8 1.3.2 CompileandExecution . 8 1.3.3 Comments............................ 9 1.3.4 VariableandType . 10 1.3.5 Function............................. 12 1.3.6 FunctionandTypes . 14 1.3.7 AutomaticTypeConversion. 18 i ii CONTENTS 1.3.8 Statement............................ 19 1.3.9 Expression ........................... 22 1.3.10 Library ............................. 23 1.3.11 StandardInput/OutputLibrary . 24 1.4 Tools................................... 25 1.4.1 Gcc ............................... 25 1.4.2 Make .............................. 25 1.4.3 man(theUnixmanual) . 26 1.4.4 OtherUNIXutilitiesandcommands . 26 1.5 Exercises ................................ 26 2 Fundamental Data Types 29 2.1 Concepts ................................ 29 2.2 IntegerDataTypes........................... 29 2.3 FloatingPointNumberDataTypes . 33 2.4 Mixed arithmetic operation for an integer and a floating point number 34 2.5 Manipulating bitfields and bits within numbers . 34 2.6 Tools................................... 35 2.6.1 MoreonMake ......................... 35 2.7 Exercises ................................ 35 3 Arrays, Structs and Pointers 37 3.1 Concepts ................................ 37 3.2 Arrays.................................. 37 3.3 Pointers ................................. 41 3.3.1 Pointerexplanationandvalue. 42 3.3.2 Characterarraysandpointers. 44 3.3.3 Pointervariables . 47 3.3.4 Manipulationofpointervariables . 50 3.4 PointersandFunctions. 53 3.4.1 Functioncallsandarguments . 53 3.4.2 Howtopassarrays. 56 3.5 Structures................................ 59 3.6 Tools................................... 62 3.6.1 gcc: phasesofcompilation. 62 3.6.2 git ................................ 62 3.7 Exercises ................................ 62 CONTENTS iii 4 String Processing and Pointers 65 4.1 Concepts ................................ 65 4.2 Stringsasarrays ............................ 66 4.2.1 Copyingandconcatenatingstrings . 69 4.2.2 Comparisonofstrings . 72 4.2.3 Classifyingcharacters . 74 4.2.4 Stringsandnumericaldata . 76 4.3 Dealingwithnon-ASCIIcharacters . 80 4.4 Structuresandfunctions. 82 4.5 Tools................................... 86 4.5.1 ThePreprocessor. 86 4.6 Exercises ................................ 89 5 Memory Management, Scope in Naming, and More Control 91 5.1 Concepts ................................ 91 5.1.1 Memorymanagement . 91 5.1.2 Scope .............................. 92 5.2 Dynamicmemoryusewithpointers. 92 5.3 Scopeofvariables............................ 95 5.4 Controlstructures ........................... 99 5.4.1 switch ............................. 99 5.4.2 break ..............................102 5.4.3 continue statement ......................104 5.5 Tools...................................105 5.5.1 Interpretinggcc’serrormessages . .105 5.6 Exercises ................................106 6 Input/Output 111 6.1 Concepts ................................111 6.1.1 FilesandI/O..........................111 6.1.2 Commandlinearguments . .113 6.1.3 Environmentvariables . .114 6.2 StandardInput/Ouput. .114 6.3 Streaminputandoutput . .116 6.3.1 fopen()/fprintf()/fgets()/fclose() . 116 6.3.2 Handlingerrorsfromlibrarycalls. .118 6.3.3 Skippingaroundinafile. .121 6.4 Inputandoutputusingfiledescriptors . 121 6.4.1 open()/write()/read()/close() . 121 6.4.2 Handlingerrorsfromsystemcalls. .124 6.4.3 Skippingaroundinafile. .125 iv CONTENTS 6.5 mmap() .................................126 6.6 Makingfilenamesfromstrings . .126 6.7 Commandlinearguments . .127 6.7.1 Commandlineoptions . .129 6.8 Environmentvariables . .129 6.9 Tools...................................130 6.9.1 Make: building programs from more than one file . 130 6.10Exercises ................................133 First Interlude 137 7 Linked Lists and Recursion 139 7.1 Concepts ................................139 7.1.1 Linkedlists ...........................139 7.1.2 Recursion ............................140 7.2 Self-referentialstructures . 140 7.3 2-DArrays ...............................142 7.4 Recursion ................................142 7.5 Tools...................................143 7.5.1 GeneratingDependenciesinMake . 143 7.6 Exercises ................................143 8 Algorithms, Intermediate Data Structures, and Analysis 149 8.1 Concepts ................................149 8.1.1 Big-O notation: How long will my computation take? . 149 8.1.2 Sorting .............................151 8.1.3 BasicDataStructures . .151 8.1.4 BasicBinarySearchTrees . .153 8.1.5 Red-BlackBinaryTrees . .153 8.2 C.....................................156 8.3 Tools...................................156 8.3.1 gprof: profilingexecution . .156 8.4 Exercises ................................157 9 Finite State Machines 159 9.1 Concepts ................................159 9.2 C.....................................159 9.3 Tools...................................159 9.4 Exercises ................................159 CONTENTS v 10WritingandTestingSystemUtilities 161 10.1Concepts ................................161 10.2C.....................................162 10.3Tools...................................162 10.4Exercises ................................164 Second Interlude 165 II Achieving Scale in the Real World: Concurrency, Parallelism, Communication and 167 11 Soft Real-Time Computing 169 11.1Concepts ................................169 11.2C.....................................169 11.2.1 AnArduinomultitasker . .169 11.3Tools...................................172 11.4Exercises ................................172 12 Processes and Pipelines of Programs 173 12.1Concepts ................................173 12.2C.....................................173 12.3Tools...................................173 12.4Exercises ................................173 13Signals: CommunicatingAsynchronously 175 13.1Concepts ................................175 13.2C.....................................175 13.3Tools...................................175 13.4Exercises ................................175 14Threads:AbstractingProcessors 177 14.1Concepts ................................177 14.2C.....................................177 14.3Tools...................................177 14.4Exercises ................................177 15 Parallelism: OpenMP 179 15.1Concepts ................................179 15.2C.....................................179 15.3Tools...................................179 15.4Exercises ................................179 vi CONTENTS Third Interlude 181 16Communication:IPNetworkingBasics 183 16.1Concepts ................................183 16.2C.....................................183 16.3Tools...................................183 16.4Exercises ................................183 17Naming:theDomainNameService 185 17.1Concepts ................................185 17.2C.....................................185 17.3Tools...................................185 17.4Exercises ................................185 18 Communication: TCP Sockets 1 187 18.1Concepts ................................187 18.2C.....................................187 18.3Tools...................................187 18.4Exercises ................................187 19 Distribution: TCP Sockets 2 189 19.1Concepts ................................189 19.2Tools...................................189 19.3Exercises ................................189 A C Syntax Supplement 191 A.1 StorageClass ..............................191 A.1.1 volatile .............................197 A.1.2 const...............................198 A.2 Unions..................................199 A.3 Macros..................................199 A.3.1 Object-likemacros . .199 A.3.2 Function-likemacros . .200 A.3.3 Thetrapofmacros. .200 A.4 ConditionalCompilation. .201 A.4.1 Usingmacros ..........................201 A.4.2 Platformdependentcompilation . 204 A.5 Operators ................................205 A.5.1 Operatorprecedence . .205 A.5.2 Conditional operator for expressions . 205 A.5.3 =operator ...........................206 A.5.4 Incrementanddecrementoperators . 207 CONTENTS i A.5.5 Comma(,)operator . .208 A.6 FinePointsofExecutionOrder . .209 A.6.1 Undefinedbehavior. .209 A.6.2 Logicalexpressions . .210 A.7 FunctionPointers. .211 A.7.1 Example: the qsort libraryfunction . .211 A.7.2 Arrayofpointerstofunctions . .212 B Development Environment Addendum: MacOS 215 B.1 LLVM ..................................215 B.2 Clang ..................................215 B.3 LLDB ..................................215

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    244 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us