Operating System Concepts Learning note 16 memory Management (ii) segment page

Source: Internet
Author: User

Operating System Concepts Learning note 16 memory management (ii) paging (paging)

The paging (paging) memory management scheme allows the physical address space of a process to be non-contiguous. Paging avoids matching memory blocks of different sizes to swap space (the memory management scenario described earlier has this problem, and when the in-memory code and data needs to be swapped out, space must now be found on the backup storage, which is the problem.) The backup storage also has the memory-related fragmentation issues described earlier, but with slower access.

Traditionally, paging support has been handled by hardware. The most recent design is to implement paging by matching the hardware with the operating system.

Basic methods

The basic method of implementing paging is to design physical memory into blocks of fixed size, called frames, and logical memory into blocks of the same size, called pages. When the process needs to be executed, its pages are transferred from the backup storage into the available memory frames. Backup storage is also divided into fixed-size blocks, which are the same size as frames.

Each address generated by the CPU is divided into two parts: page number (p) and page offset (d). The page number as the index of the page table. The page table contains the base address of the physical memory where each page resides, and the combination of these base addresses and page offsets forms a physical address that can be sent to the physical unit.

The page size (as the frame size) is determined by the hardware. is usually a power of 2. Selecting a power of 2 for a page makes it easy to convert the logical address to page number and page offset. If the logical address space is 2^m and the page size is 2^n, the high m-n bits of the logical address represent the page number (the index of the page table), and the low n bits represent the page offset. The size of each page ranges from 512B to 16MB.

Set the page size to a, base address f according to page number p, page offset to D, then the physical addresses are f*a+d

Paging is a dynamic relocation. Each logical address has a paging hardware binding for a certain physical address. Paging is similar to using a set of base (relocation) address registers, each of which corresponds to this memory frame.

The use of paging technology does not create external fragmentation: each frame can be assigned to the process that needs it. However, pagination has internal fragmentation.

The entries for each page table are usually 4B, but this is mutable, and a 32-bit entry can point to any one of the 2^32 physical frames, and if the frame is 4KB, the system with 4B entries can access the 2^44b size.

When a system process needs to execute, it checks the size of the process (per page calculation). Each page of the process requires a frame. Therefore, if a process requires n pages, then there should be at least n frames in memory. If so, assign it to a new process. The first page of the process loads an allocated frame, and the frame number is placed in the page table of the process. The next page is assigned to another frame, and its frame number is also placed in the page table of the process.

An important feature of paging is the separation of memory and actual physical memory from the user's perspective. The user program handles the memory as an entire block, and it only includes this process. In fact, a user program is distributed in physical memory along with other programs.

The difference between the memory of the user perspective and the actual physical memory is coordinated by the address translation hardware. The logical address is converted to a physical address, which is not known to the user but is controlled by the operating system. Note that the user process is not able to access the memory that it occupies by definition. It cannot access memory outside of its page table, and the page table includes only those pages owned by the process.

Because the operating system manages physical memory, it must know the details of the allocation of physical memory: which frames are occupied, which frames are available, how many frames are in total, and so on. This information is usually saved in the frame table . In the frame table , each entry corresponds to a frame that indicates whether the frame is idle or occupied, and if so, which page of the process is occupied.

In addition, the operating system must be aware that the user process is executing within the user space and that all logical addresses must be mapped to physical addresses. If a user executes a system call (for example, I/O) and provides an address as a parameter, the address must be mapped to a physical address. The operating system maintains a copy of the page table for each process, just as it needs to maintain the contents of the instruction counters and registers. When the operating system must manually map a logical address to a physical address, this copy can be used to translate the logical address into a physical address. When a process can be allocated to the CPU, the CPU scheduler can define hardware page tables based on that copy. Therefore, paging increases the switching time.

Hardware support

Each operating system has its own method to save the page table. The vast majority of each process is assigned a page table. A pointer to a page table is stored in the process control block along with the values of other registers, such as instruction counters. When a scheduler needs to start a program, it must first mount the user register and define the correct hardware page table values based on the saved User page table.

There are many ways to implement the hardware of a page table. The simplest is to implement the page table as a set of dedicated registers (register) . These registers are constructed using high-speed logic circuitry to effectively convert the paging address. Because each access to memory passes through the paging table, efficiency is important. The instructions for the CPU to load or modify the page table register are privileged, so only the operating system can modify the memory map.

If the page table is small (for example, 256 entries), it is reasonable to use a register for the page table. However, the vast majority of contemporary computers allow page tables to be very large (such as 1 million entries). For these machines, it is not possible to implement a page table with a fast register, so you need to put the page table in memory and point the page Table base Register (page-table base REGISTER,PTBR) to the page table. Change the page table, only need to change this register to be able to, this also greatly reduced the switching time.

The problem with this approach is that it takes some time to access the user's memory location. If you want to access location I, you must first use the value in PTBR plus the offset of page number I to find the page table. This task requires memory access, based on the resulting frame number, plus the page offset, to get the actual physical address, followed by access to the desired location in memory. In this way, access to a byte requires two memory accesses (once for page table entries, one for bytes), so that memory accesses are halved and in most cases this delay is intolerable.

The standard solution to this problem is to use a small but dedicated fast hardware buffer called the conversion table buffer (translation look-aside buffer,tlb). The TLB is associated with fast memory. The TLB entry consists of two parts: a key (label) and a value. When associated memory is found based on a given value, it is compared to all keys at the same time. If an entry is found, then the corresponding range is obtained. This is faster to find, but the hardware is also expensive, and usually the number of entries in the TLB is not much, usually between 64~1024.

The TLB is used in conjunction with the page table as follows: The TLB includes only a small subset of the entries in the table. When the CPU generates a logical address, its page number is submitted to the TLB. If the page number is not in the TLB (called tlb invalidation ), then you need to access the page table. Add the page number and frame number to the TLB. If the entry in the TLB is full, the operating system chooses one to replace it. There are many substitution strategies, from least recently used replacements (LRU) to random substitutions. In addition, some TLB allows some entries to be pinned down. Usually the kernel code entries are fixed.

Some TLB also holds the address Space Identification code (Address-space identifier,asid) in each TLB entry. ASID can be used to uniquely identify a process and provide address space protection for the process. When the TLB tries to parse the virtual page number, it ensures that the asid of the current running process matches the ASID associated with the virtual page. If it does not match, it is invalidated as a TLB. In addition to providing address space protection, ASID allows the TLB to contain entries for more than one process at a time. If the TLB does not support standalone ASID, each time a page table is selected (for example, context switches), the TLB must be flushed (flushed) or deleted to ensure that the next process does not use the wrong address translation.

The percentage that the page number is found in the TLB is called the hit rate.

A 80% hit rate means 80% of the time you can find the page number you want in the TLB.

If the lookup TLB requires 20ns, access to memory requires 100ns, if you access the page number in the TLB, then memory-mapped access requires 120ns. If it cannot be found in the TLB (20ns), then you must first access the page table in memory to get the frame number (100ns), and then access the memory required bytes (100NS), which requires a total of 220ns. In order to obtain valid memory access time , each case must be weighted according to probabilities.

Valid memory Access time = 0.80 * + 0.2 * = (NS)

In this case, the memory access speed is now slow 40% (100ns~140ns)

If the hit rate is 98%, then

Valid memory Access time = 0.98 * + 0.02 * = 122 (NS)

Memory access time is only 22% slower due to increased hit ratio

Protection

In a paging environment, memory protection is achieved through the protection associated with each frame. Typically, these bits are saved in the page table.

You can use a bit to define whether a page is read-write or read-only. Each time the address reference is used to find the correct frame code through the page table, the protection bits can be checked for verification while the physical address is computed. Writing to read-only pages generates hardware traps (or memory protection conflicts) to the operating system.

This method can be easily extended to provide more granular protection, and hardware can be created to provide read-only, read-write, and protection-only execution. Alternatively, various combinations of these accesses can be achieved by providing separate protection bits for each access situation, and illegal access is captured by the operating system.

There is also a bit that is usually associated with each entry in the page table: valid-Invalid bit . Valid, which indicates that the related page is within the logical address space of the process, and therefore is a valid page, and is not valid, indicating that the related page is not within the logical address space of the process. Illegal addresses can be captured by using a valid-invalid bit . The operating system can allow or disallow access to a page by the bit.

Some systems provide hardware such as page table length registers (page-table length REGISTER,PTLR) to represent the size of the page table, which can be used to check each logical address to verify that it is within the valid range of the process, and if detection fails, Will be captured by the operating system.

Shared pages

One of the advantages of paging is that you can share common code. This consideration is particularly important for the time-sharing environment. Consider a system that supports 40 users, and each user executes a text editor. If the text editor includes 150kb of code and 50KB of data space. 8000KB is required to support these 40 users. If the code is reentrant (reentrant code, also known as pure Code), it can be shared. , you see the 3-page editor (50kb per page) shared between three processes, each with its own data page. In this way, only one copy of the editor can be saved in physical memory. Each user's page table maps to the same physical copy of the editor, and the data page maps to different frames. Therefore, in order to support 40 users, only one copy of the editor (150k) plus 40 user data space copy 50kb, the total demand space is 2150kb, not 8000kb, this is a significant savings.

Reentrant code is code that cannot be self-modified, and it never changes during execution. Two or more processes can execute the same code at the same time. Each process has its own copy of registers and data stores to control the data that the process executes. Data for two different processes will also be different.

Other commonly used programs can also be shared, such as compilers, Windows systems, runtime libraries, database systems, and so on.

The read-only feature of shared code cannot be guaranteed only by the correct code, but requires an operating system to enforce it.

Multiple process memory sharing for a system is similar to a task of multi-threaded address space sharing. Some operating systems implement shared memory by implementing shared pages.

In addition to allowing multiple processes to share the same physical pages, paging through the memory provides many other benefits.

Page Table Structure hierarchy

The vast majority of modern operating systems support large logical address spaces (2^32~2^64). In this way, the page table itself is very large.

Imagine a computer system with a 32-bit logical address space, if the system has a page size of 4KB (2^12b) Then a page table can contain 1 million entries (2^32/2^12) assuming that each entry has 4 B, then each process requires a 4MB physical address space to store the page table itself. Obviously, it is not possible for us to continuously allocate this page table in memory. A simple solution to this problem is to divide the page table into smaller parts. There are many ways to divide.

One approach is to use a two-level paging algorithm. is to re-page the page table. Still take the previous 32-bit system as an example, a logical address is divided into 20-bit page numbers and 12-bit page offset d. Because the page table is paged, the page number can be divided into 10-bit page numbers P1 and 10-bit page offset P2. Where P1 is used to access the index of the external page table, and P2 is the page offset of the external page table. This method is also known as the Forward Mapping page table (forward-mapped page tables)because the address translation is within the extroversion.

The VAX architecture also supports two-tier pagination variants. The logical address is divided into 4 zones. The first two bits of the logical address represent the appropriate partition, the middle 21 bits represent the page number in the zone, and the last 9 bits represent the offsets in the desired page. The operating system can use certain partitions only when the process requires it. And the VAX Architecture changes page tables for user processes to reduce the use of main memory.

For the 64-bit logical address space of the system hierarchy is not applicable, for 32-bit can also be used in three-tier paging scheme, or even four-tier paging scheme.

Hash page tables (Hashed page table)

A common way to handle more than 32-bit address space is to use a Hash page table (Hashed page tables)with a virtual page number as the hash value. Each entry in the hash page table includes a list of elements that texts hash the same location (to the processor collision). Each element has 3 fields:

    • (1) Virtual page number
    • (2) The frame number that is mapped
    • (3) A pointer to the next element in the list.

The algorithm works as follows: The virtual page number in the virtual address is converted to a hash table number, and the virtual page number is compared to the first field of each element in the list. If a match is made, the corresponding frame number (the second field) is used to form the physical address, and if it does not match, the next node in the list is compared to find a matching page number.

A variant of this approach is proposed, which is more suitable for 64-bit address space, the cluster page table (clustered page tables), similar to the hash page table, but each entry of this hash table includes not only one page of information, but also multiple pages. A page table entry can store mappings for multiple physical page frames. Clustered page tables are particularly useful for sparse address spaces where the address references in the sparse address space are not contiguous and are scattered throughout the address space.

Reverse page tables (inversed page table)

Typically, each process has a related page table. Each page used by the process has an entry in the page table (or each virtual address has an entry, whether or not it is valid). This kind of page is more natural to express, because the process is referencing the page by its virtual address. The operating system must convert this reference into a physical memory address. Because the page table is sorted by virtual address, the operating system can calculate the position of the corresponding entry in the page table and can use the value directly. One disadvantage of this approach is that each page table can have many items that may consume a lot of physical memory, but only to track how physical memory is used.

To solve this problem, introduce the Reverse page table (inversed page tables). The Reverse Page table has an entry for each true memory frame or page. Each entry contains the virtual address of the page saved in the true memory location and the information about the process that owns the page. Therefore, the entire system has only one table, and there is only one corresponding entry for each physical memory page.

Because the system has only one page table, and many address spaces map physical memory, an address space identifier is typically required in an entry for a Reverse page table to ensure that a logical page of a particular process can be mapped to the corresponding physical frame.

An identifier for the address space, Process-id. When a memory reference is required, the virtual address part consisting of Process-id and Page-number is sent to the memory subsystem to find the match by looking for the Reverse page table. If a match is found, such as entry I, then the physical address I+offset is generated. If there is no match, it is an attempt to access an illegal address.

While this scenario reduces the memory time required to store each page table, it increases the time it takes to find the page table when the page is referenced. (because the Reverse page table is sorted by physical address and looks for a virtual address, you may need to find the entire table to seek a match.) This lookup can take a lot of time)

Therefore, you can limit the lookup to one or a few page table entries by using the hash page table. However, each access to the hash page table adds a memory reference to the entire process, so a virtual address reference requires at least two memory reads: A Lookup Hash page table entry, and another Lookup page table. To improve performance, you can find the TLB first when you access the hash page table.

Systems with reverse page tables are having difficulty implementing shared memory. Shared memory is typically implemented as a multi-virtual address (in which each process shares memory) that is mapped to a physical address. This standard method cannot be used with reverse page tables because each physical page has only one virtual page entry, and a physical page cannot have two (or more) shared virtual addresses.

An easy way to solve this problem is to allow the page table to contain only one virtual address to the shared physical address mapping, which means that a reference to a virtual address that is not mapped will result in a page fault.

Segment (segmentation)

One unavoidable problem with paging memory management is the separation of memory and actual physical memory from the user's perspective.

Basic methods

Users are generally willing to consider memory as a collection of segments of different lengths that are not in a certain order. such as objects, arrays, stacks, variables, and so on.

Segmentation (segmentation) is a memory management approach that supports this user perspective. A logical address space consists of a set of segments. Each segment has a name and a length. The address specifies the segment name and the intra-paragraph offset. So the user specifies the address by two quantities: segment name and offset.

Note the comparison of this scheme with pagination. In pagination, the user specifies only one address, which is divided by hardware into page numbers and offsets.

For simplicity, the segment is numbered and is referenced by the segment number rather than the segment name. Therefore, the logical address consists of an ordered pair: (Segment-number, offset)

Typically, when you compile a user program, the compiler automatically constructs segments based on the input program.

A C compiler might create the following segment:

① Code

② Global variables,

③ Heap (memory is allocated from the heap)

④ the stack used by each thread

⑤ standard C library functions

Libraries that are linked at compile time may be assigned to different segments. When you load the program, all these segments are loaded and the segment numbers are assigned to them.

Hardware

Although the user can now refer to objects in the program through a two-dimensional address, the actual physical address memory is still a one-dimensional sequence of bytes. Therefore, you must define an implementation that maps a two-dimensional user-defined address to a one-dimensional physical address. This address is implemented through the section table (segment table). Each entry in the Cong has a segment base address and a segment boundary. The Subgrade address contains the beginning physical address of the segment in memory, and the segment bounds specify the length of the segment.

A logical address consists of two parts: the segment number S and the offset D within the segment. The segment number is used to index the segment table, and the offset d for the logical address is between the 0 and the segment bounds.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Operating System Concepts Learning note 16 memory Management (ii) section page

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.