File:System Programming Textbook from Illinois
Total Page:16
File Type:pdf, Size:1020Kb
Coursebook B. Venkatesh, L. Angrave, et Al. Contents 1 Introduction 1 1.1 Authors . 1 2 Background 7 2.1 Systems Architecture . 7 2.1.1 Assembly . 7 2.1.2 Atomic Operations . 7 2.1.3 Caching . 8 2.1.4 Interrupts . 8 2.1.5 Optional: Hyperthreading . 9 2.2 Debugging and Environments . 9 2.2.1 ssh . 9 2.2.2 git . 10 2.2.3 Editors . 12 2.2.4 Clean Code . 13 2.2.5 Asserts . 14 2.3 Valgrind . 14 2.3.1 TSAN . 16 2.4 GDB......................................................... 17 2.4.1 Involved gdb example . 19 2.4.2 Shell . 21 2.4.3 Undefined Behavior Sanitizer . 21 2.4.4 Clang Static Build Tools . 21 2.4.5 strace and ltrace . 21 2.4.6 printfs . 23 2.5 Homework 0 . 25 2.5.1 So you want to master System Programming? And get a better grade than B? . 25 2.5.2 Watch the videos and write up your answers to the following questions . 26 2.5.3 Chapter 1 . 26 2.5.4 Chapter 2 . 27 2.5.5 Chapter 3 . 28 2.5.6 Chapter 4 . 28 2.5.7 Chapter 5 . 29 2.5.8 C Development . 29 2.5.9 Optional: Just for fun . 30 2.6 UIUC Specific Guidelines . 30 2.6.1 Piazza . 30 3 The C Programming Language 33 ii CONTENTS CONTENTS 3.1 History of C . 33 3.1.1 Features . 34 3.2 Crash course introduction to C . 34 3.2.1 Preprocessor . 35 3.3 Language Facilities . 36 3.3.1 Keywords . 36 3.3.2 C data types . 48 3.3.3 Operators . 49 3.4 The C and Linux . 50 3.4.1 Everything is a file . 50 3.4.2 System Calls . 51 3.4.3 C System Calls . 51 3.5 Common C Functions . 51 3.5.1 Handling Errors . 51 3.5.2 Input / Output . 52 3.5.3 stdin oriented functions . 54 3.5.4 string.h . 56 3.6 C Memory Model . 58 3.6.1 Structs . 58 3.6.2 Strings in C . 61 3.6.3 Places for strings . 61 3.7 Pointers . 63 3.7.1 Pointer Basics . 63 3.7.2 Pointer Arithmetic . 65 3.7.3 So what is a void pointer? . 66 3.8 Common Bugs . 66 3.8.1 Nul Bytes . 66 3.8.2 Double Frees . 67 3.8.3 Returning pointers to automatic variables . 67 3.8.4 Insufficient memory allocation . 67 3.8.5 Buffer overflow/ underflow . 68 3.8.6 Strings require strlen(s)+1 bytes . 68 3.8.7 Using uninitialized variables . 69 3.8.8 Assuming Uninitialized memory will be zeroed . 69 3.9 Logic and Program flow mistakes . 69 3.9.1 Equal vs. Equality . 69 3.9.2 Undeclared or incorrectly prototyped functions . 70 3.9.3 Extra Semicolons . 70 3.10 Topics ..