There are two kinds of algorithms in memory management: one is the memory allocation algorithm, the other is the page permutation algorithm
Memory allocation algorithm: Refers to how the memory address is allocated from a contiguous logical address space to the process.
Common memory allocation algorithms and their advantages and disadvantages are as follows:
(1) First adaptation algorithm. When the algorithm is used for memory allocation, it is searched from the top of the idle partition chain until it finds an idle partition that meets its size requirements. Then, by the size of the job, a chunk of memory from the partition is allocated to the requestor, and the remaining free partitions remain in the idle partition chain.
The algorithm prefers to use an idle partition in the low-address part of memory, where the free partition at the high address portion is seldom exploited, preserving the large idle area of the high-address part. Obviously, the allocation of large memory space for later large jobs has created conditions. The downside is that the low-address section is constantly being divided, leaving many hard-to-use, small, idle areas, and each time the lookup starts from the low-address section, it will undoubtedly increase the cost of finding.
(2) Cyclic first adaptation algorithm. The algorithm is evolved from the first adaptation algorithm. When allocating memory space for a process, it is no longer a lookup from the top of the chain each time, but a lookup from the last found free partition until a free partition is found that satisfies the requirement, and a piece is drawn out to be distributed to the job. This algorithm can make the memory partition in the idle distribution more evenly, but will lack the large free partition.
(3) optimal adaptation algorithm. The algorithm always assigns both the required and the smallest free partition to the job.
To speed up lookups, the algorithm requires that all the idle areas be sorted by their size, forming a blank chain in ascending order. So every time we find the first to meet the requirements of the idle area, it must be optimal. In isolation, the algorithm seems to be optimal, but it does not necessarily. Since the remaining space must be minimal after each allocation, there will be a lot of hard-to-use small idle areas left in the memory. It also has to be reordered after each allocation, which brings a certain amount of overhead.
(4) worst-fit algorithm. In the worst-fit algorithm, the algorithm forms an idle area chain in descending order of size, which is allocated directly from the first free partition of the Idle zone chain (which does not satisfy the need). Obviously, if the first free partition is not satisfied, then there is no more free partition to meet the needs. This method of allocation does not seem reasonable at first, but it also has a strong visual appeal: After placing the program in the large idle area, the rest of the spare area is often very large, and a larger new program can be installed.
The worst-fit algorithm is exactly the same as the best-fit algorithm, and its queue pointer always points to the largest idle area, where the search is always started from the largest idle area when the allocation is made.
The algorithm overcomes the shortcomings of many small fragments left by the optimal adaptation algorithm, but the possibility of preserving large idle areas is reduced, and the recovery of idle areas is as complex as the optimal adaptive algorithm.
Page replacement algorithm: refers to the linear address into the physical address of the process of the algorithm, because the actual physical memory is limited, a process of all the logical pages of the page will not be mapped to the actual physical frame, but a certain number of physical frame, After that, a certain page replacement algorithm is used to dial the logical page into memory that needs to be paged into memory.
Evaluate the merits and demerits of an algorithm by running it on a particular storage access sequence (page orientation) and calculating the number of pages that are missing.
1 first in, first Out method (FIFO)
The simplest page substitution algorithm is the first in, first Out (FIFO) method. The essence of this algorithm is to always choose to stay in main memory for the longest (that is, the oldest) of a page permutation, that is, the first page to enter the RAM, first exit memory . The reason for this is that the first page to be paged into memory is more likely to be used than it was when it was just transferred into memory. Create a FIFO queue that hosts all the pages in memory. The displaced page is always on the queue header. When a page is put into memory, it is inserted at the end of the queue.
This algorithm is ideal only when the address space is accessed in a linear order, otherwise it is inefficient. Because those pages are often visited, often in main memory also stay the longest, the result they become "old" and have to be replaced.
Another drawback of FIFO is that it has an anomaly, that is, increasing the memory block, instead of increasing the fault rate of the pages. Of course, the page trend that causes this anomaly is actually very rare.
2 optimal permutation algorithm (OPT)
The optimal permutation (Optimal replacement) is an algorithm proposed in theory . The essence of this is: when a new page has to be placed in advance to replace an old page, the selected old page should be no longer used in the future, or be accessed in the furthest future. The page replacement algorithm is used to ensure that there is a minimum paging rate.
But the implementation of the optimal page replacement algorithm is difficult, because it requires people to know in advance the whole process of the page in the process of running the entire situation. However, the algorithm can be used to measure (for example, by simulating experimental analysis or theoretical analysis) the merits and demerits of other algorithms.
3 Longest unused algorithm (LRU)
The main difference between the FIFO algorithm and the OPT algorithm is that the FIFO algorithm uses the time of the page to enter memory as the replacement basis, and the OPT algorithm is based on the future use of the page time. If the recent past is an approximation of the near future, you can replace pages that have not been used for the longest time in the past. The essence of this is that when you need to replace a page,Choose a page that has been unused for the longest time in recent times。 This algorithm is calledthe longest unused algorithm(Least recently USED,LRU).
The LRU algorithm is related to the last time each page was used. When a page has to be replaced, the LRU algorithm chooses which pages have been unused for the longest time in the past.
The LRU algorithm is a frequently used page replacement algorithm and is considered quite good, but there are problems in how to implement it. The LRU algorithm requires real hardware support. The question is how to determine the order of the last use time, there are two possible ways to do this:
(1) counter. The simplest case is to have each page table item correspond to a usage time field and add a logical clock or counter to the CPU. Each storage access, the clock is added 1. Whenever a page is accessed, the contents of the clock register are copied to the usage time field of the corresponding page table entry. This way we can always keep the "time" that each page accesses last. When you displace a page, select the page with the lowest time value. To do this, not only to look up the page table, and when the page table changes (due to CPU scheduling) to maintain the time in the page table, but also to consider the problem of clock value overflow.
(2) stack. Keep the page number with a stack. Whenever a page is accessed, it is removed from the stack and placed on top of the stack. This way, the top of the stack always has the most used pages, and the bottom of the stack is the least used page. Because you want to remove an item from the middle of the stack, you should connect it with a bidirectional chain with a tail-pointing pointer. In the worst case, moving a page and placing it on top of the stack requires a change of 6 pointers. Each modification has a cost, but the need to replace which page can be directly obtained, not to find, because the tail pointer to the bottom of the stack, where there are displaced pages.
The implementation of the LRU algorithm requires a lot of hardware support, but also a certain amount of software overhead. So the actual implementation is a simple and effective LRU approximation algorithm.
An LRU approximation algorithm isalgorithms not recently used(not recently Used,nur). It adds a reference bit to each table item that stores the Chunked table, and the operating system periodically resets them to 0. When a page is accessed, the location is 1 by the hardware. After a while, check these bits to determine which pages were used and which pages have not been used since the last 0. You can eliminate the 0 page because it has not been visited in the most recent time.
4 Second Chance algorithm (SCR)
The basic idea of the second chance algorithm is the same as the FIFO, but it has been improved to avoid replacing the frequently used pages. When you select the Displacement page, check its access bits. If it is 0, the page will be eliminated; If the access bit is 1, give it a second chance and select the next FIFO page. When a page gets a second chance, its access bit is cleared to 0, and its arrival time is set to the current time. If the page was accessed during this period, access location 1. This gives the second chance the page will not be eliminated, until all other pages have been eliminated (or given a second chance). Therefore, if a page is used frequently, its access bit will always remain at 1, and it will never be eliminated.
The second chance algorithm can be considered as a ring queue. Use a pointer to indicate which page is to be eliminated below. When a storage block is needed, the pointer advances until it finds a page with a 0 access bit. As the pointer progresses, the access bit is cleared to 0. In the worst case, all access bits are 1, the pointer passes through the entire queue for a week, and each page gives a second chance. This will degenerate into a FIFO algorithm.
The page permutation algorithm also has many variants, such as considering whether the replaced page has been modified, the page that is selected by the FIFO algorithm is being used, and so on, all of which require hardware and software co-implementation.
Some algorithms in memory management