Computer Science from the Bottom up Ian Wienand Computer Science from the Bottom up Ian Wienand a PDF Version Is Available At
Total Page:16
File Type:pdf, Size:1020Kb
Computer Science from the Bottom Up Ian Wienand Computer Science from the Bottom Up Ian Wienand A PDF version is available at https://www.bottomupcs.com/csbu.pdf. A EPUB version is available at https:// www.bottomupcs.com/csbu.epub The original souces are available at https://github.com/ianw/bottomupcs Copyright © 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Ian Wienand This work is licensed under the Creative Commons Attribution-ShareAlike License. To view a copy of this license, visit http:// creativecommons.org/licenses/by-sa/3.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA. Table of Contents Introduction ................................................................................................................. xi Welcome ............................................................................................................. xi Philosophy ................................................................................................... xi Why from the bottom up? ........................................................................... xi Enabling Technologies ................................................................................ xi 1. General Unix and Advanced C ............................................................................... 1 Everything is a file! ............................................................................................ 1 Implementing abstraction .................................................................................... 2 Implementing abstraction with C ................................................................. 2 Libraries ....................................................................................................... 4 File Descriptors .................................................................................................... 5 The Shell ...................................................................................................... 8 2. Binary and Number Representation ..................................................................... 11 Binary — the basis of computing ...................................................................... 11 Binary Theory ............................................................................................ 11 Hexadecimal .............................................................................................. 17 Practical Implications ................................................................................. 19 Types and Number Representation .................................................................. 21 C Standards ............................................................................................... 21 Types ......................................................................................................... 22 Number Representation ............................................................................. 26 3. Computer Architecture .......................................................................................... 35 The CPU ............................................................................................................ 35 Branching ................................................................................................... 36 Cycles ........................................................................................................ 36 Fetch, Decode, Execute, Store ................................................................. 36 CISC v RISC ............................................................................................. 39 Memory .............................................................................................................. 41 Memory Hierarchy ..................................................................................... 41 Cache in depth .......................................................................................... 42 Peripherals and buses ....................................................................................... 46 Peripheral Bus concepts ............................................................................ 46 DMA ........................................................................................................... 48 Other Buses ............................................................................................... 49 Small to big systems ......................................................................................... 51 Symmetric Multi-Processing ...................................................................... 51 Clusters ...................................................................................................... 53 Non-Uniform Memory Access .................................................................... 54 Memory ordering, locking and atomic operations ...................................... 57 4. The Operating System .......................................................................................... 62 The role of the operating system ...................................................................... 62 Abstraction of hardware ............................................................................. 62 Multitasking ................................................................................................ 62 Standardised Interfaces ............................................................................. 62 Security ...................................................................................................... 63 Performance ............................................................................................... 63 iii Computer Science from the Bottom Up Operating System Organisation ........................................................................ 63 The Kernel ................................................................................................. 64 Userspace .................................................................................................. 69 System Calls ...................................................................................................... 69 Overview .................................................................................................... 69 Analysing a system call ............................................................................. 70 Privileges ........................................................................................................... 76 Hardware .................................................................................................... 76 Other ways of communicating with the kernel .......................................... 81 File Systems .............................................................................................. 82 5. The Process .......................................................................................................... 83 What is a process? ........................................................................................... 83 Elements of a process ...................................................................................... 83 Process ID ................................................................................................. 83 Memory ...................................................................................................... 84 File Descriptors .......................................................................................... 89 Registers .................................................................................................... 89 Kernel State ............................................................................................... 89 Process Hierarchy ............................................................................................. 90 Fork and Exec ................................................................................................... 90 Fork ............................................................................................................ 90 Exec ........................................................................................................... 91 How Linux actually handles fork and exec ................................................ 91 The init process ......................................................................................... 93 Context Switching .............................................................................................. 95 Scheduling ......................................................................................................... 95 Preemptive v co-operative scheduling ...................................................... 95 Realtime ..................................................................................................... 96 Nice value .................................................................................................. 96 A brief look at the Linux Scheduler ........................................................... 96 The Shell ........................................................................................................... 97 Signals ............................................................................................................... 98 Example ..................................................................................................... 99 6. Virtual Memory ...................................................................................................