Chapter 3. Computer Architecture the CPU
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, 2017, 2018, 2019 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 ............................................................................. 27 3. Computer Architecture .......................................................................................... 36 The CPU ............................................................................................................ 36 Branching ................................................................................................... 37 Cycles ........................................................................................................ 37 Fetch, Decode, Execute, Store ................................................................. 37 CISC v RISC ............................................................................................. 40 Memory .............................................................................................................. 42 Memory Hierarchy ..................................................................................... 42 Cache in depth .......................................................................................... 43 Peripherals and buses ....................................................................................... 47 Peripheral Bus concepts ............................................................................ 47 DMA ........................................................................................................... 49 Other Buses ............................................................................................... 50 Small to big systems ......................................................................................... 52 Symmetric Multi-Processing ...................................................................... 52 Clusters ...................................................................................................... 54 Non-Uniform Memory Access .................................................................... 55 Memory ordering, locking and atomic operations ...................................... 58 4. The Operating System .......................................................................................... 63 The role of the operating system ...................................................................... 63 Abstraction of hardware ............................................................................. 63 Multitasking ................................................................................................ 63 Standardised Interfaces ............................................................................. 63 Security ...................................................................................................... 64 Performance ............................................................................................... 64 iii Computer Science from the Bottom Up Operating System Organisation ........................................................................ 64 The Kernel ................................................................................................. 65 Userspace .................................................................................................. 70 System Calls ...................................................................................................... 70 Overview .................................................................................................... 70 Analysing a system call ............................................................................. 71 Privileges ........................................................................................................... 77 Hardware .................................................................................................... 77 Other ways of communicating with the kernel .......................................... 82 File Systems .............................................................................................. 83 5. The Process .......................................................................................................... 84 What is a process? ........................................................................................... 84 Elements of a process ...................................................................................... 84 Process ID ................................................................................................. 84 Memory ...................................................................................................... 85 File Descriptors .......................................................................................... 90 Registers .................................................................................................... 90 Kernel State ............................................................................................... 90 Process Hierarchy ............................................................................................. 91 Fork and Exec ................................................................................................... 91 Fork ............................................................................................................ 91 Exec ........................................................................................................... 92 How Linux actually handles fork and exec ................................................ 92 The init process ......................................................................................... 94 Context Switching .............................................................................................. 96 Scheduling ......................................................................................................... 96 Preemptive v co-operative scheduling ...................................................... 96 Realtime ..................................................................................................... 97 Nice value .................................................................................................. 97 A brief look at the Linux Scheduler ........................................................... 97 The Shell ........................................................................................................... 98 Signals ............................................................................................................... 99 Example ................................................................................................... 100 6. Virtual Memory ...................................................................................................