1.1 Information = Bits + Context
1. Bits: 0, 1
2. 8-bit chunk ==> bytes ==> character
3. 像 `hello.c` 这样只由 ASCII 字符构成的文件叫做 **text file(文本文件)**。所有其他文件都称为 **Binary File(二进制文件)**。
- 系统中所有信息都是Bits,用于区分不同数据对象的唯一方法就是 读context。
1.2 program 被其他programs 翻译成了不同形式
0. 首先以hello.c为例子简要说明一下:
C语言是高级语言,但是要执行,必须转化为低级机器语言指令(instruction),然后这些指令按照一种称为可执行目标程序(executable object program)的格式打包好,并以二进制磁盘文件(binary disk file)的形式存放起来。
在Unix系统中,执行这个过程的就叫做 compiler driver(编译器)。
1. 编译系统

pre-processing:
hello.c ---(translate)---> hello.i,就是把 #include的那些加到程序中compilation:
hello.i ---(translate)-----> hello.s(包含一个汇编语言程序(assembly-language program))。汇编语言非常哟用,所有编译器最终的输出的都是一样的汇编语言程序。assembly:
hello.s ----(translate)----> machine language instructions -----(package)-----> relocatable object program -----(store in)----> hello.o(binary file containing XX bytes to encode the instructions)linking: merge 用到的 XXfunction.o 到hello.o中,最终得到
hello。
1.3 了解编译系统如何工作大有益处
1.4 Processors Read and Interpret Instructions Stored in Memory
shell is a **command-line interpreter**
在shell 里面,像 下面这么一行, `linux>` 叫prompt
linux> ./hello
1.4.1 系统硬件的组成

Buses(总线)
1.1 就是带着bytes of information在各部件之间跑的electric conduit
1.2 Buses 被设计为transfer固定size bytes(words)的chunks。 word size(一个word中含有多少bytes)是一个基本的系统参数(系统和系统之间不同)。I/O devices
2.1 I/O 设备就是系统与外部的联系通道。
2.2 I/O 设备与I/O总线 控制器或适配器(这两者区别在于packaging方式)main memory(主存)
3.1 物理上,一堆DRAM(dynamic random access memory)
3.2 逻辑上,a linear array of bytes,每个都子unique address(array index) starting from 0-
processor(CPU)
4.1 engine that interprets (or executes) instructions stored in main memory
4.2 core is word-size storage device (or register) --> program counter (PC)
4.3 PC points at (contains the address of) some machine-language instruction in main memory.
4.4 从系统通电到断电,processor就一直在重复执行 被program counter指向的instruction 和 更新program counter来让它指向下个instruction
4.5 processor只有一些 围绕着 main memory, register file, ALU(算数逻辑单元 arithmetic/logic unit)的简单操作。- register file是包含着一系列word-size register 的存储设备,每个都有unique name。
- ALU是计算新数据和address values。
1.4.2 Running hello program
- shell program is executing its instructions, waiting for us to type a command
- we type
./helloat keyboard, shell reads each character into a register, then store into memory - we hit
enterat the keyboard, shell knows we're done. then loadhellofile by 执行一系列指令copy code and data inhelloobject file from disk to memory
1.5 Cache matters
1.6 Storage Devices Form a Hierarchy
1.7 OS manages hardware
我们可以把OS看作是 应用程序 和 硬件 之间插入的一层 **软件**。
操作系统有两个基本功能:
1. 防止硬件被不被控制的的应用程序滥用。
2. 向应用程序提供简单一致的机制来控制复杂又通常不相同的low-level 硬件设备。
操作系统通过 process(进程),virtual memory(虚拟内存) 和 Files 来实现这两个功能。

如上图,files 是对I/O设备的抽象表示。 VM是对主存+I/O设备的抽象表示。process是对processor + 主存 + I/O设备的抽象表示。
1.7.1 Process
当程序在运行的时候,OS会给我们一种假象:好像系统上只有这个程序在运行,好像独占了processor, main memory 和I/O设备。
process是OS对一个正在运行的程序的一种抽象。
concurrent: 一个进程的指令和另一个进程的指令是交错运行的 --> context switching
OS 追踪 process运行所需的所有 状态信息(context)(如program counter, register file的当前值,main memory 内容)
重要例子(shell进程和hello进程):
- 只有shell进程在运行,即等待输入。
- 当需要hello的时候,shell通过调用一个专门的函数(system call)来执行请求, system call 会讲控制权传递给OS
- OS保存shell进程context,创建新的hello 进程+context,将控制权交给hello。
- hello运行。
- hello运行结束后,OS恢复shell的context,并将控制权交给shell。
- shell继续等待输入。
一个进程到另一个进程的转换是OS的kernel 管理的,kernel是OS code 常驻main memory的部分。
注意:kernel不是一个独立的进程,而是系统管理 全部进程 所用的 **代码和数据结构的集合**。
1.7.2 Thread(线程)
一个进程由多个执行单元(thread)组成。每个thread都运行在process的context中,并share同样的code和全局数据。
1.7.3 Virtual Memory
VM为每个进程提供了一个假象:每个进程都在独占的使用main memory。
每个进程看到的memory都是一致的,成为virtual address space。

Linux中,最上面区域是保留给OS中的代码和数据的,这对所有进程来说都是一样的。
底部区域存放的是code and data defined by the user's process.
虚拟内存的运作需要硬件和OS软件间的精密复杂交互,包括对processor生成的每个地址的硬件翻译。基本思想是:把((一个进程的虚拟内存)的内容)存在disk上,然后用main memory作为disk的cache。
1.7.4 Files
Files 就是a sequence of bytes。所有I/O devices都是Files。系统中所有输入输出都是通过使用一小组称为 Unix I/O的系统函数 调用读写文件来实现的。
Files概念简单精致而强大,因为它向应用程序提供了 **uniform view** --> 来支持各种各样的I/O devices。
1.8 系统之间利用网络通信
网络可以视为一个I/O设备。

1.9 重要主题
1.9.1 Amdahl定律
当我们对系统的某个部分加速时,其对系统整体性能的影响取决于该部分的重要性和加速程度。
1.9.2 Concurrency and Parallelism
Concurrency: 一个同事具有多个活动的系统。
Parallelism: 用Concurrency来使一个系统运行的更快。
按照系统层次结构中由高到低的顺序强调三个层次:
1. 线程级并发(Thread-level concurrency)
构建在process(进程)之上,设计出有同时多个程序执行的系统 --> Concurrency
使用Thread(线程),能在一个process中执行多个control(控制流)。
2. 指令级并行(Instrcution-level Parallelism)
在较低的抽象层次上,现代处理器可以 同时执行多条指令的属性 称为Instruction-level parallelism。
如果处理器可以达到 比一个周期一条指令更快的执行速率,就成为 super-scalar(超标量)处理器。
3. 单指令、多数据并行(Single-Instruction, Multi-Data Parallelism)
最低层次上,特殊硬件,允许一条指令产生多个可以并行执行的操作 --> 单指令多数据并行。
如: 并行的对 8对float 做加法指令。
1.9.3 计算机系统中抽象的重要性
如API。无需了解内部工作便可以使用代码。
不同编程语言提供不同形式和等级的抽象支持,如Java的declaration 和 C的prototype。
1.10 Summary
计算机系统由硬件和系统软件组成,他们共同协作以运行应用程序。计算机内部的信息被表示为一组组的bits,他们依据context不同,翻译方式也不同。程序被其他程序翻译成不同的形式,从ASCII文本开始,然后被compiler 和 linker 翻译成binary executable files。
processor读取并翻译存放在main memory中的binary instructions。因为计算机花费了大量的时间在内存、I/O、CPU之间复制数据,所以将系统中的存储设备划分成层次结构。层次结构中较高层次的存储设备可以作为较低层次设备的cache。通过理解和运用这种存储层级结构的知识,程序员可以优化C程序的性能。
OS kernels 是应用程序和硬件之间的媒介。它提供了三个基本抽象:
Files <--> I/O devices
Virtual Memory <--> main memory and disk
Process <--> processor and main memory and I/O devices