12.7-12.13 Study Notes

Source: Internet
Author: User

Three key features of virtual memory:

    • Consider main memory as a cache of address space stored on disk, protecting only active areas in main memory.
    • Provides a consistent address space for each process, simplifying memory management.
    • The address space of each process is protected from corruption by other processes.
9.1 Physical and general addressing
    • Physical addressing: main memory is organized into an array of units of m contiguous byte size, and the addressing method in turn is called physical addressing.

    • Virtual addressing: The CPU generates a virtual address (VA) to access the main memory, which is converted to the appropriate physical address before being transmitted to the memory. Address translation is done through the Memory management unit on the CPU chip.

9.2 Address Space

An address space is an ordered collection of non-negative integer addresses:

{0,1,2,......}
    • Linear address space: integers in the address space are contiguous.
    • Virtual address space: The virtual address generated by the CPU.
    • Physical address space: corresponds to M-bytes of physical memory in the system.
9.3 Virtual memory as a caching tool

The VM system splits the virtual memory into virtual pages, and the collection of virtual pages is divided into three disjoint subsets:

    • Unassigned: The system has not yet allocated a page, no data is stored, and no disk space is consumed.
    • Cached: Allocated pages in physical memory currently in slow existence
    • Not cached: The allocated pages in the physical memory are not being slowed.
Page table

Page Table: Maps a virtual page to a physical page, which is an array of page table entries.

PTE: Consists of a valid bit and an n-bit address field that indicates whether the virtual page is cached in DRAM.

Page hit

When the CPU reads a word, the address translation hardware locates the PTE with the virtual address as an index and reads it from the memory.

Page fault
    • The operating system provides a separate page table for each process, which is a separate virtual address space.
    • Shaking a virtual page can be mapped to the same shared physical page.
    • Memory mapping: A representation that maps a contiguous set of virtual pages to any location in any file.

DRA cache misses are called missing pages. When a hit occurs, the policy for the page is called on-demand page scheduling.

9.4 Virtual memory as a tool for memory management
    • The operating system provides a separate page table for each process, which is a separate virtual address space.
    • Shaking a virtual page can be mapped to the same shared physical page.
    • Memory mapping: A representation that maps a contiguous set of virtual pages to any location in any file.
9.5 Virtual memory as a tool for memory protection
    • SUP: Indicates whether the process must be running in kernel mode to access the page
    • READ: Reading permissions
    • Write: Writing Permissions
9.6 Address Translation

Address translation is a mapping between an element in the virtual address space (VAS) of an n element and the Physical address space (PAS) of an M element.

MAP:VAS→PAS∪空

MAP (A) =

    • A ': If the data at virtual address A is located at the physical address a of PAS '
    • Null: If the data at virtual address A is not in physical memory

A control register in the CPU the page Table base Register points to the current page table, and the virtual address of n bits contains two parts: a P-bit virtual page offset (VPO) and A (n-p) bit of virtual page number , The physical page number in the page table entry is concatenated with the VPO in the virtual address, and the corresponding physical address is obtained.

Use TLB to accelerate address translation

A small cache of PTEs is included in the MMU, called a translation fallback buffer (TLB), where each row holds a block of a single Pte.

    • The CPU generates a virtual address.
    • The MMU extracts the corresponding PTEs from the TLB.
    • The MMU translates this virtual address into a physical address and sends it to cache/main memory.
    • Cache/Main memory returns the requested data Word to the CPU.
Multi-level page table

A common way to compress page tables is to use a hierarchical page table.

9.8 Memory Mapping

Linux initializes the contents of this virtual memory area by associating a virtual memory area with an object on a disk, which becomes a memory map. There are two types of objects that can be mapped:

    • Ordinary files in the Unix file system. A zone can be mapped to a contiguous portion of a regular disk file.
    • anonymous files. Anonymous files are created by the kernel and contain all binary 0.

Once a virtual page is initialized, it is swapped for a dedicated swap file maintained by the kernel. Swap files are also called swap spaces, or swap areas.

Shared objects

An object is mapped to a region of the virtual storage, either as a shared object or as a private object.

A virtual memory area that is mapped to a shared object is called a shared area, similar to a virtual region.

Private objects are mapped to virtual memory using a technique called write-time copying, and the page table entries for the corresponding private areas are marked as read-only and the zone structure is marked as private write-time copies.

Execve function

The process of loading a a.out program into storage using the EXECVE function

Execve("a.out",NULL,NULL);

The following steps are described:

    • Deletes a user area that already exists.
    • Maps private areas.
    • Map shared areas.
    • Sets the program counter.

      User-level memory mapping using the map function

UNIX processes can use the MMAP function to create new virtual memory regions and map objects to these areas

void *mmap(void *start,size_t length,int prot,int flags,int fd,off_t offest); 若成功则为指向映射区域的指针,若出错则为MAP_FAILDE(-1)

The Munmap function deletes the area of the virtual storage.

int munmap(void *start,size_t length); 若成功则返回0,若失败则返回-1.
9.9 Dynamic memory allocation dynamic memory allocator

When additional virtual storage is required at run time, the virtual memory area of a process is maintained using the dynamic memory allocator. There are two styles of dispensers.

    • Display allocator: Requires an application to explicitly release any allocated blocks.

    • Implicit allocator: The allocator is required to detect when an allocated block is no longer in use by the program, releasing the Block. Also called a garbage collector.

malloc and FREE functions

The C standard library provides an explicit allocator called the malloc package that allocates blocks from the standard heap by invoking the malloc function.

void *malloc(size_t size);                若成功则为指针,若出错则为NULL。

Frees an allocated block by calling the free function.

void free(void *ptr);
The objectives and requirements of the dispenser

Constraints on the work of an explicit allocator:

    • Processes any request sequence.
    • Respond to requests immediately.
    • Use only the heap.
    • For aligning (alignment requirements).
    • The allocated block is not modified.

The goal of the ideal allocator:

    • Maximize throughput: Throughput is defined as the number of requests completed per unit of time.
    • Maximizing memory Utilization: The most useful criterion is peak utilization.
9.10 Garbage Collection

A garbage collector is a dynamic storage allocator. , the auto-release program is no longer required to be allocated fast (garbage).

Basic knowledge of garbage collector

The garbage collector treats the memory as a forward graph, and the node of the graph is assigned to a set of root nodes and a set of heap nodes. When there is a forward path from any root node to the p, it is said that node P is reachable.

Mark&sweep garbage collector

The Mark&sweep garbage collector consists of a tagging phase and a purge phase, which marks the root node for all the accessible and assigned successors, and the purge phase releases each unmarked allocated block.

Use the following functions in the description of Mark&sweep

    • PTR isptr (PTR p): if p points to a word in an allocated block, it returns a pointer B to the starting position of the block, otherwise null is returned.
    • int blockmarked (PTR b): Returns TRUE if Block B has already been marked.
    • int blockallocated (PTR b): Returns TRUE if Block B is assigned.
    • void Markblock (PTR b): Tag block B.
    • int length (PTR b): Returns the length in words of block B (excluding the head).
    • void Unmarkblock (PTR b): Changes the state of block B from marked to unmarked.
    • PTR nextblock (PTR b): Returns the successor of Block B in the heap.
Memory-related errors common in the 9.11 C language
    • Indirectly referencing bad pointers: there are large holes in the virtual address space of the process, no mapping to any meaningful data, and if you try to reference a pointer to these holes, the operating system terminates the program with a segment exception. The typical errors are:

      scanf ("%d", Val);

    • Read Uninitialized memory: Although the bass memory location is always initialized to 0 by the loader, it is not the case for heap storage.

    • Allow stack buffer overflow: If a program does not check the size of the input string. A buffer overflow error occurs when the program writes to the target buffer in the stack.

    • Assume that the pointer is the same size as the object that points to them.

    • Cause dislocation errors.

    • Refers to the pointer, not the object that he points to.

    • Misunderstanding pointer operations: the arithmetic operations that forget pointers are made in units of the size of the object they point to, and this size unit is not necessarily a byte.

    • References a variable that does not exist.

    • Reference the data in the free heap block.

    • Cause memory leaks: When you accidentally forget to release an allocated block and create garbage in the heap, it causes a memory leak.

12.7-12.13 Study Notes

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.