Operating System Overview (II)

Operating system memory management

Memory management including memory management and virtual memory management.
Memory management program into a concept including such switching technology, a continuous mode and a discontinuous distribution management distribution management (paging, segmentation, page segment type).
Virtual memory management including virtual memory concept, demand paging management, page replacement algorithms, page allocation strategy, the working set.

Let's take a look at what is memory:

Memory is an important part of the computer system, only the program in memory to be executed by CPU, and the CPU running the necessary data and programs run space is fetched from memory, so the memory performance has a direct impact on our the quality of computer performance.

We talked about memory can talk about the classification of memory:

Memory allocation may be divided by function cache (Cache), a main memory (RAM), an external memory (external memory):

A cache memory (cache): cache is divided into a cache and secondary cache, a cache memory located inside the CPU, which is responsible for storing and transmitting data and instructions required CPU, CPU located in the secondary cache and the main memory between (DRAM), two role is to store those needed when the CPU, cache and can not store data. When the CPU reads data, starting with a cache to find, and then looking for can not be found from the secondary cache, and sometimes need to look for from three cache. The common denominator is slower reading speed than the CPU faster than memory , small memory capacity and high prices. cache appears mainly to solve the CPU speed and memory access speed does not match the contradiction, because the memory read and write speeds than the CPU speed is much faster, it would take a long time to wait for CPU data arrival or write data to memory.

The main memory (RAM): We said cell phone or computer memory is the main memory to run, the program will run in the external memory data exchange to the main memory, main memory stored inside may need to use a lot of CPU runtime data, it is faster to read and write characteristics, small in size. 4G-8G size generally.

External memory (external memory): external memory is what we often say that the hard disk, which is responsible for storing system programs and large data files and databases, is characterized by large capacity, low speed.

By category memory of us can be divided into the following categories:

The RAM
the RAM (Random Access Memory, Random Access Memory). Regardless of the position of the content storage unit can be freely removed or stored as needed, and the access speed of the memory cell of the memory. Such a memory when power is lost which stores content, it is mainly used for storing a program for a short time. Storing information in different, random access memory is divided into a static random access memory (Static RAM, SRAM) and dynamic random access memory (Dynamic RAM, DRAM).

SRAMS
the RAM (the RAM the Static, static random access memory), does not require refresh circuitry, data is not lost, and generally not multiplexed row and column address. But his relatively low degree of integration, not suitable for large capacity memory is generally used in the processor's cache inside.
SRAM is actually a very important memory, a wide range of its use. SRAM is very fast, the fast read and refresh to maintain data integrity. Internal SRAM is used in the form of a bistable circuit to store data. Therefore SRAM circuit configuration very complicated. SRAM manufactured in the same capacity is higher than the cost of the DRAM more. Because of this, only their development has been limited. Thus substantially only present SRAM cache used in the CPU and a built-in L2 cache. SRAM can be used on only a small number of network servers and routers.

A DRAM
the Dynamic the RAM, dynamic random access memory, at regular intervals is necessary to refresh the data, in order to save the data. And the ranks of address multiplexing, many with page mode. SDRAM is one of them.

The SDRAM
the SDRAM (Synchronous a DRAM, synchronous dynamic random access memory), i.e., the need to read and write data to synchronize clocks. A storage unit which is not linear arrangement is paged.
DRAM and SDRAM to achieve due process issues, large capacity than SRAM. But reading and writing as fast as SRAM.
SDRAM general embedded products which are used in memory. Computer memory is used in this RAM, called DDR SDRAM, their integration is very high, because it is dynamic, so there must be a refresh circuit, from time to time have to refresh the data.

A ROM
the Read-Only Memory, ROM general.
In the early development of the computer, BIOS, are stored in the ROM (Read Only Memory, ROM). The internal ROM information in the ROM in the manufacturing process, be burned in the factory into the special way in which the content can only be read can not be changed, once the burning inside, the user can verify the write data is correct, You can not make any changes. If the information is found to have no errors, only do not give up, re-made one. ROM is produced in the production line, due to the high costs, is generally used in the case of high-volume applications.

PROM
programmable read-only memory, can only be written once and wrong have to be scrapped, and now with very little, as if those relatively low cost microcontroller inside the OPT with the memory of it is this.

An EPROM 
an EPROM (Erasable Programmable a ROM, erasable programmable ROM) chip erase and write can be repeated, the solution can be written only once PROM chips drawbacks.
EPROM chip has an obvious feature, on its front side a ceramic package, a glass window is opened, through the window, the integrated circuit can be seen therein, the ultraviolet ray can be erased through the aperture illumination chip data in, and chip erase operations use the EPROM eraser.

EEPROMEEPROM (Electrically Erasable Programmable ROM, electrically erasable programmable read only memory), one kind of data is not lost after a power failure of the memory chip. Read-only memory EEPROM can be changed by the user, which can be erased and reprogrammed by the effect of higher than normal voltage (rewritable), which can erase the existing information on a computer or a dedicated device and reprogramming. Unlike chip EPROM, EEPROM can be away from the computer without modification, it is now used more than memory, such 24CXX series EEPROM.
In a EEPROM, when the computer is in use when reprogramming can be frequent, EEPROM's life is a very important design consideration parameters.

A flash memory (Flash)
flash memory (FLASH) is a nonvolatile memory, i.e., power data will not be lost. Because, unlike flash RAM (Random Access Memory) as rewriting data in bytes, and therefore it can not replace RAM.   
Flash memory card (Flash Card) is the use of flash memory (Flash Memory) technology to the electronic information stored in memory, generally used in digital cameras, handheld computers, MP3 and other small digital products as the storage medium, so look small, like a card, so called flash memory card. According to different manufacturers and different applications, flash memory cards about U disk, SmartMedia (SM Card), Compact Flash (CF card), MultiMediaCard (MMC cards), Secure Digital (SD card), Memory Stick (Memory Stick), XD-Picture card (XD card) and a micro hard disk (MICRODRIVE). These flash memory cards, although different appearance, specifications, but the principle is the same technology.

NAND FLASH and NOR FLASH are now used more than a non-volatile flash memory.
Execution of the program

To execute the program, and program data into memory. The user source program becomes executable in memory, usually requires the following steps:

编译:由编译程序将用户源代码编译成若干个目标模块。
链接:由链接程序将编译后形成的一组目标模块,以及所需库函数链接在一起,形成一个完整的装入模块。
装入:由装入程序将装入模块装入内存运行。

Link program is divided into the following categories:

静态链接:程序在执行前,将各个模块以及它们需要用到的库函数链接成可执行文件.
装入时动态链接:程序在编译完之后得到一组目标模块,在装入内存时链接在一起.
运行时动态链接:程序在运行前还是离散的,在运行时把运行所需要的模块链接在一起.

Program loader into the following categories:

绝对装入:在编译时,如果知道程序将驻留在内存的某个位置,编译程序将产生绝对地址的目标代码。绝对装入程序按照装入模块中的地址,将程序和数据装入内存。由于程序中的逻辑地址与实际内存地址完全相同,故不需对程序和数据的地址进行修改。
可重定位装入:在多道程序环境下,多个目标模块的起始地址通常都是从 0 开始,程序中的其他地址都是相对于起始地址的,此时应釆用可重定位装入方式。根据内存的当前情况,将装入模块装入到内存的适当位置。装入时对目标程序中指令和数据的修改过程称为重定位,地址变换通常是在装入时一次完成的,所以又称为静态重定位。
静态重定位的特点是在一个作业装入内存时,必须分配其要求的全部内存空间,如果没有足够的内存,就不能装入该作业。此外,作业一旦进入内存后,在整个运行期间不能在内存中移动,也不能再申请内存空间。
动态运行时装入,也称为动态重定位:程序在内存中如果发生移动,就需要釆用动态的装入方式。装入程序在把装入模块装入内存后,并不立即把装入模块中的相对地址转换为绝对地址,而是把这种地址转换推迟到程序真正要执行时才进行。因此,装入内存后的所有地址均为相对地址。这种方式需要一个重定位寄存器的支持。
动态重定位的特点是可以将程序分配到不连续的存储区中;在程序运行之前可以只装入它的部分代码即可投入运行,然后在程序运行期间,根据需要动态申请分配内存;便于程序段的共享,可以向用户提供一个比存储空间大得多的地址空间。

System partition

Memory is divided into two areas, one used to store the operating system, used to store a user process, when the process is transferred to the memory we need to allocate a block of memory space for it, here is the contiguous memory allocation we use, store in a process a continuous period of memory space.
Memory Protection

Memory protection is to prevent the operating system process, or affect the user into the user process affects other processes, the operating system to achieve this protection by using the relocation register and the boundary address register, re-location register which contains the smallest physical address, the address register contains the smallest sector logical address, the logical address of each process must be less than boundary address register, the memory management unit dynamically logical addresses and sector addresses are compared, if not the occurrence of cross-border, plus the relocation register address value to find a physical address, sending to the memory cell.
Contiguous memory allocation management

To be able to program into memory, memory space must be allocated a certain amount of it, is a contiguous allocation of memory allocation · earliest, the distribution of user program allocates a contiguous memory space, i.e., the code of the program or adjacent logical address of data, reflect the allocation of memory space adjacent to the physical address. Contiguous allocation can be divided into four categories: single continuous fixed partition assignment, dynamic partition allocation and assignment relocatable partition (compact) method in four ways:

单一连续分配。内存此时分为系统区和用户区,系统区只分配给操作系统使用,通常在低地址部分;用户区为用户提供。内存中只有一道程序,也无需进行内存保护。无外部碎片但是有内部碎片,且存储器效率低下。
固定分区分配。将内存空间划分为若干个固定大小的区域,每个分区只能装入一道作业。当有空闲分区时,便可以再从外存的后备作业队列中,选择适当大小的作业装入该区,分为(分区大小相等和分区大小不相等两种方式)无外部碎片但是有内部碎片(分区内部有空间的浪费),且存储器效率低下,但是可存在多道程序,是用于多道程序并发执行的最简单的内存分配方式。

动态分区分配。也成为可变分区分配,它不预先对内存进行划分,而是在进程装入内存时,根据进程的大小动态的建立分区,并使分区的大小正好适合进程的需要,其分区的数目和大小是可变的。但是随着时间的推移,很容易产生外部碎片,外部碎片指的是分区以外的存储空间被浪费
基于顺序搜索的动态分区分配算法:

首次适应算法,空闲分区以地址递增的方式链接,分配内存时顺序查找,找到大小满足要求的第一个空闲分区,通常该算法是最快最好的也是最简单的。http://www.daiqiyang.com
最佳适应算法,空闲分区以容量递增形成分区链,找到第一个满足要求的空闲分区,实际上新能不佳,因为每次最佳分配通常会留下很小的难以利用的内存块,产生外部碎片。
最坏适应算法,又称最大适应算法,空闲分区以容量递减形成分区链,找到第一个满足要求的空闲分区,也就是挑选出最大的分区性能较差,因为算法开销也是需要考虑的一部分
邻近适应算法,又称循环首次适应算法,也就是从首次适应算法中演变而来,不同的是,从上次查找结束的位置开始继续查找性能较差

Segmentation and paging mechanism

To understand segmentation and paging, we need to know why there is both segmentation and paging technologies;

First of all we need to know segmentation and paging is to better manage computer resources - memory.

Before segmentation techniques have not come out, the program is to start looking at memory in memory before running to find a contiguous address space, such as we need space A 10M program, then we must find out the address space from 10M memory space and then loaded into a, if not find contiguous space, then it is not allocated.

From here we can see the shortcomings of previous memory allocation;

地址空间不隔离,假如我们两个程序A和B,A存放在0x000001~0x0000999这个内存空间,B存放在0X0001000~0x0001100这个空间里,那如果我们对A的操作地址0x0000500误写为0x0001050,那么我们不仅没有对A实施正确的操作,还影响了程序B的执行。
程序地址的不确定,因为我们现在都是对内存地址直接操作,所以我们程序需要写死要操作的某个内存的地址,但是如果我们想要操作地址0x0000001000,我们就必须写死我们要操作的地址为0x0000001000,但是这样问题来了,我们程序运行时在内存的地址在不同时候可能不一样,比如我们第一次可能在内存中的地址为0x0000000001~0x00000100000;那么我们第一次操作可能就没有问题,但是第二次运行时他在内存中的地址可能就变为0x000000100000~0x00000110000,那么我们第二次运行是我们想要操作的地址根本程序占有的内存里,我们可能又误操作其他程序的数据了。
内存使用率低,假如我们内存大小为70M,我们有A B C三个程序在内存中分别连续占20M  10M  30M的地址空间,当我们B运行完被置换出来时A和C中间就有10M的内存空间,然后我们有大小为11M的程序D想要装入内存,然后它从内存中寻找一个11M的连续地址空间,然后我找到了两个10M的地址空间,但是它们是不连续的,所以程序没办法装入内存中,只能等待A或者C运行完才能装入,这样这20M的内存空闲地址就没有被利用起来。

To solve these problems, people went looking for a way to solve it, so segmentation technology came out:

In order to achieve segmentation technology, people and introduced the concept of virtual address space, what is the virtual address space, that is, that there is no real space, a concept but we operate in order to better address the physical and leads, in short, we program before running its operations are virtual addresses; for example, program a of the virtual address space is 0x00000100 ~ 0x100000200, in this case, we only need a contiguous physical memory to store a, also need to be mapped to virtual address a physical address space, virtual address space may be mapped to a from 0x00000100 ~ 0x100000200 to the physical address space 0x00000000 ~ 0x00000100;

So what segmentation is it?

Segmentation mechanism is the virtual memory organized into memory units called variable-length segment, the segment table is managed by segments, each segment defines a logical set of information, each program can have multiple segments, such as data section, other code segments, each segment is a contiguous address space segment length determined by the size of the program, the length of each segment vary, so that the address space is two-dimensional sections, and paragraphs from the first address length composition;

What is the role of segmentation?

Sharing and protection segments: Here we talk about protection segment, the segment is divided into cross-border protection mechanisms to check and authorization checks, bounds checking is stored in the segment table register with the segment table information, which includes the starting address and paragraphs length, during memory access, the first segment number and the segment table length by comparing logical address space, if the segment table number greater than or equal lengths, will issue an interrupt signal range address, followed by the segment address is greater than also check segment length, segment length is greater than if the cross-border generate an interrupt signal to ensure that each process can run in its own address space. Permission check is in each segment table entry are provided 'access control' field, the access mode for a predetermined period, usually has read-only access mode, performing only, read / write. You can solve the problems affecting other programs of a program to protect and share by segment.

Address translation mechanism segment: the segment management can provide the logical addresses to physical addresses, logical addresses to physical addresses by the address mapping table is complete, so only the virtual address does not change, then we do not need to be concerned about the physical address of the program memory is What, so you can solve the problem by 2 address mapping mechanism segment.

3 But our problem is not resolved, memory utilization is still not resolved, so we leads paging management

Paging supervisor is the main memory space is divided into equal and fixed block size, the block size is small, as the basic unit of the main memory, a page table stores basic information of these pages, a page table corresponding to each process, (Process blocks called pages, main memory in blocks called frames, referred to as the external memory blocks, when performing the process, the application main memory block, the generated page to page frame mapping relationship) as Figure:

How to use the paging mechanism to solve the external fragmentation, let's use the paging unit to a group of non-contiguous free page frames mapped into contiguous linear address, so they are not continuous but logically contiguous, this is an effective solution outside physically fragmentation issues.

The use of a well-known partner in linux algorithm to solve the external fragmentation problem. All the free page frame list are grouped into 11 blocks, each comprising a size of each list 1,2,4,8,16,32,64,128,256,512,1024 contiguous page frames, for the maximum request corresponding page frame 1024 size of the continuously 4MB RAM (4KB page size), each block address of the first physical page frame is an integer multiple of the block size, e.g., block size of the page frame 16, which is the start address a multiple of 2 * 16 ^ 12.
We use an example to illustrate the principle of partnership algorithm, assuming now request block a 256-page box (1MB), the algorithm steps are as follows:
• check list 256 page frame whether or not there is a free fast, if not, Find the next larger block, if any, to satisfy the request.
• check the list page frames 512 whether there is a free block, if any, to the page frame 512 is divided into two free blocks, used to satisfy the request first, second link 256 to page frames list. If no free block, continue to look for the next larger block. Figure:

The reverse process of the above process, the process is the release of the page frame block, the algorithm is the origin of the name, the size of the kernel trying to put a pair of free partner of the block B merged into a single block 2B, the two blocks meet the following criteria referred to as a partner:
• two blocks have the same size http://www.daiqiyang.com
• their physical addresses are consecutive
physical block address of the first page of the first block is 2 B 2 ^ 12 is
the recursive algorithm if it successfully merged B, we will try to merge 2B, in an attempt to form a larger block again.

Segmentation and paging is the difference between:

1, the paging mechanism uses a fixed size memory block, and a segment management using variable size blocks of memory management.

2, the use of fixed-size paging block is more suitable physical memory management, segmentation mechanism using a variable block size is more suitable for complex systems logically partitioned.

3, the segment table stored in linear address space, and page tables are stored in the physical address space.

4, collated address space is one-dimensional, i.e. a single linear space, the programmer need only use a character memory (linear address hexadecimal), indicates to an address.

Segmented address space is two-dimensional operation, when the programmer identifies an address, requiring both given section name (such as section data, and stack segments of code, etc.), and the need to give the segment address.

Guess you like

Origin blog.51cto.com/6624835/2462933
Recommended