內(nèi)存就是線性結(jié)構(gòu)的字節(jié)數(shù)組,對于32位平臺而言,這個字節(jié)數(shù)組的大小是2^32=4294967296個字節(jié),地址編號為:00000000-FFFFFFFF。
數(shù)據(jù)和代碼加載到內(nèi)存,對存儲設(shè)備的讀取是按塊(transer block,幾個字節(jié)、幾十、幾百、幾千個字節(jié)不等)進(jìn)行的。在CPU內(nèi)部,對數(shù)據(jù)的處理是以字長為單位進(jìn)行的。
1 空間局部性與時間局部性
Well-written computer programs tend to exhibit good locality. That is, they tend to reference data items that are near other recently referenced data items or that were recently referenced themselves. This tendency, known as the principle of locality, is an enduring concept that has enormous impact on the design and performance of hardware and software systems.
一個編寫良好的計算機(jī)程序常常具有良好的局部性(locality)。也就是,它們傾向于引用最近引用過的數(shù)據(jù)項的附近的數(shù)據(jù)項,或者最近引用過的數(shù)據(jù)項本身。這種傾向性,被稱為局部性原理(principle of locality)。局部性原理是一個持久的概念,對硬件和軟件系統(tǒng)的設(shè)計和性能都有著極大的影響。
Locality is typically described as having two distinct forms: temporal locality and spatial locality. In a program with good temporal locality, a memory location that is referenced once is likely to be referenced again multiple times in the near future. In a program with good spatial locality, if a memory location is referenced once, then the program is likely to reference a nearby memory location in the near future.
局部性通常有兩種不同的形式:時間局部性(temporal locality) 和空間局部性(spatiallocality), 在一個具有良好時間局部性的程序中,被引用過一次的內(nèi)存位置很可能在不遠(yuǎn)的將來再被多次引用。在一個具有良好空間局部性的程序中,如果一個內(nèi)存位置被引用了一次,那么程序很可能在不遠(yuǎn)的將來引用附近的一個內(nèi)存位置。
Programmers should understand the principle of locality because, in general, programs with good locality run faster than programs with poor locality. All levels of modern computer systems, from the hardware, to the operating system, to application programs, are designed to exploit locality. At the hardware level, the principle of locality allows computer designers to speed up main memory accesses by introducing small fast memories known as cache memories that hold blocks of the most recently referenced instructions and data items.
程序員應(yīng)該理解局部性原理,因為一般而言,有良好局部性的程序比局部性差的程序運(yùn)行得更快?,F(xiàn)代計算機(jī)系統(tǒng)的各個層次,從硬件到操作系統(tǒng)、再到應(yīng)用程序,它們的設(shè)計都利用了局部性。在硬件層,局部性原理允許計算機(jī)設(shè)計者通過引人稱為高速緩存存儲器的小而快速的存儲器來保存最近被引用的指令和數(shù)據(jù)項,從而提高對主存的訪問速度。
At the operating system level, the principle of locality allows the system to use the main memory as a cache of the most recently referenced chunks of the virtual address space. Similarly, the operating system uses main memory to cache the most recently used disk blocks in the disk file system. The principle of locality also plays a crucial role in the design of application programs. For example,Web browsers exploit temporal locality by caching recently referenced documents on a local disk. High-volumeWeb servers hold recently requested documents in front-end disk caches that satisfy requests for these documents without requiring any intervention from the server.
在操作系統(tǒng)級,局部性原理允許系統(tǒng)使用主存作為虛擬地址空間最近被引用塊的高速緩存。類似地,操作系統(tǒng)用主存來緩存磁盤文件系統(tǒng)中最近被使用的磁盤塊。局部性原理在應(yīng)用程序的設(shè)計中也扮演著重要的角色。例如,Web 瀏覽器將最近被弓]用的文檔放在本地磁盤上,利用的就是時間局部性。容量的Web 服務(wù)器將最近被請求的文檔放在前端磁盤高速緩存中,這些緩存能滿足對這些文檔的請求,而不需要服務(wù)器的任何干預(yù)。
Programs that repeatedly reference the same variables enjoy good temporal locality.
重復(fù)引用相同變量的程序有良好的時間局部性。
For programs with stride-k reference patterns, the smaller the stride, the better the spatial locality. Programs with stride-1 reference patterns have good spatial locality. Programs that hop around memory with large strides have poor spatial locality.
對于具有步長為k的引用模式的程序,步長越小,空間局部性越好。具有步長為 1 的引用模式的程序有很好的空間局部性。在內(nèi)存中以大步長跳來跳去的程序空間局部性會很差。
. Loops have good temporal and spatial locality with respect to instruction fetches. The smaller the loop body and the greater the number of loop iterations, the better the locality.
對于取指令來說,循環(huán)有很好的時間和空間局部性。循環(huán)體越小,循環(huán)迭代次數(shù)越多,局部性越好。
// C語言是以行優(yōu)先順序存儲數(shù)組。
#define COL 12
int sumArrRows(int(*a)[COL],int r)
{
int i,j,sum=0;
for(i=0;i<r;i++)
for(j=0;j<COL;j++)
sum += a[i][j]; // 逐行處理,具有良好的局部性
return sum;
}
int sumArrCols(int(*a)[COL],int r)
{
int i,j,sum=0;
for(i=0;i<r;i++)
for(j=0;j<COL;j++)
sum += a[j][i]; // 逐列處理,空間局部性差
return sum;
}
2 存儲器的層次結(jié)構(gòu)和傳送塊
Organizing memory systems, usually, adopts the technology , known as the memory hierarchy. In general, the storage devices get slower, cheaper, and larger as we move from higher to lower levels.
組織內(nèi)存系統(tǒng)通常采用被稱為內(nèi)存層次結(jié)構(gòu)的技術(shù)。一般來說,隨著我們從更高的級別移動到更低的級別,存儲設(shè)備變得更慢、更便宜、更大。
In general, a cache (pronounced “cash”) is a small, fast storage device that acts as a staging area for the data objects stored in a larger, slower device. The process of using a cache is known as caching (pronounced “cashing”).
一般而言,高速緩存(cache, 讀作“cash”)是一個小而快速的存儲設(shè)備,它作為存儲在更大、也更慢的設(shè)備中的數(shù)據(jù)對象的緩沖區(qū)域。使用高速緩存的過程稱為緩存(caching)。
The central idea of a memory hierarchy is that for each k, the faster and smaller storage device at level k serves as a cache for the larger and slower storage device at level k + 1. In other words, each level in the hierarchy caches data objects from the next lower level. For example, the local disk serves as a cache for files (such as Web pages) retrieved from remote disks over the network, the main memory serves as a cache for data on the local disks, and so on, until we get to the smallest cache of all, the set of CPU registers.
存儲器層次結(jié)構(gòu)的中心思想是,對于每個k,位于k層的更快更小的存儲設(shè)備作為位于k+1層的更大更慢的存儲設(shè)備的緩存。換句話說,層次結(jié)構(gòu)中的每一層都緩存來自較低一層的數(shù)據(jù)對象。例如,本地磁盤作為通過網(wǎng)絡(luò)從遠(yuǎn)程磁盤取出的文件(例如Web 頁面)的緩存,主存作為本地磁盤上數(shù)據(jù)的緩存,依此類推,直到最小的緩存—— CPU 寄存器組。
Data are always copied back and forth between level k and level k + 1 in block-size transfer units. It is important to realize that while the block size is fixed between any particular pair of adjacent levels in the hierarchy, other pairs of levels can have different block sizes.
數(shù)據(jù)總是以塊大小為傳送單元(transfer unit),在第k層和第k + 1 層之間來回復(fù)制。雖然在層次結(jié)構(gòu)中任何一對相鄰的層次之間塊大小是固定的,但是其他的層次對之間可以有不同的塊大小。
Figure below shows the general concept of caching in a memory hierarchy. The storage at level k + 1 is partitioned into contiguous chunks of data objects called blocks. Each block has a unique address or name that distinguishes it from other blocks. Blocks can be either fixed size (the usual case) or variable size (e. g. , the remote HTML files stored on Web servers). For example, the level k + 1 storage in Figure below is partitioned into 16 fixed-size blocks, numbered 0 to 15.
下圖展示了存儲器層次結(jié)構(gòu)中緩存的一般性概念。第 k+1 層的存儲器被劃分成連續(xù)的數(shù)據(jù)對象組塊(chunk), 稱為塊(block)。每個塊都有一個唯一的地址或名字,使之區(qū)別于其他的塊。塊可以是固定大小的(通常是這樣的), 也可以是可變大小的(例如存儲在Web 服務(wù)器上的遠(yuǎn)程HTML 文件)。例如,下圖中第k+1 層存儲器被劃分成16 個大小固定的塊,編號為0~15。
存儲層次結(jié)構(gòu):
3 編寫局部性良好的程序
Programs that repeatedly reference the same variables enjoy good temporal locality.
重復(fù)引用相同變量的程序有良好的時間局部性。
For programs with stride-k reference patterns, the smaller the stride, the better the spatial locality. Programs with stride-1 reference patterns have good spatial locality. Programs that hop around memory with large strides have poor spatial locality.
對于具有步長為k的引用模式的程序,步長越小,空間局部性越好。具有步長為1的引用模式的程序有很好的空間局部性。在內(nèi)存中以大步長跳來跳去的程序空間局部性會很差。
. Loops have good temporal and spatial locality with respect to instruction fetches. The smaller the loop body and the greater the number of loop iterations, the better the locality.
對于取指令來說,循環(huán)有好的時間和空間局部性。循環(huán)體越小,循環(huán)迭代次數(shù)越多,局部性越好。
4 緩沖
4.1 緩存(cache)是存儲分層的概念,在存儲器層次結(jié)構(gòu)中,高一層的存儲設(shè)備是低一層的緩存。
4.2 緩沖(buffer)是內(nèi)存的一塊區(qū)域,可以分為兩類:
4.2.1 用于IO的一塊臨時內(nèi)存存儲區(qū)域,用于IO數(shù)據(jù)的批量處理。
#include <stdio.h>
int main()
{
char buf[BUFSIZ];
setbuf(stdout, buf); // 以字符數(shù)組buf做為輸出緩沖區(qū),其大小為BUFSIZ
puts("This is runoob");
fflush(stdout);
int a;
scanf("%d",&a);
fflush(stdin); // The POSIX and C standards explicitly state that
// using fflush on an input stream is undefined behavior
getchar();
return(0);
}
BUFSIZ是STDIO.H中定義的一個宏:
#if defined(_M_MPPC)
#define BUFSIZ 4096
#else /* defined (_M_MPPC) */
#define BUFSIZ 512
#endif /* defined (_M_MPPC) */
4.2.2 在棧上定義的一個局部字符數(shù)組,也就是函數(shù)內(nèi)定義的字符數(shù)組。
如果字符數(shù)組是通過輸入來賦值,而輸入的長度超過字符數(shù)組的長度時,則會出現(xiàn)所謂緩沖區(qū)溢出的概念,如果溢出覆蓋了函數(shù)棧幀的返回地址,而覆蓋返回地址的字符是經(jīng)過特殊設(shè)計的,就有可出現(xiàn)所謂緩沖區(qū)溢出攻擊的情況。
void bufOverflow()
{
char buf[64];
gets(buf); // here is the stack buffer overflow bug
return;
}
更多細(xì)節(jié)請參考:C|輸入輸出緩沖區(qū)與緩沖區(qū)溢出
-End-
好了,這篇文章的內(nèi)容發(fā)貨聯(lián)盟就和大家分享到這里,如果大家網(wǎng)絡(luò)推廣引流創(chuàng)業(yè)感興趣,可以添加微信:80709525 備注:發(fā)貨聯(lián)盟引流學(xué)習(xí); 我拉你進(jìn)直播課程學(xué)習(xí)群,每周135晚上都是有實戰(zhàn)干貨的推廣引流技術(shù)課程免費(fèi)分享!