Information Security System Design Foundation 12th Week study Summary

Source: Internet
Author: User
Tags intel core i7

Nineth Chapter Virtual Memory

Virtual memory provides three important capabilities

    1. It sees main memory as a cache of address space stored on disk, storing only the active area in main memory and transmitting data back and forth between the disk and main memory as needed.
    2. It provides a consistent address space for each process, simplifying the management of the memory.
    3. It protects the address space of each process from being destroyed by other processes

Why to understand virtual storage

    1. Virtual memory is central to all aspects of a computer system. 2, virtual memory is powerful; 3, virtual memory is dangerous

9.1 Physical and virtual addressing

Virtual addressing: CPU generates virtual address to access main memory, virtual address is translated into physical address and sent to memory.

Early PCs use physical addressing. Modern processors use virtual addressing.

9.2 Address Space

An address space is an ordered collection of non-negative integer addresses. If the integer in the address space is contiguous, it becomes the linear address space.

Virtual address space: The size is described by the number of bits required to represent the maximum address.

9.3 Virtual memory as a caching tool

9.3 virtual memory as a tool for caching

Conceptually, the virtual memory is organized into an array of "n contiguous bytes of cells stored on disk". The contents of the disk are cached in memory, and the memory and disk are transmitted in block format. Because the technology of virtual storage is earlier than cache technology, this kind of block is called the page, which is now the argument. Such as:

Shows a small virtual storage of 8 virtual pages. The green 3 pages indicate that the memory has been cached (refer to the three pages in memory green) with an unassigned and three non-cached.

Because memory accesses 100,000 times times faster than disk, if memory is not hit, the cost will be very high (the program will run very slowly), and the cost of reading the first byte of one sector of the disk is 100,000 times times slower than reading the entire sector. These two conditions determine the size of a virtual storage page, preferably a sector or several sectors. Typically, 4KB-2MB.

Also, even if there is a very large page, the operating system in the replacement page operation is not handled properly, the speed will be very slow. Page fault is an exception, the name is called "Page fault", after an exception occurs, virtual memory load the disk into memory and replace the sacrificial pages. The operating system runs the instruction again and no more pages are missing.

When we and many people understand the concept of virtual memory, our first impression is: "It should be very inefficient, because if not hit, then the cost will be very high." "We are also concerned about the performance of the page scheduling to disrupt the program. In fact, virtual memory runs fairly well, thanks largely to program locality (locality).

Although the total number of different pages referenced by the program throughout the run may exceed the total size of the physical memory, the locality principle guarantees that at any point in time, the program will often work on a smaller set of active pages (active page), a set called the working set.

Tips Use the Getrusage function in a UNIX system to detect the number of missing pages .

9.4 virtual memory as a memory management tool

In fact, the operating system provides a separate page table for each process, and therefore a separate virtual address space. The presentation is: It feels like the program is completely using CPU and memory resources. For example, multiple virtual leaf colleagues map to the same shared physical page (the dynamic link library or the shared library is the principle).

A process-independent address space allows each process's memory image to use the same basic format, regardless of where the code and data actually reside in the physical memory. For example, every process text section on a Linux system always starts at the beginning of the virtual address 0x08048000. In summary, virtual storage simplifies linking, simplifies loading, simplifies sharing, and simplifies memory allocation.

9.5 virtual memory as a tool for memory protection

Modern operating systems strictly restrict the access of ordinary applications to their own read-only and process-aware data, as well as the kernel portion of the operating system. Therefore, the virtual memory can be set in the beginning of the page a few flags to indicate whether the process is a system process or user process. If a day instruction violates this condition, then the CPU triggers an exception. The Unix shell typically reports this exception as "segment Error segmentation fault"

9.6 Address translation, a little professional. Doesn't have much to do with the application level.

9.7 Case Study: Intel Core i7/linux Memory System

A pointer to a variable is printed on a 64-bit Mac computer, and the result pointer is 6 bytes . This is because the i7 CPU only supports 48-bit virtual addresses, and 52-bit physical address space. Although my Mac is i5, it is also estimated to be a 48-bit virtual address. Other content is address translation, fault pages.

Linux Virtual storage area: Linux organizes virtual storage into a collection of areas. An area is a continuous slice (chunk) of virtual memory that already exists (allocated)

9.8 Memory Mapping

There is a question: "Computer 4G of memory, loading a program is very slow, but look at the amount of memory remaining, and the remaining memory, should not be slow ah." If you upgrade to 8G of memory, the program loads faster. This is because the operating system has a thing called swap space, and the swap space may show the number of virtual memory pages. " Understand, if the memory is large, swap space is large, the program loads quickly.

Concept: It is important to understand the concept of a proprietary copy-on-write. To illustrate with a diagram:

The right section describes a write-time copy: Processes 1 and 2 share a set of pages in memory, but process 2 needs to write the last page. Then start a write-time copy and copy the last page to the memory elsewhere. (The In-memory page may not be contiguous.)

The fork function opens a section of virtual storage directly to a different copy of the main process, and then it points to the same physical memory as the main process. From the left section we can confirm that process 2 can be seen as creating from the fork function called from Process 1.

Execve how the application is loaded. The EXECVE function loads and runs a.out in the current process, effectively replacing the current program with the A.out program. The following steps are required when loading a.out:

1) Delete the existing user area.

2) Map private areas. Create a new regional structure for the text, data, BSS, and stack areas of the new program.

3) Map the shared area.

4) Set the program technology PC. The entry point to which to point the text area.

9.9 Dynamic memory allocation malloc

Dynamic allocation, my own understanding is the allocation of the area in the virtual memory, the area may already be in memory, or on disk.

Here's the real definition: The dynamic memory allocator, which maintains a process's virtual memory area as a "heap," for each process, the operating system kernel maintains a variable BRK (break) pointing to the top of the heap. There are two basic styles of dispensers.

1) Explicit allocators: For example, malloc and new operations for C and C + +. However, the programmer is required to receive free and delete processing.

2) Implicit allocator: Also known as the garbage collector, the process of automatically releasing unused allocated blocks is called garbage collection. For example, Java's garbage collection mechanism.

Requirements and targets for dynamic storage allocator

Processes any request sequence.

Respond to requests immediately. The allocator must respond to allocation requests immediately. Therefore, the allocator is not allowed to improve performance, from the new arrangement or buffering requests.

Just use the heap.

Alignment blocks, such as 8-byte alignment.

The allocated block is not modified. You cannot compress an allocated block.

Goal

1 Maximum throughput rate

2 Maximize the utilization of the memory. Naïve programmers often do not correctly assume that virtual memory is an unlimited resource, in fact, the total number of virtual memory allocated by all processes in a system is limited by the amount of swap space on disk.

Memory fragmentation (This is a very interesting topic). The internal and external fragments are introduced first.

External fragmentation is much more complex than internal fragment processing! The allocator also has other functions: merging the idle blocks is also very interesting (the original book uses 19 pages of volume to describe the principle and action of the dispenser), smart programmers use a number of techniques to achieve, refer to p568 page.

9.10 garbage Collection (recycling)

The garbage collector is a dynamic allocator that automatically frees the allocated blocks that are no longer needed by these programs. Garbage collection dates back to the the 1960s early-morning Lisp system developed at MIT. He is an important part of modern language systems such as Java, ML, Perl, and Mathematica.

The principle of garbage collection. The gray dots indicate that they are already rubbish and need to be cleaned up.

is a professional word is: is a map, the white block can be reached, the gray block indicates unreachable (from any root node unreachable).

9.11 Common memory-related errors in the program.

Indirectly referencing bad pointers

Read Uninitialized memory

Allow stack cache to overflow

Assume that the pointers and the objects they point to are the same size

Cause dislocation

Reference pointer, not the object it points to

Misunderstanding pointer arithmetic

Apply a non-existent variable

Apply data from a free block

Cause memory leaks

Summarize

Three key features of virtual memory:

First, it automatically caches the contents of the "Recently used" virtual address space on disk in memory.

Second, virtual storage simplifies storage management, simplifies linking and sharing

Third, virtual memory simplifies memory protection by adding a protection bit to each page table entry.

Information Security System Design Foundation 12th Week study Summary

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.