Summary of Chapter 1 of In-depth Understanding of Computer Systems

 
 
#include<stdio.h>
int main(void){
print("hello world");
return 1;
}

The life cycle of the hello program starts from a high-level C language program source file. An executable file is generated through the preprocessor (cpp), compiler (cc1), assembler (as), and linker (ld).

Preprocessing stage: The preprocessor is processed according to the command starting with # in the source file. For example, #include(stdio.h) inserts the information of the stdio.h header file into the source file text. Generates a text file with an .i extension.

Compilation phase: The compiler translates the preprocessed text file into an assembly text file. Generates an assembly text file with a .s extension.

Assembly phase: The assembler translates the assembly file into a retargetable binary file, usually with an extension of .o.

Link phase: The hello program calls the function printf, which is a library function provided by every C compiler. The linker links hello.o and printf.o to generate an executable binary file.

Whether it is instructions, text, or numbers, computers only store them as binary data. The computer itself cannot identify what kind of data is stored. The same data may be represented as text, numbers, or instructions, and it can only be distinguished by context.

Run hello program

Initially, we input ./hello from the shell, and the shell reads the character stream from the keyboard, for example, when we enter the characters , characters go from the keyboard through the IO bus to the IO bridge through the system bus, to the CPU, and are saved to the memory through the memory bus. Then it goes to the CPU, and finally to the display (disregarding cache for now). When we press Enter, the shell determines that our input has ended

Then the shell program reads the character stream stored in the memory, and uses direct memory access technology (DMA) to directly transfer the hello program from the hard disk to the memory without going through the CPU.


The CPU reads the hello program in the memory, reads "hello world" from the memory to the CPU, and prints it to the display through the iO bus.

During the process of running the hello program, you can find that the computer spends a lot of time moving data from one place to another. This time is system overhead.

Modern CPUs have cache systems

Generally speaking, the faster the speed, the smaller the capacity, and the more expensive the unit price. Slower speeds generally have larger capacities and cheaper unit prices. Due to the principle of program locality, we can store programs cheaply through a cache system while running the program with limited memory.

Let's go back to the hello program. When we enter ./hello to call the hello program, neither the shell nor hello directly access the memory, hard disk, keyboard, or display. These are all operated indirectly through the calls provided by the operating system. We can think of the operating system as a layer of software between applications and hardware. It provides a simple and consistent interface to applications, shielding complex and widely different hardware resources. At the same time, it can prevent applications from abusing the hardware out of control. Applications provide both services through a simple abstraction.

Files: Files are a simple yet powerful concept. In the Linux system, all IO devices are abstracted into files, and files are byte streams. Writing a file means that the byte stream flows from the memory into the IO device, and reading the file means that the IO device byte stream flows into the memory. The concept of file provides a unified interface for different IO devices.

Virtual Memory: When a program is running, it seems to occupy all the memory. This is achieved through virtual memory. Virtual memory is an abstraction of memory and block devices (usually hard disks). It provides each program with a memory space that is much larger than the actual memory. At the same time, the memory occupied by each program does not seem to affect each other.

Process: Process is one of the most important concepts in modern computer science. A process provides an illusion to the application, like its own exclusive CPU and device resources. This is achieved by saving the context. When one process switches to another process, the operating system saves all the state information of the current process, the instruction counter, registers and the contents of the memory, and at the same time restores the state information saved by the next process to The state when last terminated. It seems like all processes keep running.

Guess you like

Origin blog.csdn.net/u013259665/article/details/80289511