Operating System Notes - Memory Management

Operating System Notes - Memory Management

3. Memory management

3.1 Basics of memory management

3.1.1 Overview of memory management

Memory is an important part of the computer system and a precious and scarce resource in the computer system.Storage management in the operating system refers to the management of memory (also called main memory. By default, the memory in this chapter refers to memory), which is one of the important functions of the operating system.

Memory management functions

  • Memory allocation and deallocation. The operating system completes the allocation and management of memory space, freeing programmers from the trouble of storage space allocation and improving programming efficiency. To do this, the system should remember the usage of memory space: implement memory allocation; reclaim memory space released by the system or user.
  • Address transformation. In a multiprogramming environment, the logical address in the program is usually inconsistent with the physical address in the memory, so the storage management must provide an address conversion function to convert the logical address into a physical address.
  • Expand memory. With the help of virtual storage technology or other automatic overlay technology, users are provided with an address space larger than the memory space, thereby achieving the purpose of logically expanding memory capacity.
  • Storage protection. It is ensured that each job entering the memory runs in its own storage space without interfering with each other. It is necessary to prevent one operation from damaging other operations due to errors, and to prevent it from damaging system programs. This protection is generally accomplished by a combination of hardware and software.

Compiling, linking and loading applications

The application process from the source file written by the user to the process executed in the memory is roughly divided into 3 stages: first,The source code is compiled into several object modules through the compiler (Compiler); secondly, the compiled object modules and required library functions are linked together through the linker (Linker) to form a complete load module. (LoadModule); Finally, load these load modules into memory through the loader (Loader) and execute them. Simply put, the process from source program to execution goes through three steps : compilation, linking, and loading .

Insert image description here

Insert image description here

There are three ways to link the program:

  • Static linking.before the program runs, first link each target module and required libraries into a complete executable program, and will not disassemble it later.
  • Dynamic linking on load. When a set of target modules obtained after compiling an application program are loaded into memory, a dynamic linking method of loading and linking is used.
  • Dynamic linking at runtime. untilWhile the program is runningModules are linked only when they are needed. This linking method will postpone the linking of some modules until execution. That is to say, during the execution process, when a called module is found to have not been loaded into the memory, the module will be found immediately and loaded into the memory. , and then link it to the caller module. Any target module that is not used during execution will not be transferred into memory and linked to the loading module. This not only shortens the loading process of the program, but also saves a large amount of memory space, making it easy to modify and update. , to facilitate sharing of target modules.

There are three ways to load the program:

  • Definitely loaded.Know at compile time the physical address in memory where the program will reside, the compiler generates object code containing physical addresses. This approach is not suitable for multiprogramming.

  • Relocatable mounts.According to the current situation of the memory, the load module is loaded into the appropriate location in the memory. The address transformation is usually completed once during loading and does not change thereafter., this method is also called static relocation. The implementation of static relocation is very simple. When the operating system allocates a continuous main memory area with a certain address as the starting address for the program, during relocation, the logical address of the instruction or operand in the program is added to the starting address. Got the physical address.

    Insert image description here

  • Run the load dynamically.Allows the program to move locations in memory while it is running. All addresses after the load module is loaded into the memory are relative addresses. During program execution, only when the corresponding instruction or data is accessed, the relative address of the program or data to be accessed is converted into a physical address.. Because the relocation process is completed step by step as instructions are executed during program execution, it is also called dynamic relocation.The implementation of dynamic relocation relies on the hardware address conversion mechanism. The simplest implementation method is to use a relocation register. When a job starts to be executed, the operating system is responsible for sending the starting address of the job in the main memory into the relocation register. Later, during the entire execution of the job, whenever the memory is accessed, the system will automatically transfer the starting address of the job to the relocation register. The contents of the location register are added to the logical address to obtain the physical address corresponding to the logical address.

    Insert image description here

The characteristic of static relocation is that it is easy to implement without adding a hardware address conversion mechanism. But it requires that a contiguous memory area be allocated for each program, if the space is not enough to fit the entire program, it cannot be allocated, and it cannot be moved during the execution of the program, and no more memory space can be applied for, making it difficult to share programs and data.

The characteristic of dynamic relocation is that the program can be allocated to discontinuous storage areas, part of its code can be loaded before the program is run, and then the memory can be dynamically allocated as needed during the running of the program., which facilitates the sharing of program segments and can provide users with an address space that is much larger than the storage space of main memory. Dynamic relocation requires additional hardware support, and the software algorithm for storage management is relatively complex.

In relocation, the conversion formula from logical address to physical address is :

Physical address = base address register content + logical address

Logical address and physical address

Logical Address refers to the offset address part generated by the program and related to the segment (related to the page because it is only visible to the user) .After the source code is compiled, the address used in the target program is the logical address, and the range of the logical address is the logical address space. When the compiler compiles the source code, it always starts addressing from unit 0. The addresses in the address space start relative to 0, so the logical address is also called a relative address.. Multiple processes running in the system may have the same logical address, but when these logical addresses are mapped to physical addresses, they become different locations.

Physical address (Physical Address) refers to the address signal for physical memory that appears on the CPU external address bus. It is the final result address after logical address transformation. The physical address space refers to the collection of physical address units in the memory. When a process needs to access access instructions or data during operation, it is obtained from the main memory based on the physical address. The physical address is completely transparent to ordinary users, and users only need to care about the logical address of the program.The conversion process from logical address to physical address is automatically completed by hardware. This conversion process is called address relocation.

memory protection

Memory protection is to prevent a job from intentionally or unintentionally damaging the operating system or other jobs.. Commonly used memory protection methods include the limit register method and the storage protection key method .

  • bounds register method

    There are two ways to implement memory protection using the bound register method : the upper bound register method, the lower bound register method, and the base address and limited length register method .

    • Upper and lower bound register methods. The upper and lower bound registers are used to store the end address and start address of the job respectively.During the running of the job, each address accessing memory is compared with the contents of these two registers. If it is out of range, a protective interrupt is generated.
    • Base address and limited-length register methods. The base address and limit registers are used to store the starting address of the job and the address space length of the job respectively. The base address register is also called the relocation register, and the limit register is also called the boundary address register.During the running of the job, the relative address of each accessed memory and the value in the relocation register are added to form the physical address of the job; the limit register is compared with the relative address. If the value of the limit register is exceeded, an error message is issued. Interrupt the signal out of bounds and stop the job.
  • Storage protection key method

    Assign a separate protection key to each storage block, which acts like a "lock". Different from partition storage blocks, a partition consists of several storage blocks, each storage block is the same size, and the size of a partition must be an integer multiple of the storage block. also,Each job entering the system is also assigned a protection key, which is equivalent to a "key"When the job is running, check whether the "key" and "lock" match. If they do not match, the system issues a protective interrupt signal and stops the job.

3.1.2 Coverage and switching technology

coverage technology

Overlay technology was mainly used in early operating systems, because the memory capacity in early single-user systems was generally small and the available storage space was limited. Some large jobs could not be loaded into the memory at once. This caused The contradiction between large jobs and small memory arose, and coverage technology was introduced for this purpose.

The so-called coverage technology is to divide a large program into a series of coverages, and each coverage is a relatively independent program unit. A group of overlays that are not required to be loaded into memory at the same time when the program is executed is called a coverage segment; this overlay segment is allocated to the same storage area, and this storage area is called an overlay area, which corresponds to the overlay segment one-to- one . Obviously, in order for a coverage area to be shared by each coverage in the corresponding coverage segment at different times, its size should be determined by the maximum coverage in the coverage segment.

Insert image description here

Modules A and B form coverage segment 1, and modules C, D, and E form coverage segment 2.In order to achieve true coverage, the corresponding coverage area should be the size of the largest coverage in each coverage segment. After using the overlay technology, only 80KB of memory is needed to run the user program.

switching technology

Swapping meansMove part (or all) of a program and data that are temporarily unused from memory to external memory to free up necessary memory space, or read the specified program or data from external memory to the corresponding memory, and transfer the control A memory expansion technology that transfers rights to it and allows it to run on the system. The intermediate scheduling among the three-level scheduling of the processor uses switching technology.

The characteristic of swap technology is that it breaks the limitation that once a program enters the main memory, it will run until the end, but the size of the running process is still limited by the actual main memory.

  • Swapping requires backup storage, usually using fast disks. It must be large enough and provide direct access to these memory images.
  • In order to use the CPU efficiently, the execution time of each process needs to be longer than the exchange time, and the factor that affects the exchange time is mainly the transfer time.
  • If you swap out a process, you must ensure that the process is completely idle.
  • Swap space is usually treated as an entire block of disk and is independent of the file system.
  • Swapping typically starts when there are many processes running and memory space is tight, and is paused when the load on the system decreases.
  • Ordinary exchange is not used much, but some variants of exchange technology still play a role in many systems (such as UNIX systems).

3.1.3 Continuous allocation management method

What is internal fragmentation vs. external fragmentation?

Depending on the circumstances in which fragments appear, fragments can be divided into internal fragments and external fragments.Internal fragmentation refers to the memory space that has been allocated to the job but cannot be utilized. External fragmentation refers to the storage blocks in the system that have not been allocated to the job but cannot be allocated to new processes that apply for memory space because the fragments are too small., as mentioned below, there is internal fragmentation in fixed partition allocation, while there is external fragmentation in dynamic partition allocation. A simple understanding is,If the memory area occupied by a job is not full, it is internal fragmentation. Between jobs, if there is a memory area that is not allocated to a job but cannot be allocated to any job, it is external fragmentation.

single contiguous allocation

Single continuous allocation is the simplest storage management method and can usually only be used in single-user, single-task operating systems.This storage management method divides the memory into two continuous storage areas. One storage area is fixedly allocated to the operating system and is usually placed in the low address part of the memory. The other storage area is used by user jobs. Typically, user jobs only occupy a portion of all allocated space, and the remaining portion is actually wasted.

Insert image description here

Single continuous allocation method adoptsstatic allocation, suitable for single-channel programs, and covering technology can be used.Once a job enters memory, it must wait until it ends before the memory can be released.. Therefore, this allocation method does not support the implementation of virtual memory and cannot realize multi-program sharing of main memory.

The advantage of a single continuous allocation method is that it is simple to manage, requires little software and hardware support, is easy for users to understand and use, and does not have the problem of interference from other users. Its disadvantage is that it can only be used in a single-user, single-task operating system, and only one job is loaded into the memory to run, resulting in low utilization of various resources. A single contiguous allocation creates internal fragmentation .

Fixed partition allocation

The fixed partition allocation (also called fixed partition storage management) method is the earliest storage management method that can run multi-programs.It divides the memory space into several fixed-size partitions, and a program can be loaded into each partition. The size of the partition can vary, but it must be determined in advance and cannot be changed at runtime. When there is a free partition, a job of appropriate size is selected from the backup queue to be loaded and run.. In fixed partition allocation, programs are usually loaded into memory using static relocation . In order to achieve fixed partition allocation, the system needs to establish a partition description table to record the partition numbers available for allocation, the size of the partition, the starting address and status of the partition, usually sorted in order of partition size.

Insert image description here

When a user program is to be loaded into the memory, the memory allocation program retrieves the partition description table, finds an unallocated partition that meets the requirements from the table and assigns it to the program, and then modifies the corresponding partition table entry in the partition description table. status; if a partition of sufficient size cannot be found, memory allocation for the program is refused. When the program is executed and no longer requires memory resources, the partition occupied by the program is released, and the management program only needs to set the status of the corresponding partition to unallocated. The size of the partitions can be equal or unequal.

  • Partitions are of equal size. The lack of flexibility results in a waste of memory space. When a program is too large, one partition is not enough to load the program, causing the program to fail to run.
  • The partitions are not equal in size.The memory area can be divided into multiple smaller partitions, an appropriate number of medium partitions and a small number of large partitions.. Appropriate partitions can be allocated based on the size of the program.

The advantage of fixed partition allocation is that it can be used for the simplest storage allocation in multi-programming systems. Its disadvantage is that it cannot share a main memory area with multiple processes, the utilization rate is low, and internal fragmentation will occur .

Dynamic partition allocation

Dynamic partition allocation, also known as variable partition allocation, is a partitioning method that dynamically divides memory.This allocation method does not divide the main memory into partitions in advance. Instead, when the job enters the main memory, the partition is dynamically established according to the size of the job, and the size of the partition just meets the needs of the job. Therefore, the size of the partitions in the system is variable, and the number of partitions is also variable.

  • Data structures in partition allocation

    In order to achieve dynamic partition allocation, corresponding data structures must also be set up in the system to record memory usage. Commonly used data structures are as follows.

    • Free partition table. Set up a free partition table to register free partitions in the system. Each free partition corresponds to a table entry. Each table entry includes the partition number, starting address, size and status.

      Insert image description here

    • Free partition chain. Use the link head pointer to link the free partitions in the memory to form a free partition chain. Figure 3-8 shows an example of a free partition chain,The implementation method is to use the first few bytes of each free partition to store control information, which stores the size of the free partition and the pointer to the next free partition.

      Insert image description here

  • Partition allocation algorithm

    In order to load a job into memory, a partition that meets the job requirements should be selected from the free partition table (or free partition chain) according to a certain allocation algorithm and allocated to the job.If the capacity of this free partition is larger than the space capacity requested by the job, then part of the partition will be allocated to the job, and the remaining part will still remain in the free partition table (or free partition chain). At the same time, the free partition table ( or free partition chain) to modify the relevant information

    • First adaptation algorithm. Use a linked list to form a queue in the order of increasing address of the free partitions . Every time you need to allocate memory for a process,Start searching from the head of the queue and follow the linked list until you find a large enough free partition., and then allocate a piece of memory space from the partition to the requester according to the job size, and the remaining free partitions still remain in the free partition table (or free partition chain). If there is no qualified partition from beginning to end, the allocation fails.
      • Advantages: Prioritize the use of free partitions in the low-address part of the memory, thus preserving the large free partitions in the high-address part without internal fragmentation.
      • Disadvantages: Since the low address part is continuously divided, there are many small free partitions (external fragments) under the low address end that are difficult to use, and each search starts from the low address part, which undoubtedly increases the time required to search for available free partitions. s expenses.
    • Adapt the algorithm next time. This algorithm is also called the iterative first adaptation algorithm.That is, on the basis of the first adaptation algorithm, the queue is changed into a circular queue (the free partitions are still arranged in the order of increasing addresses), and the free partitions are not found from the head of the queue every time, but from the last found free partition . Start looking for the next partition of
      • Advantages: This makes the distribution of free partitions more even and reduces the cost of finding free partitions.
      • Disadvantages: Resulting in lack of large free partitions.
    • Best fit algorithm. It is required that the free partitions be arranged in increasing order of capacity . Each time memory space is allocated for a job,Always assign to a job the smallest free partition that can meet the space requirements. This results in the smallest free partition of memory.
      • Advantages: This method always assigns the most appropriate partition to the job and preserves large partitions.
      • Disadvantages: It results in a lot of fragmented space that is difficult to use.
    • Worst fit algorithm. The free partitions are required to be arranged in descending order of capacity . Each time memory space is allocated for a job,Always allocate to job the largest memory space that meets the requirements
      • Advantages: This makes the remaining free partition after allocating jobs relatively large, enough to load other jobs.
      • Disadvantages: Since the largest free partition is always allocated first, when a large job arrives, its storage space request will not be satisfied.
  • Partition recycling

    When the job execution ends, the system should reclaim the used partitions. Based on the size and first address of the reclaimed partition, the system checks whether there are adjacent free partitions in the free partition table (or free partition chain). If there are adjacent free partitions, it merges them into a large free area and modifies the relevant partitions. status information. There are the following four situations in which a reclaimed partition is adjacent to an existing free partition:

    Insert image description here

    • The recycling area is adjacent to a free partition, as shown in Figure 3-9a.At this time, the recycling area and the upper adjacent partition F1 should be merged into a continuous free partition. The first address of the merged partition is the first address of the free partition F1, and its size is the sum of the two.
    • There is an idle partition adjacent to the recycling area, as shown in Figure 3-9b.At this time, the recycling area and the next partition F1 should be merged into a continuous free partition. The first address of the merged partition is the first address of the recycling area, and its size is the sum of the two.
    • The upper and lower parts of the recycling area are adjacent to a free partition, as shown in Figure 3-9c.At this time, the recycling area and F1 F2 should be merged into a continuous free partition. The first address of the merged partition is the first address of F1, and its size is the sum of the three, and F2 should be deleted from the free partition table (free partition)
    • The reclaim area is not adjacent to any free partition,At this time, create a separate table entry, fill in the partition size and address and other information and add it to the appropriate location of the free partition table (or free partition chain), as shown in Figure 3-9d.
  • Dynamic management of partition allocation

    In the partition storage management method, the job must be loaded into a continuous memory space.. If there are several small partitions in the system, the total capacity of which is larger than the job to be loaded, but because they are not contiguous, the job cannot be loaded into memory. For example, there are 4 empty partitions in the memory that are not adjacent. Their sizes are 20KB, 30KB, 15KB, and 25KB respectively, and the total size is 90KB. But if a 40KB job arrives, the job cannot be loaded into the memory because the capacity of all free partitions in the system is less than 40KB, so dynamic management of partition allocation is required.

    • Splicing technology. The so-called fragments (also called fractions) areRefers to the storage space in the memory that cannot be used. In the partition storage management mode, after the system has been running for a period of time, fragments in the memory will occupy a considerable amount of space.

      One way to solve the fragmentation problem is toMove all allocated partitions in the memory to one end of the main memory, so that multiple small free areas that were originally scattered are connected into a large free area. This method of splicing multiple scattered small partitions into one large partition through movement is called splicing or compaction, which can also be called compaction.

      In addition to the technical problem of how to perform splicing, the implementation of splicing technology also has the problem of splicing timing . There are two solutions to this problem.

      • The first option isSplice immediately when a partition is recycled, so that there is always only one continuous free area in the main memory. However, splicing is time-consuming, and too high a splicing frequency will increase system overhead.
      • The second option isSplice when a large enough free partition cannot be found and the total capacity can meet the job requirements. The frequency of splicing in this way is much lower than the original solution, but the management of free partitions is slightly more complicated.
    • Dynamic relocation partition allocation technology.The dynamic relocation partition allocation algorithm is basically the same as the dynamic partition allocation algorithm. The only difference between the two is that the splicing function is added to this allocation algorithm , usually when a large enough free partition cannot be found to meet the job requirements, and the system Splicing is performed when the total capacity of the medium and free partitions is greater than the job requirements.

  • Advantages and Disadvantages of Dynamic Partition Allocation

    • advantage
      • It realizes that multi-programs share the main memory (sharing means that multiple processes exist in different locations in the main memory at the same time).
      • The management solution is relatively simple and requires no further overhead.
      • The means to achieve storage protection are relatively simple.
    • shortcoming
      • The main memory is not fully utilized and there is external fragmentation.
      • It is impossible to achieve multi-process sharing of memory information (sharing means that multiple processes use the same main memory segment).
      • Main memory expansion cannot be achieved, and the process address space is limited by the actual storage space.

3.1.4 Discontinuous allocation management method

Non-contiguous allocation allows a program to be spread across non-contiguous memory partitions. In the continuous allocation management method, we found that even if the memory has more than 1GB of free space, if there is no continuous 1GB of space, a job that requires 1GB of space will still not be able to run: ButIf discontinuous allocation is used, the 1GB memory space required by the job can be dispersedly allocated in various areas of the memory. Of course, this also requires additional space to store the indexes of them (dispersed areas), making the storage density of the discontinuous allocation method low. in continuous storage

The non-continuous allocation management method is divided into paging storage management method and segmented storage management method according to whether the partition size is fixed. The paging storage management method is divided into basic paging according to whether all pages of the job need to be loaded into the memory before running the job. Storage management method and request paging storage management method

Basic paging storage management method

In partition storage management, jobs are required to be placed in a continuous storage area, which will cause fragmentation problems (external fragmentation). Although the fragmentation problem can be solved through splicing technology, the cost is higher. If you allow a job to be spread across many non-contiguous partitions, then splicing can be avoided, effectively solving the problem of external fragmentation. Based on this idea, paging storage management (or page storage management) technology was introduced.

  • Paging principle

    In paging storage management,The address space of a user's job is divided into several equal-sized areas called pages or pages.. Correspondingly, theThe storage space of main memory is also divided into areas equal to the page size, called blocks or physical blocks. When allocating storage space for a job, it is always allocated in blocks, and any page in the job can be placed in any block of main memory .

    A physical block in main memory equal to the size of a page may also be called a page frame.

    When the scheduled job is running,All its pages must be loaded into main memory at once;The job waits if there are not enough physical blocks in main memory. This type of storage management is called simple paging or pure paging.

    The size of the page is determined by the address structure of the machine. When determining the address structure, if you select a smaller page, you can make the fragments within the page smaller and reduce the total space of memory fragments, which is beneficial to improving memory utilization, but it will also cause each process to require more pages, resulting in If the page table is too long, it takes up more memory and reduces the efficiency of page swapping in and out. If the selected page is larger, although the length of the page table can be reduced and the efficiency of page swapping in and out can be improved, it will increase the fragmentation within the page.

    Therefore, the page size should be selected moderately (usually an integer power of 2) to facilitate address conversion, generally 512B~4KB.

    The logical address in the paging storage management system (see Figure 3-10) contains two parts: the former part is the page number P, and the latter part is the intra-page displacement W (also called the intra-page offset).

    Insert image description here

    In the above address structure, the address length composed of two parts is 32 bits. inBits 0~11 are the bit addresses within the page, that is, the size of each page is 2 12 B = 4KBBits 12~31 are page numbers, that is, the maximum number of pages a process can run is 2 20 =1M pages

    Assume that the logical address is A and the page size is L, then the page number P= (int)(A/L), and the page bit W=A%L. Among them, "(int)" is a forced type conversion to an integer type, "/" is a quotient operation, and "%" is a remainder operation.

  • page table

    In order to map the consecutive page numbers on the logical address to the physical memory into multiple discrete physical blocks, it is necessary to map each page to each physical block one-to-one. This mapping relationship is reflected in the page table.. Each page table entry in the page table consists of a page number and a block number. According to the page table entry, the block number of the physical block in the physical memory corresponding to each page number can be found. Page tables are usually stored in memory.

    Insert image description here

    Assume that a user program is divided into n pages in the basic paging storage management mode, as shown in Figure 3-11a, then the corresponding relationship between its page number and the block number in the memory is shown in the page table in Figure 3-11b. According to the corresponding relationship in the page table, each page of the user program is stored in the memory area in Figure 3-11c.

  • basic address translation mechanism

    Insert image description here

    The entire address conversion process is automatically completed by hardware.

    Page table register (PTR): used to store the starting address of the page table in memory and the length of the page table

    Assume that the page size is L, the page table length is M, the address is A, and the address is E. The steps to obtain the physical address E from the logical address A and access the memory are listed below.

    • Calculate the page number P=(int)(A/L); the bit in the page W=A%L.
    • Compare the page number P and the page table length M. If P>=M, an out-of-bounds interrupt is generated, otherwise go to 3) to continue execution.
    • The starting address F of the page table is added to the product of the page number P and the length of the page table entry, and the obtained address value is used to retrieve the number b stored in the memory unit from the memory. This b is the physical block number.
    • The product of the physical block number b and the physical block size and the intra-page displacement W synthesize the physical address E.
    • Use the obtained physical address E to access the memory.
  • Address translation mechanism with block table

    **If the page table is entirely placed in the main memory, then accessing the main memory at least twice is necessary to access one data or one instruction. in,The first time is to determine the physical address of the accessed data or instructions by accessing the page table, and the second time is to access the data or instructions based on the obtained physical address.**Obviously, this method is half as slow as executing instructions normally.

    In order to improve the speed of address translation, a cache memory with parallel search function (also called associative memory or fast table) can be added to the address translation mechanism, and some page table entries can be placed in this cache memory (TLB). It is generally implemented by semiconductor memory, and its working cycle is roughly the same as that of the CPU, but the cost is higher. In order to reduce costs,Usually, those page table entries currently accessed by the running job are stored in the fast table, and the rest of the page table is still stored in memory.

    Insert image description here

    • According to the logical address, the page number P and the page displacement W are obtained.
    • First compare the page number with all page numbers in the fast table. If there is a matching page number, the corresponding block number is directly read out and concatenated with the intra-page displacement to obtain the physical address; if there is no matching page number, access is still required. The page table in the memory takes out the physical block number from the page table, concatenates it with the intra-page displacement to obtain the physical address, and stores this page table entry in the fast table.
    • Use the obtained physical address to access memory.Since the fast table is a register and has limited storage space, it often cannot accommodate a few page table units. Therefore, it is not always possible to find the block number corresponding to the required page number in the fast table. Before each search of the page table, the fast table is searched first. If the required page number is found, the block number is read directly, and then the memory only needs to be accessed once; if the required page number is not found, then the block number can only be read from the page table. To find the block number, this requires two accesses to the memory, and the time required to access the fast table is increased compared with that without a fast table. Therefore, we should try our best to ensure that commonly used page numbers and corresponding block numbers are placed in the fast table, so as to truly reduce access time.
  • Two-level page table and multi-level page table

    • Calculation of page table size. In basic paging systems, the page table length M pages is determined by the number of digits in the page number. The size of the page table can be understood as the area of ​​a rectangle. The length of this rectangle is the page table length M, and the width is the size of each page table entry, that is, the number of digits in the block number.

    • Two-level page table. It can be seen from the calculation formula of page table size that the page table size is proportional to the page table length, and the page table length increases exponentially with the increase in the number of page numbers. Therefore, if the system's logical address has a large number of digits, the page table will be very large, and the entire page table needs to be continuously stored in the memory. This is a very difficult thing, so there is a two-level page table.

      Insert image description here

      A two-level page table system divides logical addresses into outer page numbers, outer page inner addresses, and inner page addresses .First use the outer page number P1 to search on the external page table. The found unit content is the first address of the secondary page table. The first address of the page table plus the inner page address P2 of the outer page is the address of the page table entry. Take out the inside The value of (that is, the physical block number). The physical address is obtained by combining the physical block number with the page address d.. Under the two-level page table mechanism, both the number of digits in the outer page number and the number of digits in the address in the outer page are much less than in the one-level page table mechanism. In this way, the length of the page table can be reduced a lot, and the corresponding page The size of the table is also greatly reduced.

    • multi-level page table

      For a 32-bit machine (the number of logical addresses is 32 bits), it is appropriate to use a two-level page table mechanism, but for a 64-bit system, the two-level page table mechanism will make the size of the page table unacceptable, so You can reduce the size of the page table by continuing to increase the number of page table levels, but this will greatly increase the number of page tables. The main disadvantage of multi-level page tables is that they require multiple accesses to the memory, and each address change is a waste of time.

  • Page sharing and protection

    In multiprogramming systems, data sharing is very important.In a paged storage management system, sharing is achieved by having pages in the shared user address space point to the same physical block.

    Sharing is more difficult in a paged storage management system than in a segmented system. Because in the paging storage management system, the division of the job's address space into pages is transparent to the user, and the job's address space is linearly continuous.When the system divides the job's address space into pages of the same size, the shared parts are not necessarily included in a complete page. In this way, data that should not be shared is also shared, which is not conducive to confidentiality.. In addition, during the process of dividing the address space of each job into pages, the starting address of the shared part may have different intra-page displacements in each page, which also makes sharing difficult.

    The paging storage management system can provide two protection methods for memory:

    • One is address out-of-bounds protection, which is accomplished by comparing the page table length in the address translation mechanism with the page number in the logical address to be accessed.
    • The other is to protect memory information through access control information in the page table. For example, set an access control field in the page table and define the field as read, write, execute and other permissions according to the page usage. When the address is converted, not only the block number corresponding to the page must be obtained from the corresponding entry in the page table, but also whether the operation is consistent with the operations allowed by the access control field. If not, the hardware will capture it and issue a protection Sexual disruption.
  • Advantages and Disadvantages of Basic Paged Storage Management

    • advantage
      • High memory utilization
      • Implemented discrete allocation
      • Facilitates storage access control
      • No external debris.
    • shortcoming
      • Requires hardware support (it is a fast table)
      • Memory access efficiency decreases
      • Difficulty sharing
      • There are internal fragments

Basic segmented storage management methods

Segmented storage management has the following advantages compared to paging storage management:

  1. Convenient programming: users divide their jobs into several segments according to logical relationships.Each segment is addressed starting from 0 and has its own name and length.
  2. For information sharing, a page is a physical unit for storing information and has no complete meaning; while a segment is a logical unit of information. Users can put part of the code and data that needs to be shared in the same segment to facilitate information sharing.
  3. Information protection: Since each segment contains relatively independent information, information protection can be done by segment protection. Information protection is much more convenient than paging.

  • Segmented storage principle

    In a segmented storage management system,The address space of a job is composed of several logical segments. Each segment is a relatively complete set of information in a logical sense. Each segment has its own name. Each segment is addressed starting from 0 and Use a contiguous address space. Therefore, the address space of the entire job is two-dimensional (the classification of segments is one dimension, and the displacement within the segment is another dimension). In segmented storage management, memory is allocated in units of segments, and each segment is allocated a continuous memory area, but the segments are not required to be continuous . The allocation and recycling of memory is similar to dynamic partition allocation.

    The logical address structure of the segmented storage management system consists of the segment number S and the internal bit W (also called the intra-segment offset).

    Insert image description here

    The segment number S is usually a consecutive positive integer starting from 0. When the number of bits occupied by the segment number and intra-segment displacement in the logical address structure is determined, the maximum number of segments allowed in a job address space and the maximum length of each segment are also determined.

    For example, in Figure 3-15, the segment number occupies 16 bits, and the intra-segment displacement occupies 16 bits. Therefore, an operation can have up to 2 16 = 65536 segments, and the large segment length is 64KB .

    The address space of the paging storage management system is one-dimensional, while the segmented storage management system is two-dimensional.

  • Segment table and address conversion process

    Similar to paging storage management, in order to realize the transformation from logical address to physical address, the system establishes a segment table for each process, in which each table entry describes the information of a segment. The table entry contains the segment number, segment length and the The memory starting address of the segment.

    Insert image description here

    After configuring the segment table, the executing process can find the memory area corresponding to each segment by searching the segment table to achieve mapping from logical segments to physical memory areas.

    Insert image description here

    The address translation process is automatically completed by hardware.

    Insert image description here

    Get the physical address E from the logical address A and then access the memory as follows:

    • Take the first few digits from the logical address A as the segment number S, and the last few digits as the intra-segment displacement W.
  • Compare the segment number S and the segment table length M. If S>=M, an out-of-bounds interrupt is generated, otherwise go to 3) to continue execution.

    • Take out the segment table starting address F and segment number S, add them together, and use the obtained address value to retrieve the number stored in the memory unit from the memory. The first few digits of the number taken out are the segment length C, and the last few digits are the starting address (base address) b of the segment. If the displacement within the segment is W>=C, an out-of-bounds interrupt is generated, otherwise go to 4) to continue execution.
    • The base address b of the segment and the intra-segment displacement W are added to obtain the physical address E.
    • The physical address E used to access memory.
  • Sharing and protecting segments

    In a segmented storage management system, segment sharing is achieved by making corresponding entries in the segment tables of multiple jobs point to the same physical copy of the shared segment.

    Similar to paging management,There are two main types of protection for segmentation management: cross-border protection and access control protection.

  • Advantages and Disadvantages of Basic Segmented Storage Management

    • advantage
      • Data structures that facilitate program modularization and processing of transformations
      • Facilitates dynamic linking and sharing
      • No internal debris.
    • shortcoming
      • Similar to paging, requires hardware support
      • In order to meet the dynamic growth of segments and reduce external fragmentation, splicing technology is used
      • The maximum size of a segment is limited by the available space in main memory, with external fragmentation.
  • The difference between segmentation and paging

    Paging storage management and segmented storage management have many similarities. For example, both use discrete allocation methods, and both implement address conversion through an address conversion mechanism. But there are also many conceptual differences between the two

    Insert image description here

Basic segment page storage management method

As can be seen from the above introduction, the paging system can effectively improve memory utilization and solve the fragmentation problem, while the segmentation system can reflect the logical structure of the program and facilitate segment sharing. If these two storage management methods are combined, a segmented page storage management method is formed.

In the segment-page storage management system, the address space of the job is first divided into several logical segments, each segment has its own segment number, and then each segment is divided into several fixed-sized pages. The management of main memory space is still the same as paging management, dividing it into several physical blocks with the same size as the page, and the allocation of main memory is based on physical blocks.

The logical address structure of the segment page storage management system includes three parts: segment number S, page number within the segment P and page displacement D

Insert image description here

In order to implement address conversion, segment tables and page tables need to be established simultaneously in the segment page storage management system. The system creates a segment table for each process, and each segment has a page table. The entry in the segment table should at least include the segment number, the starting address of the page table and the length of the page table, where the starting address of the page table indicates the starting position of the page table of the segment in the main memory; the entry in the page table should at least include Page number and block number. In addition, in order to facilitate address conversion, the system also needs to configure a segment table register to store the starting address and segment table length of the segment table.

Insert image description here

The steps for logical address A to obtain physical address E and then access memory :

  • The first few digits taken from the edit address are the segment number S, the middle digits are the page number P, and the last few digits are the page displacement W.
  • Compare the segment number S and the segment table length M. If S>M, an out-of-bounds interrupt is generated, otherwise go to 3) to continue execution.
  • Take out the starting address F of the segment table and add it to the segment number S, and use the obtained address value to store the number in the memory. The first few digits of the number taken out are the page table length C, and the last few digits are the page table starting address d. If the page number P>C, an out-of-bounds interrupt is generated, otherwise go to 4) to continue execution.
  • The page table starting address d is added to the product of the page number P and the page table entry length to obtain the physical address of the page table entry in the memory. The value stored at this address is found to be the physical block number b.
  • Use the physical block number b and the page bit W to form the physical address E
  • Use the obtained physical address E to access the memory.

It can be seen from the above steps that the address transformation of the segmented page storage management system requires three accesses to the memory, so the cache register (fast table) can also be used to reduce the number of accesses to the memory.

Segment page style indeed combines the advantages of segment style and page style, and overcomes the external fragmentation problem of segment style, butThe internal fragmentation of the segment page type is not as small as that of the page type. Under the page storage management method, an average program has half a page fragmentation, while under the segment page storage management method, an average segment has half a page fragmentation, and a program often has many segments, so on average, the internal fragmentation of the segment page type is higher than that of the page type. need more

3.2 Virtual memory management

3.2.1 Basic concepts of virtual memory

Reasons for the introduction of virtual memory

Several storage management methods introduced earlier all analyze how to load multiple programs into memory in parallel. These methods all have the following two characteristics: one-time (the job can only be executed after all the jobs are loaded into the memory) and resident (the job resides in the memory until the end of the run). They are difficult to satisfy larger jobs or more jobs entering memory for execution.

During program execution, some codes are rarely used (such as error handling), and some programs require a long time for I/O processing, resulting in a lot of waste of memory space. Introduced for this purposeA storage management technology that allows jobs to be run even after they are partially loaded, that is, virtual memory management technology

locality principle

When most programs are executed, only a part of the program code is used in a short period of time. Correspondingly, the storage space accessed by the program is also limited to a certain area. This is the principle of locality of program execution.

  • Temporal locality. One execution and the next execution of an instruction, one access and the next access of a data are all concentrated in a short period of time.
  • spatial locality. The current instruction and several adjacent instructions, the currently accessed data and adjacent data are all concentrated in a smaller area.

The definition and characteristics of virtual memory

Based on the principle of locality, when the program is loaded, on the one hand, part of the program can be put into the memory, and the rest can be placed in external memory, and then the program can be started (partial loading).During program execution, when the accessed information is not in the memory, the operating system will transfer the required part into the memory (request transfer). On the other hand, the operating system replaces the temporarily unused content in the memory to the external memory, thereby freeing up space to store the information to be transferred into the memory (replacement function). From the effect point of view, the computer system seems to provide users with two memories with a storage capacity much larger than the actual memory. This memory system that logically expands the memory capacity is called virtual memory (referred to as virtual memory).

It is called virtual memory because this memory does not actually exist. The system only provides partial loading, request loading and replacement functions, giving the user the feeling that there is a memory that can meet the requirements of the job address space.

The meaning of virtual memory is to separate the address space where the program exists from the storage space during runtime. Programmers can write programs in the address space without considering the size of the actual memory.The capacity of virtual memory is determined by the computer's address structure and is not infinite.

Characteristics of virtual memory

  • discreteness. Programs are stored discretely in memory (discreteness is not a unique feature of virtual memory, basic paging and segmentation are also discrete).
  • multiple times. A job can be divided into multiple times and loaded into memory.
  • Commutability (exchangeability). Jobs can be swapped in and out while they are running.
  • virtuality. By logically expanding the memory capacity, the space available to users can be much larger than the actual memory capacity.

Implementing hardware and software support for virtual memory

  • There must be a considerable amount of external memory, enough to store programs for multiple users.
  • To have a certain amount of memory, the program running on the processor must have some information stored in the memory.
  • The interrupt mechanism interrupts program execution when the part accessed by the user is not in memory.
  • Address conversion mechanism to dynamically implement address conversion from virtual address to real address.
  • Related data structures, segment tables or page tables.

Commonly used virtual storage technologies include request paging storage management, request segment storage management and request segment paging storage management.

3.2.2 Request paging storage management method

Although the paging storage management method solves the problem of external fragmentation in memory, it requires that all pages of the job be transferred into the main memory at one time. When the available space in the main memory is insufficient or the jobs are too large, some jobs will be restricted from running in the main memory. To this end, the request paging (also called request paging) storage management method is introduced.First load part of the program into memory for execution, and then load it into memory when other parts are needed.. Obviously, this method is based on the principle of locality of the program.

Request paging principle

The request paging storage management method is exactly the same as paging storage management in terms of concepts such as paging of the job address space and partitioning of the storage space.It is a virtual storage system formed on the basis of the paging storage management system by adding request paging functions and page replacement functions.. In request paging storage management, before the job is run, as long as a part of the currently required pages are loaded into the main memory, the job can be started.During the running of the job, if the page to be accessed is not in the main memory, it can be brought in through the paging function. At the same time, the temporarily unused pages can be replaced into external memory through the replacement function to free up memory space.

Request paging = basic paging + request paging function + page replacement function

Page structure

The main data structure used in the request paging system is still the page table, whose basic function is to convert logical addresses in the program address space into physical addresses in the memory space.Since the request paging system only transfers part of the job into the memory and stores the other part on the disk, it is necessary to add several items to the page table for reference by the operating system when implementing the page loading and swapping functions.

Insert image description here

  • Page number and physical block number. These two fields have been defined in paging storage management and are necessary for address translation.
  • Status bit (presence bit). Used to determine whether the page is in main memory. Whenever a main memory access is performed, this bit is used to determine whether the page to be accessed is in the main memory. If not, a page fault interrupt is generated.
  • Access fields. Used to record the number of times a page has been accessed within a period of time, or how long it has not been accessed recently for reference by the replacement algorithm when choosing to swap out the page.
  • Modify the bit. Used to indicate whether the page has been modified after being loaded into memory. When the processor accesses a page in write mode, the system sets the modification bit of the page.Since the pages in the memory have copies on the external memory, if the page has not been modified, there is no need to write the page to the external memory when the page is replaced to reduce the number of disk writes; if the page is modified, it must be The page is rewritten to external storage
  • External storage address. Used to point out the storage address of the page on the external memory for use when loading the page.

Page fault interrupt and address translation

In the request paging storage management system, if the accessed page is in the memory, the address transformation process is the same as the paging storage management; if the accessed page is not in the memory, the page should be transferred into the memory first, and then the address transformation process is the same as that of the basic paging storage. Address translation is managed the same way.

Insert image description here

If the system finds that the page to be accessed is not in the memory, it generates a page fault interrupt signal. At this time, the user program is interrupted and control is transferred to the operating system's page fault interrupt handler. The page fault interrupt handler loads the page into memory based on its location in external memory.During the paging process, if there is free space in the memory, the page fault interrupt handler only needs to load the missing page into any free storage block, and then modify the corresponding entry in the page table (such as filling in the physical block number , modify the status bit, set the access field and modify the initial value of the bit, etc.); if there is no free space in the memory, some pages in the memory must be eliminated first. If the eliminated pages have been modified, they must be written Back to external storage

Page fault interrupt is a relatively special interrupt. It is significantly different from general interrupts , mainly in the following aspects:

  • Generating and handling page fault interrupts during instruction execution. Usually, the CPU checks whether an interrupt request has arrived after the instruction is executed, and if so, responds. The page fault interrupt is generated and processed when it is found that the instruction and data to be accessed are not in the memory during the execution of an instruction.
  • One instruction can generate multiple page fault interrupts. For example, if an instruction has two operands, and each operand is not in the memory, at least two page fault interrupts will be generated when the instruction is executed.

Advantages and Disadvantages of Request Pagination Management

  • advantage
    • Programs can be stored discretely, reducing the number of fragments.
    • Provides virtual memory, improves main memory utilization, facilitates the running of multi-programs, and is convenient for users.
  • shortcoming
    • Must have hardware support.
    • In some cases, the system may experience jitter.
    • There is still some unused space on the last page of the program.

3.2.3 Page replacement algorithm

The page replacement algorithm (also known as the page elimination algorithm) is an algorithm used to select pages to swap out.. In the request page storage management method, because not all pages are in the memory when a process is running, a page fault interrupt will occur. If there is no free physical block in the memory at this time, a page in the memory needs to be replaced. Specifically, Which page to replace is determined by the page replacement algorithm. It can be seen that,The quality of the page replacement algorithm directly affects the efficiency of the system.

  • The unit of page replacement is the page rather than the entire process. The unit of exchange is the entire process.
  • When a page fault occurs, the system may not necessarily execute the page replacement algorithm.. Because the occurrence of a page fault interrupt only means that the page that needs to be executed is not in the memory. If there are free blocks in the memory space, you only need to use the page fault interrupt handler to transfer the required pages from the external memory into the memory. No page replacement is required. algorithm; the page replacement algorithm is only needed when there are no free blocks in memory. Therefore, a page fault interrupt does not necessarily lead to the execution of the page replacement algorithm.

optimal replacement algorithm

In the case of predicting the page number reference string of a process,Each time, pages that are no longer used or will be used at the latest will be eliminated. This algorithm is the best replacement algorithm.

Obviously, the best replacement algorithm is optimal and has the lowest page fault rate. However, since in actual operations it is often impossible to know in advance the information of all pages that will be referenced in the future, soThe optimal replacement algorithm cannot be implemented and can only be used as a standard to measure the pros and cons of other replacement algorithms.

Insert image description here

Page order: 3,4,2,6,4,3,7,4,3,6,3,4,8,4,6

first in first out algorithm

The FIFO algorithm is the simplest page replacement algorithm.The page that enters the memory first is always eliminated every time, that is, the page that has resided in the memory for the longest time is eliminated.

This algorithm is simple to implement and can be implemented using a queue data structure. The pages are arranged into a queue in order, and the pointer is set to point to the page entered first. Every time a page needs to be eliminated, the page pointed to by the pointer can be eliminated. .

However, the FIFO algorithm may produceBelady exception (the number of page faults increases with the allocated physical block number),This isSince the FIFO algorithm ignores a phenomenon, that is, the earliest loaded pages are often the most frequently used pages. Therefore, the FIFO algorithm is inconsistent with the actual running rules of the process. The pages selected for elimination may be pages frequently used by the program, and the actual effect is not good.

Insert image description here

Least recently used algorithm

Select pages that have not been used for the longest time recently and eliminate them. The idea is to use previous page citations to predict future page citations., that is, assuming that a page has just been accessed, the page will be accessed again soon. Therefore the optimal replacement algorithm is "backward looking", andThe least recently used rule is "Look ahead"

This algorithm can be implemented using register banks and stacks, and has better performance.Among the commonly used page replacement algorithms, the LRU algorithm is closest to the optimal replacement algorithm.

Insert image description here

clock replacement algorithm

The clock replacement algorithm is also called the Not Recently Used (NRU) algorithm and is a hybrid between LRU and FIFO. As an approximation algorithm for LRU,The CLOCK algorithm sets an access bit for each page to identify whether the page has been accessed recently.. CLOCK maintains a circular linked list of all pages in memory.When the program needs to access a page that exists in the linked list, the access bit of the page is set to 1.;otherwise,If the page that the program wants to access is not in the linked list, then a page in the memory needs to be eliminated, so a pointer will sequentially traverse the circular linked list starting from the next position of the last eliminated page. When the page pointed to by the pointer When the access bit is 1, the access bit is cleared, and the pointer moves downward. When the access bit of the page pointed to by the pointer is 0, the page is eliminated. If it is not found after traversing the linked list, it can be The eliminated pages will continue to be traversed.

Insert image description here

Improved clock algorithm

There is also an improved CLOCK algorithm,It considers the issue of whether the page has been modified after being loaded into the memory, and adds a modification bit. Unmodified pages are eliminated first among processes whose access bits are both 0, because unmodified pages can be eliminated directly, while modified pages need to be written back to external memory.. Compared with the simple CLOCK algorithm, this algorithm reduces the number of disk I/Os but increases the number of scans.

After the modified CLOCK algorithm adds the modification bit, the status of each page has the following four situations.

  • It has not been accessed recently and has not been modified (access bit=0, modification bit=0).
  • Recently accessed and not modified (access bit = 1, modification bit = 0).
  • It has not been accessed recently, but has been modified (accessed bit=0, modified bit=1).
  • It has been recently accessed and modified (access bit = 1, modification bit = 1).

The algorithm steps of the improved clock algorithm are as follows:

  • Starting from the current position of the pointer, scan the circular linked list. During this scanning process, the access bits and modification bits are not modified. The first page encountered (access bit = 0, modification bit = 0) is selected for replacement.
  • If step 1) is not found, rescan and look for pages with (access bit = 0, modification bit = 1) for replacement. During this scanning process, each non-replacement page will have its access position 0.
  • If step 2) is still not found, return to the starting position. At this time, the access bits of all pages are 0. Repeat steps 1) and 2), and the replacement page will be found.

Other page replacement algorithms

  • Least Frequently Used Permutation (LFU) Algorithm

    Select the page with the least visits to the current time for elimination. This algorithm requiresSet an access counter for each page. Whenever the page is accessed, the access counter of the page is incremented by 1. When a page fault occurs, the page with the smallest count value is eliminated and all counters are cleared.

  • Page buffering algorithm

    The PBA algorithm is a development of the FIFO algorithm. By establishing a buffer for replacement pages, it retrieves the pages that have just been replaced, thereby reducing system I/O consumption.. The PBA algorithm uses the FIFO algorithm to select the page to be replaced. The selected page is not swapped out immediately but put into one of the two linked lists. If the page has not been modified, it is placed at the end of the free page list, otherwise it is placed at the end of the modified page list. These free pages and modified pages remain in memory for some time. If these pages are accessed again, they only need to be removed from the corresponding linked list and returned to the process, thereby reducing one disk I/O. When a new physical page needs to be transferred, the new page is read into the first page of the free page linked list, and then removed from the linked list. When the number of modified pages reaches a certain number, they are written to the disk together and then placed in the free page list. This can greatly reduce the number of I/O operations.

3.2.4 Working set and page allocation strategy

working set theory

In order to solve the jitter phenomenon, the concept of working set is introduced.The working set is assumed based on the locality principle. If we can predict which pages the program will access within a certain period of time and transfer them into memory in advance, we will greatly reduce the page fault rate, thereby reducing replacement work and improving CPU utilization.

The working set is the collection of pages that have been accessed the last n times. The number n is called the working set window, which is the size of the working set.. Frequently used pages will be in the working set. If a page is no longer used, it will be discarded in the working set. When a process addresses a page that is not in the working set, a page fault interrupt is generated. When handling a page fault, the working set is updated and the page is read from disk when needed .

The principle of the working set model is to let the operating system monitor the working set of each process, mainly monitoring the size of each working set. If there are free physical blocks, you can transfer another process to the memory to increase the degree of multi-channel; if the total size of the working set increases by more than the total number of all available physical blocks, the operating system can select a process pair in the memory. Move to disk to reduce the number of processes in memory to prevent thrashing.

The correct selection of the working set window size, that is, the number of pages allocated to the process, will have an important impact on the effective utilization of memory and the improvement of system throughput.

  • On the one hand, if the window is selected to be very large, although the process will not be prone to page faults, the memory will not be fully utilized.
  • On the other hand, if the window is selected too small, the process will frequently generate page fault interrupts during operation, which will actually reduce the system throughput rate.

Page allocation strategy

In the request paging storage management system, two types ofPage allocation strategy, namely fixed allocation and variable allocation . in progresspage replacementWhen , two strategies can also be adopted, namely global replacement and local replacement . Combining them, there are three suitable strategies as follows (fixed allocation and global replacement is unreasonable, so this strategy does not exist).

  • fixed allocation local replacement

    Allocate a certain number of physical blocks to each process. This number is determined and will not change while the process is running.. In this way, processes will not compete for physical blocks, which will cause some processes to have frequent page faults due to too few physical blocks, and some processes to waste memory space because too many physical blocks are allocated.When using a fixed allocation local replacement strategy, an algorithm needs to be used to determine how many physical blocks each process will allocate. Commonly used algorithms include the average allocation algorithm, the proportional allocation algorithm, and the allocation algorithm that considers priority.

  • Mutable allocation of global permutations.The operating system maintains a free physical block queue. Every time a process has a page fault, it takes a free physical block queue and assigns it to it. If there are no free physical blocks in the system, the system may call out any process. one of the pages

  • Variable allocation of local substitutions.After allocating a certain amount of physical blocks to each process, every time a page fault occurs and there is no free physical block in the memory, the process is only allowed to swap out one of its own memory pages. However, when a process frequently encounters page faults, , the OS allocates additional physical blocks to it until the page fault rate is reduced to an appropriate level. When a process's page fault rate is particularly low, the number of physical blocks allocated to it is appropriately reduced . The variable allocation local replacement strategy can achieve higher memory space utilization while ensuring that each process has a lower page fault rate.

page loading strategy

  • Request paging strategy.A page is loaded into memory only when it is used, otherwise it is placed in external memory.. This paging method will frequently cause page faults when a process is first started, because there are no pages for the process in the memory at the beginning. This strategy is simple to implement, but it is prone to generate more page fault interrupts, takes a lot of time, and is prone to jitter.
  • Prepaging strategy. This strategy refers toLoad pages that are expected to be used in the near future into memory, even though they are not used yet. When the program starts, if the programmer can point out which pages should be loaded first and put them together, then they can be loaded into the memory at once through the pre-paging strategy, which can save a lot of time. This is a prediction based on the principle of locality, usually used for the first load of a program.

From where to load the page

Request paging systemThe external storage is divided into two parts: the file area used to store files and the swap area used to store swap pages.. Generally, since the swap area is allocated continuously and files are allocated discretely, the disk I/O speed of the swap area is higher than that of the file area. In this way, whenever a page fault request occurs, where should the system load the faulted page into memory.

  • The system has sufficient swap area space. At this time, all required pages can be transferred from the swap area to increase the paging speed. For this reason, before the process runs, the files related to the process should be copied from the file area to the swap area.
  • The system lacks sufficient swap area space. At this time, all files that will not be modified are directly transferred from the file area; when these pages are replaced, there is no need to swap them out because they have not been modified. When they are transferred in again in the future, they are still directly transferred from the file area. Tune in. But for those parts that may be modified, they should be transferred to the swap area when swapping them out, and then transferred back from the swap area when needed.

3.2.5 Jitter phenomenon and page fault rate

BeladyException

FIFO replacement algorithmThe page fault rate may increase as the number of allocated physical blocks increases. This strange phenomenon is the Belady anomaly.. For example, for the reference string 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5, 9 page fault interrupts occur when the number of physical blocks in the memory is 3, and the number of physical blocks is 4 Instead, 10 page fault interrupts will occur.

The reason for the Belady exception is that the replacement characteristics of the FIFO algorithm conflict with the dynamic characteristics of the process's access to memory, that is, the replaced pages are not accessed by the process .

Belady exceptions may occur in the FIFO algorithm, but Belady exceptions will never occur in the LRU algorithm and the optimal replacement algorithm . Belady exceptions are also unlikely to occur in the page replacement algorithm classified as a stack algorithm .

Jitter phenomenon

If the selected page replacement algorithm is inappropriate, this phenomenon may occur:The page that has just been eliminated will be accessed again soon after, and will be called out soon after it is called in. This repeats so that the system spends most of its time loading in and out of the page, and can hardly complete any effective work. This phenomenon is called jitter (or thrashing)

The cause of jitter isEach process in a demand-paging system is allocated only a portion of the total memory space it requires.

page fault rate

Assume that a job has n pages in total, and the system allocates m pages of space to the job (m<=n). If the job needs to access a page a total of A times during operation (that is, the length of the reference string is A), and the page to be accessed is not in the memory, and the number of times the required page needs to be transferred into the memory is F, then the page fault rate is defined as f= F /A, the hit rate is 1-f.

The page fault rate is an important indicator to measure the page replacement algorithm. Usually the page fault rate will be affected by factors such as the replacement algorithm, the number of allocated pages, and the page size. The page fault rate is very important for the request paging management system. If the page fault rate is too high, it will directly lead to an increase in the average time to read pages and significantly reduce the process execution speed. Therefore, how to reduce the page fault rate is a very important task.

3.2.6 Requesting the Staging Storage Management System

The request segmented storage management system, like the request paged storage management system, provides the user with a virtual memory that is much larger than the available space in main memory. Likewise, the actual size of virtual memory is determined by the computer's address structure. In the request segment storage management system, before the job is run, as long as the currently required segments are loaded into the main memory, the job can be started. During the running of the job, if the segment you want to access is not in the main memory, you can use the segment adjustment function to transfer it in. At the same time, you can also use the replacement function to replace the temporarily unused segments to external memory to free up memory space. . To do this, the segment table should be expanded.

Insert image description here

The three pieces of information, segment number, segment length and memory starting address, are necessary for address translation. The meanings of other fields are the same as requesting paging storage management.When the segment is in memory, the address translation process is the same as segmented storage management; when the segment is not in memory, the segment should be transferred into memory first, and then address translation is performed.

When the segment being accessed is not in main memory, a missing segment interrupt signal will be generated. When the operating system handles the interrupt, it searches the main memory to see if there is a partition large enough to store the segment. If there is no such partition, check the total capacity of free partitions to determine whether the partitions need to be spliced, or one or several segments must be loaded before loading the required segments.

Guess you like

Origin blog.csdn.net/pipihan21/article/details/129809005