[Operating System] 3.2 Memory Management (The operating system is responsible for the allocation and recovery of memory space ②)

Basic segmented storage management

1. What is segmentation (analog paging)

  • The address space of the process : according to the programOwn logicrelationshipDivided into several segments, Each section has a section name (in low-level languages, programmers use section names to program),Addressing from the beginning of each paragraph
  • Memory allocation rules : allocate in units of segments, ①Each segmentOccupies continuous space in the memory, but ② Between segmentsCan be non-adjacent
  • Since it is divided into logical function modules, user programming is more convenient, and the programMore readable
  • The compiler will convert the segment name into a segment number, and the CPU is segmented according to the segment number area
    Insert picture description here
  • The logical address structure of the segmentation system:
    1. The number of digits in the segment number determines how many segments each process can be divided into at most
    2. The number of address bits in the segment determines the maximum address length of each segment
      Insert picture description here

2. What is a segment table (analog page table)

  • The program is divided into multiple segments, and each segment is discretely loaded into the memory. In order to ensure the normal operation of the program, the storage location of each logical segment must be found in the physical memory. For this reason, a mapping table needs to be established for each process. Referred to as "segment table"
    Insert picture description here
  1. ⭐Each segment corresponds to a segment table entry, which records the starting position of the segment in memory (also known as the "base address") and the length of the segment
  2. determineThe length of the segment table entry
    1. The length of each segment table entry is the same. For example: a system is addressed by byte, using segmented storage management, and the logical address structure is (segment number 16 bits, segment address 16 bits), therefore, ① 16 bits can represent the maximum segment length (from the maximum segment Offset determined). ② Physical address memory size is 4GB (32 bits can be used to represent the entire physical memory address space)
    2. Therefore, each segment table entry can occupy 16+32=48 bits, which is 6B
  3. Since the length of the segment table entries is the same, it canHide segment number, Does not occupy storage space: if the starting address of the segment table is M, then the storage address of the K number segment table entry is M + K * 6

3. How to implement address translation

  1. Obtain the segment number S and the address W in the segment according to the logical address
  2. Determine whether the segment number is out of range, if S≥M, an out-of-bounds interrupt will be generated, otherwise continue to execute (Note: the length of the segment table (number of segment table entries) is at least 1, and the segment number starts from 0)
  3. Query the segment table and find the corresponding segment table entry. The storage address of the segment table entry is F+S* The length of the segment table entry
  4. within the segment address to check whether more than a long paragraph C, if W≥C, cross-border interrupt is generated, otherwise continue (Difference from page storage: Because the length of each segment in segment storage is different, it needs to be checked)
  5. Calculate the physical address
  6. Access target memory unit
    Insert picture description here

4. Comparison of segmentation and paging management

  1. page
    1. The physical unit of information, the main purpose of paging is to achieve discrete allocation and improve memory utilization
    2. Paging is only required for system management, it is completely system behavior, and is invisible to users
    3. The page size is fixed and determined by the system
  2. segment
    1. It is the logical unit of information, and the main purpose of segmentation is to better meet the needs of users. A segment usually contains a group of information belonging to a logical module
    2. The segment is visible to the user, and the user needs to explicitly give the segment name when programming
    3. The length of the segment is not fixed, it depends on the program written by the user
  3. When the address space of the user process is pagedOne-dimensional Yes, the programmer only needs to give a mnemonic to represent an address
  4. When segmenting the address space of a user processTwo-dimensional Yes, when the programmer identifies an address, he must give both the name of the section and the address within the section

    ⭐Understand:
    1. In paging, the size of each page is the same . So if the page size isGive a logical address After 2049,Calculation number:2049/1k=2, then the logical address can be calculated to be on page 2,Calculate the offset within the page: 2049%1k=1, so the offset within the page is 1, which can be combined with the page number to get the corresponding physical address
    2. In the segmentation, each segment is divided according to the logical module, that is, each segment's The size can’t be the same , so justGive a logical addressand Cannot get the segment number and offset within the segment. Therefore, it can be understood that two logical addresses , the segment number and the offset within the segment , need to be provided .
    Insert picture description here

  5. Segmentation is easier to achieve information sharing and protection than paging
    1. Code that cannot be modified is called pure code or reentrant code (not a critical resource), and such code can be shared. Modifiable code cannot be shared (for example, there are many variables in a code segment, concurrent access by each process at the same time may cause data inconsistency)
    2. The green code segment in the figure below is just a simple output, you only need to make the segment table entries of each process point to the same segment to achieve sharing
    3. In paging, the same function may be placed on the same page as the code of other functions. If all the pages are shared, other codes that cannot be shared in the page will also become shared, which is not allowed (the page is not Divided by logical modules, only part of the page may be allowed to be shared, so it is difficult to realize sharing )
      Insert picture description here
  6. How many memory fetches does it take to access a logical address?
    1. Paging (single-level page table): the first memory access-query memoryPage table, The second memory access-accessTarget memory unit. A total of two memory fetches
    2. Segmentation: the first memory access-check the memorySegment table, The second memory access-accessTarget memory unit. A total of two memory fetches
    3. Can also be used in segmented systemsIntroduce fast-track mechanism, Put the recently visited segment table entries into the fast table, so that one less visit can be made and the address conversion speed can be accelerated

Paragraph management

1. The biggest advantages and disadvantages of paging and segment management

  1. Paging management:
    1. Advantages: High memory space utilization, no external fragmentation, only a small amount of page internal fragmentation
    2. Disadvantages: It is not convenient to realize information sharing and protection according to logic modules
  2. Segment management:
    1. Advantages: It is very convenient to realize the sharing and protection of information according to the logic module
    2. Disadvantage: If the segment length is too large, it will be inconvenient to allocate a large continuous space for it. In addition, segmented management will generate external fragments (external fragments generated in segmented management can also be solved with "compact" technology, but at a relatively large time cost)

2. Combination of segmentation + paging-segment page management

  • The process is divided into logical modules, and each segment is paged (for example, each page is 4KB), and the memory space is divided into memory blocks of the same size (page frame, page frame, physical block), and the process loads each page separately In each memory block
    Insert picture description here
  • The logical structure address of the segment system consists of the segment number and the intra-segment (in-segment offset) address. Such as:
    Insert picture description here
  • The logical address of the segment page system is composed of segment number, page number, and page address (offset in page). Such as:
    Insert picture description here
    1. The number of digits in the segment number determines how many segments each process can be divided into at most
    2. The number of page numbers determines the maximum number of pages in each segment
    3. The offset in the page determines the page size and the size of the memory block

    In the above example, the system is byte-addressed, and the
    segment number occupies 16 bits. Therefore, in this system, each process has at most 2 16 = 64 K segment
    page numbers occupying 4 bits. Therefore, in this system, each segment number occupies 4 bits. A segment has a maximum of 2 4 = 16 pages The
    offset within the page occupies 12 bits, so the size of each page (memory block) is 2 12 = 4096 = 4 KB

  • note:"Segmentation" is visible to users, The programmer needs to display the segment number and address within the segment when programming . and"Pagination" of each paragraph is invisible to the user. The system will automatically divide the page number and the offset within the page according to the address in the segment. Therefore, the address structure of segment page management is two-dimensional .

3. Segment table, page table

  • Each segment corresponds to a segment table entry, and each segment table entry consists ofSegment number, page table length, page table storage block number(Used to calculate the starting address of the page table) composition, each segment table entry has the same length, and the segment number is implicit
  • Each page corresponds to a page table entry, and each page table entry consists ofPage number, page storage block numbercomposition. The length of each page table entry is the same, and the page number is implicit

If you want to read segment 0

  1. In the first segment table found 0 segment page table stored in a number of
  2. Read out the page table in block 2
  3. In the page table found segment number 0 stored in the pages of the two block numbers k, n number of blocks
  4. So access these two memory blocks to read data
  5. to sum up:
    1. A process will only correspond to one segment table
    2. Each segment corresponds to a page table
    3. Therefore, a process will correspond to multiple page tables
      Insert picture description here

4. How to implement address conversion in segment page format

  • Address conversion process:
    1. Obtain the segment number, page number, and offset within the page according to the logical address
    2. Compare the segment number with the length of the segment table to determine whether the segment number is interrupted beyond the boundary , otherwise continue to execute
    3. Query the segment table and find the corresponding segment table entry. The storage address of the segment table entry is the start address of the segment table + the segment number * the length of the segment table
    4. ⭐Get the page table length corresponding to the segment number and the block number stored in the page table from the segment table entry , compare the page number with the page table length, and judgeWhether the page number is out of bounds, Otherwise continue to execute
    5. According to the page table to store the block number and page number, query the page table to find the corresponding page table entry
    6. Get the final physical address according to the memory block number and the offset in the page
    7. Access target memory unit Insert picture description here
  • Page management segment with two page tables as neededThree memory accesses
  • Can alsoIntroduce fast-track mechanism, Use segment number and page number as keywords to query quick table. If the fast table hits, you only need to access the memory once

Guess you like

Origin blog.csdn.net/Qmilumilu/article/details/113983946