In-depth understanding of computer systems Roaming computer system (a)

The computer system roaming

Information is context-bit +

Let's look at the following code:

hello.c 

/* $begin hello */
#include <stdio.h>

int main () 
{
    printf("hello, world/n");
    return 0;
}
/* $end hello */

 

Hello life cycle of the program is from a source (or source files) start, that hello.c. A source bit is actually composed of values ​​of 0 and 1 (also called bits) sequence, eight bits are called a group of tissue, called a byte. Each byte represents some text characters in the program. Most modern computer systems are used to represent a standard ASCII text characters, in fact, in this way a unique integer value single byte to represent each character. Hello.c program such as ASCII code representation given in Figure 1-1:

 

Figure 1-1 hello.c ASCII text representation

 

hello.c program is stored sequence of bytes in a file, each byte has an integer value, corresponding to certain characters. For example, the first byte integer value is 35, which corresponds to the character '#'; second byte integer value of 105, which corresponds to the character 'I', and so on. Each text line is invisible to a newline '/ n' to the end, it corresponds to an integer value of 10. Hello.c like this only referred to a text file consisting of characters from the ASCII file, all other files are called binary file.

hello.c representation Description: All of the information system - comprising a disk file, a program memory, the user data stored in the memory and a data transmission on the network, are represented by a string of bits. The only way to distinguish between different data objects is the context in which we read these data objects. For example, in different contexts, a similar sequence of bytes may indicate an integer, float, string or machine instructions.

Program is translated into a different format other programs

Hello life cycle of the program is a high-level C language program from the beginning, because this form can be read person. However, in order to run on the system program hello.c, each C statement must be converted to low-level machine language other program instructions. Then follow these instructions executable format called target program packed, and as a binary disk files are stored together. Target program may also be referred to as the target executable file.

Now, let us use the compiler driver will be before the hello.c file is compiled into object files:

[root]# gcc -o hello hello.c 

  

GCC compiler driver read the source file hello.c, and translate it into an executable object file hello. This translation process can be divided into four stages completed, shown in Figure 1-2. Stage of the procedure performed by four (preprocessor, compiler, assembler and linker) together form a compiling system.

 

FIG compiling system 1-2

  • Pre-processing stage: the preprocessor (CPP) in accordance with the command beginning with # character, modify the original C program. For example #include <stdio.h> tells preprocessor reads the contents of the system header file stdio.h, and it directly into the program text. The results were another C program, usually .i file extension.
  • Compilation phase: Compiler (cc1) text file hello.i translated into a text file hello.s, which contains an assembly language program. The main program contains a function defined as follows:
main:
	subq     $8, %rsp
	movl     $.LCO, %edi
	call     puts
	movl     $0, %eax
	addq     $8, %rsp
	right

  

Each definition statement lines 2 to 7 are described in a text format a low level machine language instruction, the assembly language is very useful, because it provides a common output different languages ​​for different high-level language compilers. For example, it is the same assembly language output file C Compiler and Fortran compilers produced by the.

  • Compilation Phase: assembler (as) the hello.s translated into machine language instructions, these instructions format packaged into relocatable object program (relocatable object program), and the results are stored in the target file hello.o. hello.o is a binary file, which contains 17 bytes is a function of the main instruction encoding. If we open hello.o file in a text editor, you see is a pile of garbage.
  • Link connection: the Hello program calls printf function, which is a function of the standard C library provides the C compiler in. printf function exists in a separate pre-compiled object file named print.o in, and this file must be merged in some way to our hello.o program. Linker (ld) is responsible to deal with this merger. The results obtained on the hello file, which is an executable object file (or an executable file), which can be loaded into memory, executed by the system.

The processor reads and interprets the instructions stored in the memory

hello.c source code compiled into an executable object file system translate hello, and stored on disk. We can perform hello file in the following manner:

[root]# ./hello 
hello, world
[root]# 

  

In order to understand what happens when a program run hello, we need to understand the hardware organization of a typical system, shown in Figure 1-3:

Figure 1-3 a typical hardware system composed of

CPU: central processing unit; ALU: arithmetic / logic unit; PC: Program counter; USB: Universal Serial Bus

bus

Throughout the electronic system is a set of pipes, referred to as a bus, which carries information and is responsible for transmitting bytes between the various components. Bus is usually designed to transmit a fixed length block of bytes, i.e. (word). The number of bytes in a word (i.e., word length) is a basic system parameters of each system vary. Most machine word is 4 bytes (32 bits) or 8 bytes (64 bits).

I / O device

I / O (input / output) device is a system of communication passage with the outside world. Our example system includes four I / O devices: keyboard, mouse, display, and disk for long term storage of data and programs (or disk drive). Initially, the executable program hello to store on disk.

每个I/O设备都通过一个控制器或适配器与I/O总线相连。控制器与适配器之间的区别主要在于它们的封装方式。控制器是I/O设备本身或主印制电路板(通常称为主板)上的芯片组。而适配器则是一块插在主板插槽上的卡。但它们的功能都是在I/O总线和I/O设备之间传递信息。

主存

主存是一个临时存储的设备,在处理器执行程序时,用来存放程序和程序处理的数据。从物理上来说,主存是由一组动态随机存取存储器(DRAM)芯片组成。从逻辑上来说,存储器是一个线性的字节数组,每个字节都有其唯一的地址(数组索引),这些地址是从0开始的。

处理器

中央处理单元(CPU),简称处理器,是解释或执行存储再主存中指令的引擎。处理器的核心是一个大小为一个字的存储设备(或寄存器),称为程序计数器(PC)。在任何时刻,PC都指向主存中的某条机器语言指令(即含有该条指令的地址)。从系统开始通电到断电,处理器不断地在执行程序计数器指向的指令,再更新程序计数器,使其指向下一条指令。处理器看上去是按照一个非常简单的指令执行模型来操作的,这个模型是由指令集架构决定的。

在这个模型中,指令按照严格的顺序执行,而执行一条指令包含执行一系列的步骤。处理器从程序计数器指向的内存读取指令,解释指令中的位,执行该指令指示的简单操作,然后再更新PC,使其指向下一条指令,而这条指令并不一定和在内存中刚刚执行的指令相邻。
这样的操作并不多,它们围绕着主存、寄存器文件和算术/逻辑单元进行。寄存器文件是一个小的存储设备,由一些单个字长的寄存器组成,每个寄存器都有唯一的名字,ALU计算新的数据和地址值。CPU在指令的要求下可能会执行如下操作:

  • 加载:从主存复制一个字节或一个字到寄存器,以覆盖寄存器原来的内容。
  • 存储:从寄存器复制一个字节或一个字到主存的某个位置,以覆盖这个位置上原来的内容。
  • 操作:把两个寄存器的内容复制到ALU,ALU对这两个字做算术运算,将结果存放到一个寄存器中,以覆盖原来寄存器中的内容。
  • 跳转:从指令本身中抽取一个字,并将这个字复制到程序计数器(PC)中,以覆盖PC中原来的值。

运行hello程序

当我们在键盘上敲入"./hello"后,shell程序将字符逐一读入寄存器,再把它放到内存中,如图1-4:

图1-4   从键盘上读取hello命令

当我们敲入回车键时,shell程序就知道我们已经结束了命令的输入。然后shell执行一系列指令来加在可执行的hello文件,这些指令将hello目标文件中的代码和数据从磁盘复制到主存。数据包括最终会被输出的字符串"hello, world/n"。

利用直接存储器存取(DMA)技术,数据可以不通过处理器而直接从磁盘到达主存,如1-5所示:

图1-5   从磁盘加在可执行文件到主存

一旦目标文件hello中的代码和数据被加载到主存,处理器就开始执行hello程序的main程序中的机器语言指令。这些指令将"hello, world/n"字符串中的字符从主存复制到寄存器文件,再从寄存器文件复制到显示设备,最终显示在屏幕上。这些步骤如1-6所示:

图1-6   将输出字符串从存储器写到显示器

高速缓存至关重要

hello程序的机器指令最初是放在磁盘上,当程序加载时它们被复制到主存;当处理器运行程序时,指令又从主存复制到处理器。相似的,数据串"hello, world/n"开始是放在磁盘上,然后被复制到主存,最后从主存复制到显示设备。从程序员的角度来看,这些复制就是开销,减慢了程序“真正”的工作。因此,系统的设计者的一个主要目标就是使得这些复制操作尽快完成。

根据机械原理,较大的存储设备比较小的存储设备运行得慢,而快速设备的造价远高于同类的低速设备。比如说,一个典型系统上的磁盘驱动器可能比主存大1000倍,但对处理器而言,从磁盘驱动器上读取一个字的时间开销要比从主存中读取的开销大1000万倍。

类似地,一个典型的寄存器文件只存储几百字节的信息,而主存里可存放几十亿字节。然而,处理器从寄存器文件中读数据比从主存中读取几乎要快100倍。并且随着这些年半导体技术的进步,这种处理器与主存之间的差距还在持续增大。加快处理器的运行速度比加快主存的运行速度要容易和便宜得多。

针对这种处理器与主存之间的差异,系统设计者采用了更小更快的存储设备,称为高速缓存存储器(cache memory,简称为cache或高速缓存),作为暂时的集结区域,存放处理器近期可能会需要的信息。

图1-7展示了一个典型系统中的高速缓存存储器。位于处理器芯片上的L1高速缓存的容量可以达到数万字节,访问速度几乎和访问寄存器文件一样快。一个容量为数十万到数百万字节的更大的L2高速缓存通过一条特殊的总线连接到处理器。进程访问L2高速缓存的时间要比访问L1高速缓存的时间长5倍,但是这仍然比访问主存的时间快5~10倍。L1和L2高速缓存是用一种叫做静态随机访问存储器(SRAM)的硬件技术实现的。

图1-7   高速缓存存储器

比较新的、处理能力更强大的系统甚至有三级高速缓存:L1、L2和L3。系统可以获得一个很大的存储器,同时访问速度也很快,原因是利用了高速缓存的局部性原理,即程序具有访问局部区域里的数据和代码的趋势。通过让高速缓存里存放可能经常访问的数据,大部分的内存操作都能在快速的高速缓存中完成。

Guess you like

Origin www.cnblogs.com/beiluowuzheng/p/11080717.html