Programmers need to understand the hard core of knowledge Disk

This article is a "hard core programmers need to know knowledge" series fourth in the history of the article please poke

Programmers need to understand the hard core knowledge memory

Programmers need to understand the hard core knowledge of CPU

Binary programmers need to understand the hard core of knowledge

As we all know, is the five basic components of a computer 存储器, , 控制器, 运算器, 输入和输出设备which from the point of view of the storage function, can be divided into memory 内存and 磁盘the memory of our above article has been introduced, then this article we introduce disk and the relationship between memory and disk.

Recognize disk

First, disk and memory has a memory function, they are storage devices. The difference is that, through memory 电流capable of storing; magnetic disks is through 磁记录技术implemented store. Memory is a high speed, fraud expensive storage devices; and the disk is slower, cheaper storage device fraud; the computer is powered off, the data in memory will be lost, and the data on the disk can be long-term retention. Memory belongs 内部存储设备, it is part of the hard drive 外部存储设备. In general our computer, disk and memory are cooperating cooperative work.

Generally refers to memory main memory (responsible for storing the CPU operating program and data); refers to early disk floppy disk (soft disk, referred to as a floppy disk), the following is

(In 2000 I had my first computer aunt saw this, did not yet know what it is, know that now.)

The disk is now commonly used hard disk (hard disk, hard disk for short), is the following

The program does not read into memory will not run

Before understanding the disk, you also need to know how to run the mechanism of memory, our program is stored in the storage device, to achieve the execution of the program instructions by using the CPU reads. This mechanism is called 存储程序方式, in this way now appears to be taken for granted, but are running a previous program to read and write command by changing the wiring of the computer.

The main component is computer memory and disk storage. Disk stored in the program must be loaded into memory to run , saved on disk program can not be run directly, because responsible for parsing and run the program content of the CPU is required to specify the memory address by the program counter to read out the program instructions of.

Disk member

Disk cache

We mentioned above, disk, and memory is often a symbiotic relationship, mutual cooperation, holding a good working relationship with each other. Every memory will be read from disk data, is bound to read the same content, so there will be a role is responsible for storing the content we often need to read. We all do when the software is often used 缓存技术, then the hardware level is no exception, there are disk caching, disk cache is called 磁盘缓存.

It refers to a disk cache read out from the disk memory to the data storage mode, so that, when the next step is to read the same content, it will not by an actual disk, disk cache but by reading take. Emergence of a certain kind of technology or framework is bound to address an issue, then the disk cache will greatly improve the speed of disk access .

Windows operating systems provide disk caching technology, however, it is for most users feel disk cache, and with the evolution of computer hard disk access speed is also evolving, is actually the disk cache to Windows 95 / 98 already was beginning to gather.

The data stored in the high-speed low-speed devices apparatus, can be directly read out from the high-speed device when desired, such a cache in a web used widely, web browser to the remote web server and acquires the data via the network display it. Therefore, when reading the larger picture, it will spend a lot of time, when the web browser can obtain the data saved to disk, and then display the data as needed, when it is read again without re-loaded.

Virtual Memory

虚拟内存Is the second memory and disk media interaction. Virtual memory refers to the part of the disk as 假想内存to use. This is a hypothetical disk and disk cache (actually memory) relative, virtual memory is the imaginary memory (actually the disk).

Virtual memory is a computer system memory management technology. It makes the application thinks it has a 连续可用memory (a full address space), but in fact, it is usually divided into a plurality of physical debris, as well as on an external disk manager, data exchange when necessary part of the store.

The computer program should be run through the memory if the program memory for large memory space will be exhausted. To solve this problem, WINDOWS operating system uses virtual memory technology, through to come up with a portion of the hard disk as memory usage, to ensure that the program is still running out of memory space that can be stored. Virtual presence in the form of memory on the hard disk is PAGEFILE.SYSthe paging file.

By means of virtual memory, you can still run the program out of memory. For example, you can still run the program in the case of only 10MB 5MB memory space. Since the CPU is loaded into the program memory can only be performed, and therefore, virtual memory space you need and memory space 置换(swap), and then run the program.

Virtual memory and memory exchange

We have just mentioned part of the virtual memory and memory required to do the replacement before allowing the CPU to continue execution of the program, do the replacement way what is it? Which is divided into several ways to do?

Virtual memory methods 分页式and 分段式two kinds. Windows uses paged. This means, in the case where the configuration of the program is not considered, the run is divided according to a certain page size, and is replaced as a unit. In the tabbed we put the contents of the disk into memory is called Page In, the contents of memory written to disk is called Page Out. Page size is 4KB Windows computer, that is, the application needs to be carried out in accordance with 4KB page segmentation to page (page) as a unit into the disk, and then replaced.

In order to achieve memory function, Windows provides file (page file, page file) use virtual memory on the disk. This file is generated by Windows and management, virtual memory size, and the same file size, usually memory size is 1 - 2 times.

Save memory

Windows is a GUI-based operating system. Its predecessor was MS-DOCthe initial version of the program can be run on 128kb of memory, but at least now you want Windows to run smoothly flower needs 512MB of memory, but usually is often not enough.

Perhaps many people think we can use virtual memory to solve the shortage of memory, virtual memory can indeed provide a supplement in the memory of the time, but the use of virtual memory Page In and Page Out is usually accompanied by slow disk access, which is a more harm than good performance. Therefore, the virtual memory can not be resolved out of memory radically.

To address low memory situation fundamentally, either increase the memory capacity plus memory; either optimize the application, make it as small. The first recommendation often need to measure the pocket money, so we are only concerned with the second case.

Note: The following pages will relate to the introduction of the C language, knowledge of each programmer (not the language) needs to know and understand.

There are functions realized through DLL files

DLL(Dynamic Link Library)File is a 动态链接库file, by definition, is the runtime can be dynamically loaded Library(函数和数据的集合)files. In addition, multiple applications may share the same DLL file. And by sharing a DLL file you can achieve the effect of saving memory.

For example, suppose we write a function that has a certain processing functions MyFunc(). After run application A and B applications need to use this function, then built MyFunc () in the respective applications (this is called Static Link, static link) two applications, there is the memory of two of the same function programs, which will result in a waste of resources.

To change the executable file (EXE files) that use the DLL file instead of the application. Program because the same DLL file contents at runtime There may be multiple applications, there is the memory function MyFunc () is only a

Windows operating system is actually a collection of numerous DLL files. Some applications during installation, DLL files will be added. Application to run through these DLL files, which helps to save memory, you can upgrade without EXE files, DLL files can be upgraded to complete the update.

Program to reduce the file size by calling _stdcall

By calling the _stdcallmethod to reduce program files, can be used when writing applications using high-level C language skills. We come to know about what is _stdcall.

_stdcall is standard call(标准调用)an abbreviation. Function in the DLL files that Windows provides, basically done by calling _stdcall way, mainly in order to save memory. On the other hand, programs written in C default is not _stdcall. It called C language-specific invocation C 调用. The C language is not used by default _stdcall because the C language corresponding to the function parameter passing is variable, the function caller can only know in the end how many parameters, in this case, the stack of clean-up operations can not be . However, in the C language, if the number of parameters and functions fixed, specified _stdcall is no problem.

One of the C language and Java the most important difference is that the C language need to artificially control the release of the memory space

C language, after calling the function, the need for human execution stack clean-up instructions. On the stack area of memory used for unnecessary data is received from transfer function parameters and the cleaning operation is called out 栈清理处理.

For example, the following code

// 函数调用方
void main(){
  int a;
  a = MyFunc(123,456);
}

// 被调用方
int MyFunc(int a,int b){
  ...
}

Code, the main function call to the MyFunc from the main () method, in accordance with the default setting, the cleaning process stack in the appended main function of the main side. In the same program, there may be called multiple times, resulting in MyFunc () will clean up several times, which will result in a waste of memory.

After compilation of the code as follows

push 1C8h                               // 将参数 456( = 1C8h) 存入栈中
push 7Bh                                // 将参数 123( = 7Bh) 存入栈中
call @LTD+15 (MyFunc)(00401014)         // 调用 MyFunc 函数
add esp,8                               // 运行栈清理

C language function to pass parameters via the stack, using pusha command to the data stored in the stack, popis an instruction to retrieve data from the stack. 32-bit CPU, the instructions may be stored once push 4 bytes (32-bit) data. As a result of the above code two push operation, the stored 8 bytes of data. Through callto call a function instruction, the call is completed, the data stored in the stack is no longer required. By so they add esp, 8 this instruction, the stack that store data register esp proceeds 8 (set point high byte address 8), to clean up the data. Since the stack is in each case a memory field may be utilized, it is necessary to recover after use to its original state. The above operation is the execution stack cleanup. Further, in the C language, the function return value is returned by the stack to a register, not.

Stack cleanup work, perform cleanup work and to perform cleanup work at different method called repeatedly calling the method, the use of _stdcallthe way the standard is referred to repeatedly call the method invocation, execution stack clean-up overhead is relatively small in this case.

The physical disk structure

Before we introduced the CPU, memory, physical structure, and now we introduce the physical structure of the disk. Refers to the physical structure of the disk is in the form of a disk for storing data .

Through which the disk is divided into a plurality of physical space to the surface used. Divided in two ways: 可变长方式and 扇区方式. The former is a variable structure is divided into a length of the physical space, which is a disk structure is divided into fixed length spaces. Windows hard disk and floppy disks are generally used in the sector are used in this way. Sectors, the disk surface is divided into a plurality of concentric circles is space 磁道, the track division is made according to the size of the storage space is fixed扇区

扇区Is the smallest unit of physical disk reads and writes. Windows disks used, typically a sector is 512 bytes. However, Windows unit disk read and write in the logical aspects of the sector is an integral multiple of clusters. The different functions of the disk capacity, a cluster may be 512 bytes (1 cluster = 1 sector), 1KB (1 cluster = 2 sectors), 2KB, 4KB, 8KB, 16KB, 32KB (1 cluster = 64 sectors). Cluster and sector size are equal.

Whether hard drive or floppy disk, different file is not stored in the same cluster, otherwise it will lead to only one side of the file can not be deleted. So, no matter how small the file will occupy a cluster of space. As a result, all the files will occupy the space of one cluster integer multiple.

We use a floppy disk to do the experiment will be relatively simple, we first formatted floppy disk, floppy disk space formatted as follows

Next, we save a txtfile, and enter a character in the file, this time the document was in fact only takes up one byte, but we look at the properties of the disk and occupy a 512-byte

Then we continue to write something, when the file size reaches the 512 bytes of used space is 512 bytes, but when we continue to write a character, we will find that opening the properties of disk space becomes 1024 words section (= 2 clusters), we can prove by this experiment is the cluster disk units to save.

Reference article:

Disk

Disk cache

Virtual Memory

"Chapter IV how the program is run up."

http://www.intohard.com/article-436-1.html

The following publicity for himself to be a welcome public attention builders No. Java, the main number is Java technology stack, love technology, like reading, keen to share and summarize, I hope every one can give you a good article to share on the growth path. No. 002 draw public attention reply spree specially prepared for you, you will love and collections.

Guess you like

Origin www.cnblogs.com/cxuanBlog/p/11776310.html