Modern operating system notes-storage management 1

Source: Internet
Author: User

Reprint Please specify: theviper http://www.cnblogs.com/TheViper

A memory abstraction: address space

Exposing a physical address to a process can lead to several serious problems:

1. If the user program can address each byte of memory, they can easily destroy the operating system.

2. Using this model, it is difficult to run multiple programs at the same time (if only one CPU takes turns).

Address space

An address space is a set of addresses that a process can use to address memory. Each process has its own address space, and the address space is independent of the address space of other processes (except in some special cases where the process needs to share their address space).

Base Register and limit register

A simple solution is to use a simple dynamic redirect. This is a simple way to map the address space of each process to physical memory. The classic method used is to configure two special hardware registers per CPU, often called base register and limit register. When these two registers are used, the program is loaded into contiguous space in memory and no redirection is required during the load.

Specifically, when a program runs, the program's starting physical address is loaded into the base register, and the length of the program is loaded into the limit register.

The disadvantage of using this approach is that each access to memory requires addition and comparison operations. Comparisons can be done very quickly, but the addition is slow due to the problem of carry-over time, without the use of special circuits.

Switching technology

There are two common methods of handling memory overload, the simplest of which is the switching technique, which is to call a process to memory, make the process run for a period of time, and then save it back to disk. Idle processes are primarily stored on disk, so when they are not running, they do not consume memory (although some of their processes are periodically woken up to do the work and then go to sleep).

Swapping produces multiple idle areas in memory, and it is possible to synthesize a chunk of these small idle areas by moving all the processes down as far as possible. This technique is called memory crunch. This operation is usually not done because it consumes a lot of CPU time.

One issue to note is how much memory should be allocated to a process when it is created or swapped. If the process is created with a fixed size and no longer changes, it is on demand.

If the data segment of a process can grow, if the process is adjacent to an idle area, the idle area is assigned to the process. If another process is adjacent, either move the process that needs to grow to a sufficiently large area in memory or swap one or more processes to generate a large enough free area. If a process does not grow in memory and the swap area of the disk is full, the process is suspended until there is room to be free.

Free memory Management

There are two ways to keep track of memory usage: bitmaps, idle lists.

1. Storage Management Using bitmaps

Memory is divided into small to few words or as large as thousands of bytes of allocation unit. Each allocation unit corresponds to one in the bitmap, 0 for idle, and 1 for occupancy.

The main problem with this approach is that in deciding to call a process that takes up a K allocation unit into memory, the storage Manager must search for bitmaps, find the number of K consecutive 0 in the bitmap, and look for successive 0 strings of a specified length in the bitmaps as a time-consuming operation (because the string may span the boundaries of a word).

2. Using a linked list

Maintains a linked list that records allocated memory segments and free memory segments. Each node in the list contains the following fields. The indicator flag for the idle area or process, the start address, the length, and the pointer to the next node.

When storing processes and idle areas in a linked list in order of address, several algorithms can allocate memory for the process that was created (or the existing process that was swapped in from disk).

This assumes that the storage manager knows how much memory to allocate for the process. The simplest algorithm is the first adaptation method. The Storage Manager searches along the list of segments until it finds a free area that is large enough, unless the size of the scratch area is the same as the amount of space to allocate, otherwise the idle area is divided into two parts for the process to use and the other to form a new scratch area. This is a fast algorithm because it searches the list nodes as little as possible.

The next adaptation algorithm can be obtained with minimal modification of the first adaptation algorithm. It works the same way as the first adaptation algorithm, and the difference is that the location is recorded every time a suitable free area is found, so that the search will begin at the point where it was last searched the next time you look for a spare area.

Best Fit Method: Search the entire list (from start to finish) to find the smallest free area that can hold the process. The best fit method attempts to find the most realistic free area, rather than splitting up a large idle area that might be used later.

Because the entire list is scanned every time the best-fit algorithm is called, it is slower than the first-time adaptation algorithm. In addition, the optimal adaptation algorithm wastes more memory than the first-time adaptation algorithm because it produces a large number of useless idle areas.

To avoid this problem, consider the worst-fit algorithm, which always allocates the largest available idle area, making the new idle area larger so that it can continue to be used.

These four algorithms can be improved if you maintain separate linked lists for processes and idle areas. This allows you to focus on only the idle area and not the process.

However, this increases the complexity and slows down the memory release, because a reclaimed segment must be removed from the process list and inserted into the list of idle extents.

Virtual memory

Basic idea: Each program has its own address space, which is divided into blocks, each of which is called a page. Each page has a contiguous range of addresses. These pages are mapped to physical memory, but not all pages must be in memory to run the program. When a program references a portion of an address space in physical memory, the hardware immediately performs the necessary mapping, and the operating system is responsible for loading the missing part into physical memory when the program references a portion of the address space that is not in physical memory.

Page out

The virtual address space is divided into several units of the page by a fixed size.

Page table

As one of the simplest implementations of paging. The mapping of virtual address to physical address can be summed up as follows: Virtual address is divided into virtual page number (high part) and offset (low part) two parts. For example

For 16-bit addresses and 4KB page sizes, a high 4-bit can specify one of 16 virtual pages, while a low 12-bit determines the byte offset (0-4095) in the selected page. Of course, splitting a virtual address with a different number of bits is also possible.

The size of the page table entries for different computers may not be the same, but 32 bits is a common size. The most important domain is the page box number, after all, the purpose of page mapping is to find this value, followed by the "in/not" bit, this bit is 1 o'clock, indicates valid, can be used. is 0 o'clock, the virtual page that corresponds to the table item is not in memory now, and accessing the page causes a fault to be broken.

The protection bit indicates what type of access is allowed for a page, the simplest form of which is only one bit in the field, 0 for read/write, and 1 for read-only.

In order to record the usage of the page, the "modify" bit, "access" bit is introduced.

Modern operating system notes-storage management 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.