"In-depth understanding of computer systems" (CSAPP) reading notes-Chapter One Computer System Roaming

This chapter begins to learn the computer system by tracking the life cycle of the hello program. A source program starts when it is created by the programmer, runs on the system, outputs a simple message, and then terminates. We will briefly introduce some key concepts, technical terms and components that are gradually emerging along the life cycle of this program.

  I haven't updated the blog for a long time. From the National Day to now, I have been sorting out some information about autumn recruitment, resume templates, summary of embedded software interview knowledge points, sorting of autumn recruitment written examination questions, and face-to-face summary review. A total of nearly 400 pages, 16W words, have been sorted out. By the way, the information on Baidu Netdisk was also sorted out, and it was not finished until 10.16. (If you need information, there is my contact information on the homepage). I have to say that organizing information is really a human being.

  The next plan is to supplement the knowledge related to operating systems and computer composition principles. Let’s start with the book "In-depth understanding of computer systems", and study the book "In-depth understanding of computer systems" with CMU's video system. There are 9 Labs that can be used to deepen understanding. The initial plan is to have one chapter a week (I don’t know if it will work), and strive to finish this before the winter vacation.

  I will put some important knowledge points in the process of reading the book, the understanding of concepts and the detailed process of doing experiments in the blog, in-depth understanding of computer systems column . Welcome to follow my blog to get the updated content of the article as soon as possible.

  Below is a brief summary of the first chapter of this book.

How is the source program stored

#include <stdio.h>
int main()
{
    
    
	printf("hello,world\n");
	return 0;
}

  The above program is a text file created by a text editor and saved as hello.c. The source program is actually a sequence of bits (also called bits) composed of the values 0 and 1. The 8 bits are organized into a group, called a byte. Each byte represents some text character in the program. Modern computers use the ASCII standard to represent text characters. The ASCII text characters of the hello.c program are shown below.

image-20201019170810469

  The hello.c program is stored in the file as a sequence of bytes .

  The representation method of hello.c illustrates a basic idea: all information in the system, including disk files, programs in memory, user data stored in memory, and data transmitted on the network, are represented by a string of bits . The only way to distinguish different data objects is when we read the context of these data objects .

The process from source program to executable file

  The GCC compiler driver reads the source file hello.c and translates it into an executable object file hello. This translation process can be divided into four stages: pre-compilation, compilation, assembly, and linking.

image-20201019171458969

Precompiled

  In the pre-compilation process, it mainly deals with the preprocessing instructions in the source code, introduces header files, removes comments, and processes all conditional compilation instructions (#ifdef,#ifndef,#else,#elif,#endif), and macro replacement , Add the line number and keep all the compiler instructions .

Compile

  After the preprocessing is over, it is compiled. What the compilation process does is to perform grammatical analysis, lexical analysis, semantic analysis, symbol summary on the preprocessed file, and then generate assembly code .

compilation

  The assembly process converts the assembly code into a binary file, and the binary file can be read by the machine . Each assembly statement produces a sentence of machine language.

link

  The object file generated by the assembler cannot be executed immediately, and there may be many unsolved problems. For example, a function in a source file may refer to a symbol defined in another source file (such as a variable or function call, etc.); a function in a library file may be called in the program. All these problems can only be solved by the processing of the link program . The main job of the linker is to connect related object files with each other, that is, to connect the symbols referenced in one file with the definition of the symbol in another file, so that all these object files become one that can be installed by the operating system. Into the unified whole of execution .

what is the shell

  The shell is a command line interpreter. It outputs a prompt, waits for a command line to be entered, and then executes the command. If the first word of the command line is not a built-in shell command, the shell will assume that this is the name of an executable file, and it will load and run the file.

Hardware composition of a typical system

bus

  Running through the entire system is a set of electronic channels called buses. Usually, a fixed-length byte block is transmitted on the bus , that is, a word. The number of bytes in a word (word length) is a basic system parameter . Different systems have different word lengths. For example, the word length of a 32-bit system is 4 bytes, and the word length of a 64-bit system is 8 bytes.

IO device

  I/O (input/output) equipment is the communication channel between the system and the outside world . Our example system includes four I/O devices: a keyboard and mouse for user input, a monitor for user output , and a disk drive (in short, a disk) for long-term storage of data and programs .

  Each IO device is connected to the I/O bus through a controller or adapter . The main difference between the controller and the adapter lies in their packaging. The controller is the chipset on the I/O device itself or the main printed circuit board (usually called the motherboard) of the system. The adapter is a card inserted into the motherboard slot. In any case, their function is to transfer information between the I/O bus and I/O devices .

Main memory

  Main memory is a temporary storage device used to store the program and the data processed by the program when the processor executes the program. Physically speaking, the main memory is composed of a group of dynamic random access memory (DRAM) chips . Logically speaking, the memory is a linear byte array , each byte has its unique address (array index), these addresses start from zero.

processor

  The central processing unit (CPU), referred to as the processor, is an engine that executes instructions stored in main memory. The core of the processor is a storage device (or register) with a size of one word, called the program counter (PC). At any moment, the PC points to a certain machine language instruction in the main memory (that is, the address containing the instruction).

Run the hello program

  After the shell reads the "./hello" we input from the keyboard, the information flow in the computer is shown by the red line in the following figure:

  Keyboard->USB Controller->I/O Bus->I/O Bridge->System Bus->Register

  Register->System Bus->I/O Bridge->Memory Bus->Main Memory

  The shell program needs to use the user's input as a variable, and this variable must have an address in memory, so it will eventually reach the memory.

image-20201019191356753

  When we hit the Enter key on the keyboard, the shell program knows that we have completed the command input. Then the shell executes a series of instructions to load the executable hello file. These instructions copy the code and data in the hello target file from the disk to the main memory . The data includes the character string "hello,wor1d\n" that will eventually be output. The flow of information is as follows.

  Disk->Disk Controller->I/O Bus->I/O Bridge->Memory Bus->Main Memory

  This method of accessing data does not pass through the CPU, but directly from the disk to the main memory. This method is called DMA . DMA (Direct Memory Access) helps reduce the load on the CPU, allowing the CPU to perform other tasks while transferring data.

image-20201019191522808

  After loading the hello file, the CPU will start executing instructions from the main function of the hello program. These instructions copy the bytes in the string "hello, world\n" from the main memory to the register file, and then from the register file to the display device , and finally display on the screen. The flow of information is shown in the figure below.

  Main memory->register->system bus->I/O bridge->I/O bus->graphics adapter->display

image-20201019191644207

cache

  By running the hello program, we can know that instructions and data need to be copied back and forth between registers, main memory, and disk multiple times. These copies are actually overhead and slow down the speed of the program. This time we need to cache memory (cache memory) to solve this problem.

image-20201019195237101

  The capacity of the L1 cache can reach tens of thousands of bytes , and the access speed is almost as fast as the register file.

  L2 cache capacity of hundreds of thousands to millions of bytes , coupled to the processor via a special bus. The time for a process to access the L2 cache is 5 times longer than the time to access the L1 cache, but this is still 5 to 10 times faster than the time to access the main memory.

  L1 and L2 cache is called a static random access memory (SRAM) hardware techniques.

  The principle of cache locality : Programs have a tendency to access data and codes in local areas. Therefore, the cache memory serves as a temporary staging area for storing information that the processor may need in the near future .

Storage device hierarchy

  From top to bottom, the access speed of the device is getting slower and slower, the capacity is getting bigger and bigger, and the cost per byte is getting cheaper. The register file is at the top of the hierarchical structure, which is the 0th level or denoted as L0.

image-20201019200335061

  The main idea of ​​the memory hierarchy is that the upper memory is used as the cache of the lower memory . Therefore, the register file is the L1 cache, L1 is the L2 cache, L2 is the L3 cache, L3 is the main memory cache, and the main memory is the disk cache.

Operating system management hardware

  The operating system is a layer of software inserted between the application and the hardware. All attempts by applications to operate the hardware must pass through the operating system.

  The operating system has two basic functions : (1) Prevent the hardware from being abused by out-of-control applications; (2) Provide applications with a simple and consistent mechanism to control complex and often very different low-level hardware devices.

  The operating system implements these two functions through several basic abstract concepts (processes, virtual memory, and files): a file is an abstract representation of I/O devices, and virtual memory is an abstract representation of main memory and disk I/O devices. , The process is an abstract representation of the processor, main memory and I/O devices .

image-20201019201009252

Process & thread

  A process is an abstraction of a running program by the operating system. Multiple processes can run simultaneously on a system , and each process seems to be using hardware exclusively . Concurrent operation means that the instructions of one process and the instructions of another process are interleaved.

  Context: The operating system keeps and tracks all the state information (PC value, main memory content, etc.) required for the process to run.

  Context switching: The operating system controls the processor to switch between processes to achieve interleaved execution.

  The transition from one process to another is managed by the operating system kernel. The kernel is the main memory part of the operating system code. The kernel is not a separate process. Rather, it is a collection of code and data structures used by the system to manage all processes .

image-20201019203213287

A process is composed of multiple execution units called threads. Each thread runs in the context of the process and shares the same code and global data . Multithreading is easier to share data than multiple processes, and the overhead of switching between threads is much smaller than that of process switching.

Virtual Memory

  Virtual memory is an abstract concept, which provides an illusion for each process, that is, each process is exclusively using main memory . The memory that each process sees is consistent, which is called the virtual address space.

image-20201019203450101

  The above figure divides the virtual address space into several parts, and uses arrows to indicate the expansion direction of this part. The lowest address is 0, and the upward address gradually increases. The functions of each part are as follows:

   Program code and data: Store executable program code and global variables in the code.

  Heap: Memory variables used for dynamic application, such as mallocdynamic memory space applied by functions, can be expanded upward.

  Shared library: used to store the code and data of C language library functions. The printfcode and data in this example .

  Stack: Located at the top of the virtual address space, used for function calls, storage of local variables, etc. When we call a function, the stack expands downwards, and shrinks upwards when we return.

  Kernel virtual memory: The area at the top of the address space is reserved for the kernel. Applications are not allowed to read or write the contents of this area or directly call functions defined by the kernel code. Instead, they must call the kernel to perform these operations. For a 64-bit operating system, the user space is 0-3G, and the kernel space is 3G-4G. (What is the difference between user space and kernel space, see the summary of written interview knowledge points for embedded software engineers in the autumn recruitment materials)

Concurrency & Parallel

  Concurrency : means at the same time, there are multiple instructions executed on multiple processors simultaneously . So no matter from the micro or macro view, both are executed together.

img

  Parallel : It means that only one instruction can be executed at the same time, but the instructions of multiple processes are executed in a rapid rotation, making the effect of multiple processes executing at the same time at the macro level, but not at the same time at the micro level, only time Divide into several sections to enable multiple processes to execute alternately quickly .

img

Multi-core processor & multi-thread

  Multi-core processor : A multi-core processor integrates multiple CPUs (called "cores") on an integrated circuit chip. As shown in the figure below, the microprocessor chip has 4 CPU cores, each of which has its own L1 and L2 caches. The L1 cache is divided into two parts-one holds the most recently fetched instructions, the other Store data. These cores share higher-level caches and interfaces to main memory.

image-20201019212750093

  Hyper-threading : Hyper-threading, sometimes called simultaneous multi-threading (simultaneous multi-threading), is a technology that allows a CPU to perform multiple control flows . For example, the Intel Core i7 processor allows each core to execute two threads, so a 4-core system can actually execute 8 threads in parallel.

  Develop a habit, like first and then watch! If you think the writing is good, welcome one-click three-connection, thank you!

Copyright statement: This article is the original article of the blogger and follows the CC 4.0 BY-SA copyright agreement. Please attach the original source link and this statement for reprinting.
Link to this article: https://blog.csdn.net/qq_16933601/article/details/109169750

Guess you like

Origin blog.csdn.net/qq_16933601/article/details/109169750