这里主要记录CSAPP这本书每个章节自己的读书笔记,按章分开。
It pays to understand how compilation systems work
就像原话说的,我们理解了汇编系统如何运作之后能带来巨大的好处。书中提了很多问题,全都是我之前没想过的问题。
Optimizing program performance.
- Is a switch statement always more efficient than a sequence of if-else statements?
- How much overhead is incurred by a function call?
- Is a while loop more efficitent than array indexs?
- Why does our loop run so much faster if we sum into a local cariable instead of an argument that is passed by reference?
- How can a function run faster when we simply rearrange the parentheses in an arithmetic expression?
Understanding link-time errors.
- What is the difference between a static variable and a global variable?
- What happens if you define two global variables in different C files with same name?
- What is the difference between a static library and a dynamic library?
- Why does it matter what order we list libraries on the command line?
- Why do some linker-related errors not appear until run time?
Avoiding security holes.
这些问题我几乎每一个能回答上来的,但有问题就代表有学习和进步的空间,学问学问,就是边学边问。那么就带着问题继续读呗!
硬件组织结构
figure1.4这张图给了硬件的组织结构
Buses
就是用来传输字节的电极管组成的结构。Buses are typically designed to transfer fixed-size chunks of bytes known as words.
I/O
输入输出
Main memory
main memory is a temporary storage device that holds both a program and the data it manipulates while the processor is executing the program. Physically, main memory consists of a collection of dynamic random access memory (DRAM) chips. Logically, memory is organized as linear array of bytes, each with its own unique address(array index) starting at zero. In general, each of the machine instructions that constitute a program can consist of a variable number of bytes. The sizes of data items that correspond to C program variables vary according to type. For example, short require 2 bytes, type int and float 4 bytes, long and double requires 8 bytes.
Processor
The central processing unit (CPU), or simply processor, is the engine that interprets (or executes) instructions stored in main memory. At its core is a word-size storage device (or register) called the program counter (PC).
At any point in time, the PC points at (contains the address of) some machine-language instruction in main memory
- Load. 读 从main memory 读到register
Copy a byte or a word from main memory into a register, overwriting the previous contents of the register - Store. 写进main memory
Copy a byte or a word from a register to a location in main memory, overwriting the previous contents of that location. - Operate
- Jump
Extract a word from the instruction itself and copy that word into the program counter, overwriting the previous value of the PC
By concurrently, we mean that the instructions of one process are interleaved with the instructions of another process.
Caches Matter
An important lesson from this simple example is that a system spends a lot of time moving information from one place to another. The machine instructions in the hello program are originally stored on disk. When the program is loaded, they are copied to main memory. As the processor runs the program, instructions are copied from main memory into the processor. Similarly, the data string "Hello, world\n", originally on disk, is copied to main memory and then copied from main memory to the display device. From a programmer's perspective, much of this copying is overhead that slows down the "real work" of the program. Thus, a major goal for system designers is to make these copy operations run as fast as possible.
所以为了解决这种需要反复从main memory里面读取数据的问题,硬件通常会用cache(缓存)来减少读取数据的时间,缓存也有很多层级,如下图所示
1.7.2 Threads
Although we normally think of a process as having a single control flow, in modern systems a process can actually consist of multiple execution units, called threads, each running in the context of the process and sharing the same code and global data. Threads are an increasingly important programming model because of the requirement for concurrency in network servers, because it is easier to share data between multiple threads than between multiple processes, and because threads are typically more efficient than processes. Multi-threading is also one way to make programs run faster when multiple processors are available
程序一般是按照这张图来进行运作的:
figure 1.13
- Program code and data. code begins at the same fixed address for all processes, followed by data locations taht correpond to global C variables. The code and data areas are initialized directly from the contents of an executable object file -- int our case, the hello executable.
- Heap. hte code and data areas are folowed immediately by the run time head. heap expands and contracts dynamically at run time as a result of calls to C standard library routines such as malloc and free
- Shared libraries. Near the middle of the address space is an area that holds the code and data for shared libraries such as the C standard library and the math library. the notion of a shared library is a powerful but somewhat difficult concept.
- Stack. At the top of the user's virtual address space is the user stack that the compiler uses to implement function calls. Like the heap, the user stack expands and contracts dynamically during the execution of the program.
- Kernel virtual memory. The top region of the address space is reserved for the kernel.
1.7.4 Files
A file is a sequence of bytes, nothing more and nothing less. Every I/O device is modeled as a file. ?!第一次知道
All input and output in the system is performed by reading and writing files,using a small set of system calls known as Unix I/O.
哦,这样你就不用管硬件是如何读写的了,只要知道他也是一个I/O device, 通过Unix I/O来实现数据的传输就可以了
Concurrency
With threads, we can even have multiple control flows executing within a single process.
网友评论