In-depth understanding of computer systems (1): computer system roaming

In-depth understanding of computer systems (1): computer system roaming


​​ ​ ​ ​

The operating system can be seen as a layer of software inserted between the application program and the hardware. All applications attempting to operate the hardware must pass the operating system. The two basic functions of the operating system are as follows:

  • Prevent hardware from being misused by runaway applications
  • Provide applications with a simple and consistent mechanism to control complex and often very different low-level hardware devices.

These two basic functions are realized through several basic abstract concepts of process, virtual memory, and file. Files are abstract representations of I / O devices, virtual memory is abstract representation of main storage and disk I / O devices, and processes are abstract representations of processors, main storage, and I / O devices.
Insert picture description here

Explore the hello world

The following classic helloworld program is almost every programmer must write! So, when this hello program is executed on the system, what will happen to the system and why this happens, do you understand? Then start here from today's blog post!

#include<stdio.h>

int main(){
    printf("hello,world"\n);
    return 0;
}

We created and saved a text file through an editor, named hello.c (source program). This source program is actually a bit sequence (bit sequence) composed of 0 and 1, 8 bits are 1 byte, each byte represents some text characters in the program, these text characters are expressed using ASCⅡ code The ASCⅡ code actually corresponds to a different integer value.

At this point, we know that hello.c is stored in the file as a sequence of bytes, and each byte corresponds to an integer value, thus corresponding to certain characters. For example, the integer value of the first byte in hello.c is 35, which corresponds to the character #, and the integer value of the second byte is 105, and the corresponding character is i.

PS: A file composed only of ASCⅡ characters is called a text file, and the other files are binary files. Obviously hello.c is a text file.

The basic program has been written. Next, we need to translate this source program. Remember our common instructions?

linux>gcc -o hello hello.c

In order to run hello.c on the system, each C statement must be converted into a series of machine language instructions (lower than C language) by other programs, and then these instructions follow a format that becomes an executable target program Packaged and stored in the form of a binary disk file.

This instruction means that the GCC compiler driver reads the source program file hello.c and translates it into an executable object file hello. This translation process is divided into 4 stages. The preprocessor, compiler, assembler, and linker together constitute the compilation system.
Insert picture description here

Next, a brief introduction to each stage:

  • Pre-processing stage : The pre-processor (cpp) #modifies the original C program according to the commands beginning with characters . For example, the first line of #include<stdio.h>commands tells cpp to read the contents of the system header file stdio.h and insert it directly into the program text, so that you get another C program, usually with .i as the file extension.

  • Compilation stage : The compiler (ccl) translates the text file hello.i into a text file hello.s (including an assembly language program).

    main:
       subq		$8,%rsp
       movl		$.LC0,%edi
       call		puts
       movl		$0,%eax
       addq		$8,%rsp
       ret
    
  • Assembly stage : The assembler translates hello.s into machine language instructions, packages these instructions into a song called relocatable target program, and saves the result in the target file hello.o, which is a binary file .

  • Linking stage : Recall that the printf function is called in our hello program. This function is a function in the standard C library provided by each C compiler. It exists in a separate precompiled named printf.o The target file, and this file must be merged into our hello.o program in some way. The linker (ld) is responsible for the merge, and the result is a hello file, which is an executable file that can be loaded into memory and then executed by the system.

Ok, after the above translation, I finally got the executable file hello, then you need to enter the following command to run this file:

linux>./hello
hello,world
linux>

When we enter a string on the keyboard ./helloand hit enter, the shell program knows that we have ended the input of the command, and then the shell executes a series of instructions to load the executable hello file, these instructions will be in the hello target file The code and data are copied from disk to main memory. The data includes strings that will eventually be output hello,world\n.

At this point, the birth of a source program to how to display it has been completed ~

System hardware composition

In order to better understand what happens when running the hello program, we next briefly understand the hardware composition of the system, and then look at the execution of the hello program from this angle.

1. Bus

A set of electronic pipes that run through the entire system. The bus carries information bytes and is responsible for passing between the various components.

2.I / O devices

The I / O device is the communication channel between the system and the outside world. Four commonly used I / O devices: a mouse and keyboard as user input, a display as user output, and a disk for long-term storage of data and programs. When the "translation" task is completed, the hello program is stored on the disk. Each I / O device is connected to the I / O bus via a controller / adapter.

3. main memory

The main memory is a temporary storage device that is used to store programs and data processed by the program when the processor executes the program. The main memory is composed of a group of DRAM.

4. Processor

This is what we often say about the CPU. It is an engine that interprets instructions stored in main memory. The core of the processor is a storage device (or register) of one word in size, called a program counter (PC). At any time, the PC points to a certain machine language instruction in the main memory.

The processor reads the instruction from the memory pointed to by the PC, interprets the bits in the instruction, performs the simple operation indicated by the execution order, and then updates the PC to point to the next instruction, which is not necessarily the same as The instruction just executed in memory is adjacent.

Talking about simple operation examples:

  • Load: copy a byte or a word from the main memory to the register to overwrite the original content of the register
  • Storage: copy a byte or a word from a register to a location in main memory to overwrite the original content at this location
  • Operation: Copy the contents of the two registers to the ALU. The ALU performs arithmetic operations on these two words and stores the result in a register to overwrite the original contents of the register.
  • Jump: Take a word from the instruction itself and copy this word to the PC to overwrite the original value in the PC.

img

Let's take a look at the hello program ~

After we type on the keyboard ./hello, the shell program will read the characters into the registers one by one, and then put it into the memory.

[External chain image transfer failed, the source site may have an anti-theft chain mechanism, it is recommended to save the image and upload it directly (img-SSlFhIcp-1586767083410) (C: \ Users \ NayelyA \ AppData \ Roaming \ Typora \ typora-user-images \ image-20200413153902880.png)]

When you hit enter, the shell program knows that we have ended the input of the command, and 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 Save. The data includes strings that will eventually be output hello,world\n. Using DMA technology, the data can be passed without the processor 直接从磁盘到达主存.

[External chain image transfer failed, the source site may have an anti-theft chain mechanism, it is recommended to save the image and upload it directly (img-FosUIabI-1586767083411) (C: \ Users \ NayelyA \ AppData \ Roaming \ Typora \ typora-user-images \ image-20200413154052937.png)]

Once the code and data in the target file hello are loaded into the main memory, the processor begins to execute the machine language instructions in the main program of the hello program, these instructions hello,world\ncopy the bytes in the string from the main memory to the register The file is copied from the register file to the display device and finally displayed on the screen.

[External chain image transfer failed, the source site may have an anti-theft chain mechanism, it is recommended to save the image and upload it directly (img-D3gA29Lr-1586767083413) (C: \ Users \ NayelyA \ AppData \ Roaming \ Typora \ typora-user-images \ image-20200413154235338.png)]

Cache and memory hierarchy

Through the above introduction, the program has been copied a few times in total, where is it copied from? At the beginning, the machine instructions of the program are placed on the disk. When the program is loaded, it is copied to the main memory. When the processor runs the program, the instructions are copied from the main memory to the processor. These copy work will slow down the real work of the program, you can understand that these copy will bring some overhead.

According to the mechanical principle, larger storage devices run slower than smaller storage devices, and the cost of fast devices is much higher than similar low-speed devices. In response to the difference between the processor and main memory, a cache memory is designed to store information that the processor may need in the near future.

[External chain image transfer failed, the source site may have an anti-theft chain mechanism, it is recommended to save the image and upload it directly (img-gpfhXsk0-1586767083414) (C: \ Users \ NayelyA \ AppData \ Roaming \ Typora \ typora-user-images \ image-20200413154646140.png)]
The storage devices of each computer system have been organized into a memory hierarchy result, I must have seen this picture in class.

[External chain image transfer failed, the source site may have an anti-theft chain mechanism, it is recommended to save the image and upload it directly (img-Km3m5EUJ-1586767083414) (C: \ Users \ NayelyA \ AppData \ Roaming \ Typora \ typora-user-images \ image-20200413154808443.png)]
The main idea of ​​the memory hierarchy is that the upper layer of memory serves as a cache for the lower layer of memory .

Concurrency and parallelism

1. Thread-level concurrency

When building a system composed of multiple processors controlled by a single operating system kernel, we get a multi-processor system. With the advent of multi-core processors and hyper-threading, multi-processor systems have become common.

The multi-core processor is to integrate multiple CPUs into an integrated circuit chip. Below is a typical multi-core processor organizational structure, which has 4 CPU cores, each core has its own L1 and L2 cache, these cores share a higher level of cache and the interface to the main memory.

(External chain image transfer failed, the source site may have an anti-theft chain mechanism, it is recommended to save the image and upload it directly (img-mckLkfr1-1586767083415) (C: \ Users \ NayelyA \ AppData \ Roaming \ Typora \ typora-user-images \ image-20200413161327569.png)]
The concept of hyper-threading has also appeared in many facets, let's focus on the mark! Hyperthreading is sometimes called simultaneous multithreading, and it is a technology that allows a CPU to execute multiple control flows. Hyper-threading technology simulates the two logical cores inside a multi-threaded processor into two physical chips, which allows a single processor to use thread-level parallel computing and use idle CPU resources to complete more work in the same time.

2. Instruction set parallelism

The property that modern processors can execute multiple instructions simultaneously is called instruction-level parallelism. If the processor can achieve a faster execution rate than an instruction per cycle, it is called a superscalar processor, and most modern processors support superscalar operations.

3. Single instruction, multiple data parallel

Some processor hardware runs a single instruction to generate multiple operations that can be executed in parallel. This method is called single instruction and multiple data.

Amdahl's law

Amdahl's Law: To significantly accelerate the entire system, you must increase the speed of a considerable part of the entire system.
Insert picture description here

HomeWork

Insert picture description here
Insert picture description here
Insert picture description hereInsert picture description here

Published 201 original articles · Like9 · Visitors 10,000+

Guess you like

Origin blog.csdn.net/weixin_40992982/article/details/105491872