Operating system learning notes-Swap and virtual storage
Both Swap and virtual storage are the solutions for insufficient memory and there are swap-in and Swap-out operations. The main difference is that swap is used to redeem the entire process when memory is insufficient, the switch-in and switch-out are in process units. virtual storage allows a process to be executed without having to all be in the memory. The switch-in and switch-out are in page units.
The content of this article is as follows:
Swap Technology
Virtual Storage Technology
Concept
Locality Principle
Basic Ideas
Advantages
Request webpage
General process of requesting a webpage
Page table
Page disconnection
Page Replacement
Frame allocation
Jitter
Memory ing File Memory-Mapped-Files
Comparison: Copy-On-Write during Write
1. Swap Technology
The process must be executed in the memory (not all in the memory ). A process can be converted from memory to backup storage temporarily ). The backup area is usually a fast disk, which must be large enough to accommodate copies of all users' memory images without a file system.
The system maintains a ready queue containing processes that are ready for execution. The memory images of these processes may also be in the memory or in the backing store. When the cpu scheduler decides to execute the process, call dispatcher to check whether the next process in the queue is in the memory. If it is not in the memory and there is no idle memory area, dispatcher exchanges a process in the current memory, redeem the executed process. Then reload the register and transfer the control to the selected process.
Swap may be subject to some restrictions, and the context switching time is very high. If I/O is halfway through, you still need to wait for I/O.
Unix adopts an improved swapping policy. swap is usually disabled. swap is enabled when many processes are executed and the memory usage reaches a threshold (threshold. When the system load drops, swap suspends again.
2. Virtual Storage Technology
What is virtual storage?
Virtual Memory refers to a memory system that can run a job only by loading a part of the job into the memory. It has the Request Transfer and replacement functions and can logically expand the memory capacity. The logical capacity is determined by the sum of the memory capacity and the external memory capacity. Its running speed is close to the memory speed, while each cost is close to the external memory.
Local Principle (Principle of Locality)
Temporal locality
If a certain command of the program is executed at a certain time, the command may be executed again soon, such as a loop
If a data structure is accessed at a certain time point, the data structure may be accessed again soon. For example, a data structure can be accessed cyclically.
Spatial locality
Once a program accesses a storage unit in a certain location, a storage unit near it may also be accessed soon, such as an array
Basic Idea of virtual storage
1. based on the Locality Principle, before a job runs, it is not necessary to load all the memory. Instead, it only loads the page or segment that is currently needed into the memory to start running, and the rest are temporarily on the disk.
2. if the page or segment required by the program is no longer in memory and becomes a missing page segment, the program should use the requested page or request segment function provided by the operating system to transfer them to the memory, so that the process can continue to run.
3. if the memory is full at this time and a new page (segment) cannot be loaded, you must also use the page (segment) replacement function to temporarily remove pages (segments) that are not used in the memory) call it out to the disk to free up enough memory space, and then transfer the accessed (segment) to the memory so that the program continues to run;
Advantages of virtual storage
Programs can be larger than physical memory/logical address space can be larger than physical address space
Allow multiple processes to share the address space/easily implement file sharing and Memory sharing among multiple processes.
Provides an efficient process creation mechanism
For users, logical memory and physical memory are separated.
Request webpage
Generally, virtual storage includes the request page type, request segment type, and segment-page type virtual storage. The following describes the virtual storage technology using the request page type as an example.
Request webpageIs a page system that can be changed. During program execution, a page is loaded into the memory only when it is needed; an inaccessible page is not loaded into the memory. Support information must be provided in the page table.
The general process of requesting a webpage,
1. When a page is required, that is, when it is referenced, check whether the page is in memory.
If the reference is invalid from the page table, stop,
It is a valid reference but no longer in memory. You need to load the page into the memory;
2. When the access page is no longer in memory, page-faulttrap will be interrupted. The operating system needs to complete a series of work in case of page-missing interruptions;
3. When a page is loaded into the memory, there is no idle frame in the memory, and page replacement is required. There are some policies for page replacement and some algorithms.
4. Page Replacement is sometimes too frequent, which may cause system instability and jitter (thrashing, trembling, and trembling). The operating system has some processing methods for jitter.
Page table
Include frame number, valid bit, record address is valid, or add existing bit, record is in memory
Page disconnection
If there is a reference to a page, the first reference to the page will generate a trap for the operating system.
The operating system first compares the system page table with the process page table to determine whether a page reference is illegal or a legal page that has not been loaded into memory.
Find an idle Frame
Load the referenced page into the frame
Reset the page table
Restart the command to generate a page missing error.
There is a difference between page-missing interruptions and general interruptions. Page-missing interruptions occur during the execution of commands. When the commands or data to be accessed are no longer in memory, page-missing interruptions are generated and processed by the operating system; generally, an interruption is checked and processed after the execution cycle, that is, the interruption cycle.
During the execution of a command, page breaks may occur multiple times, and the maximum number of possible occurrences is related to the computer architecture.
For example, the IBM-370 may require 6 pages to process the ss move command; the command is 6 bytes and may span two pages, and the source to MOVE may span two pages, the destination to be moved may also span two pages. Such a MOVE command may cause 6 page-missing interruptions,
Page Replacement
When a page is requested, there is no idle frame in the memory. Select an unused frame from the memory and release it (write the content of this frame to the redemption space and modify the page table, to indicate that the frame is no longer in memory ).
The purpose of the page replacement algorithm is to ensure performance and minimize the number of page interruptions.
Below are some general ideas about the algorithms used for page replacement.
FIFO Page Replacement
When selecting a victim, the "first in first out" page is replaced first. You can use a queue to maintain pages in the memory to implement this algorithm.
Optimal Replacement Algorithm
The policy is to replace the pages that are used as far as possible in the future. This method can prove to be optimal, but is difficult to implement. It is impossible to predict which page is used as far as possible in the future, although difficult to implement, however, it can be used to compare the performance of other algorithms.
LRU Page Replacement, Least-recently-used. Assign a fixed number of frames to a process and replace the least commonly used page. The idea of this algorithm is also based on the local principle, "using the past, predicting the future is not far", so as to approach the optimal replacement algorithm. In fact, the current count implementation, stack implementation, and several LRU approximation algorithms, such as the additional bit, the second opportunity, and the second opportunity are enhanced. The specific descriptions of these algorithms are not given.
Frame allocation
One is the allocation during initialization,
Pure virtual storage, which is not allocated at the beginning and needs to be distributed at the time. Generally, considering performance factors, the minimum number of frames to be allocated to each process during initialization depends on the computer architecture. For example, IBM 370 may require six frames. There are several allocation algorithms for allocation: fixed allocation, including equal allocation and proportional allocation. equal allocation means the average distribution of each process, and proportional allocation is based on the proportion of the Process size, fixed allocation is also called priority allocation, which is based on the priority base as the proportion or the priority and process size as the proportion.
The other is the allocation during page replacement.
One way isGlobal allocationThat is to say, when the memory allocated to a process has no idle frames, you can release an unused frame inside the process and load the requested page, you can also release unused frames of other processes and load the requested pages;Local allocationIt can only be replaced within the process.
Jitter:
Jitter refers to the high-frequency page feed status. A process is busy switching processes from the memory, and the time spent on the page feed exceeds the execution time.
Cause: if a process does not have enough frames, the error rate will be high, resulting in:
Low CPU usage
The operating system considers it necessary to increase the degree of Multi-Program (to improve the CPU utility)
Another process is added to the memory.
Lead to lower CPU usage, such a vicious circle.
How can this problem be solved?
One isLimit the impact of JitterThe local replacement algorithm does not affect other processes during process jitter.
The other uses a local hypothesis-basedWorking Set Model. Defines the working set window as delta, and the working set is defined as a collection composed of the most recent Delta referenced pages. As a partial estimation of the program, the operating system records the working sets of each process, allocate enough memory for the process to accommodate its working set. If the sum of the working sets of all processes exceeds the number of available frames, the jitter condition is met, the operating system needs to select a process and suspend it to release the memory and allocate the released memory to other required processes. The released memory can be used to redeem the process as a swap.
Another is based onPage Error Rate. First, determine a tolerable page error rate. The operating system records the error rate of each process. If the error rate is higher than the tolerable value, the process obtains the memory frame. If the error rate is lower than the tolerable value, the process loses the physical frame.
Memory ing File Memory-Mapped-Files
This technology allows you to continue writing a disk like the read/write memory. By using the virtual storage technology, it maps disk blocks to pages in the memory, through virtual storage, convert file I/O to read/write memory instead of calling through the read and write systems. In addition, multiple processes are allowed to map the same file and pages in the memory are shared.
Specifically, when a file is started, a common page-based Read Request (resulting in page missing errors) is used. A file part of the page size can be read from the file system to a physical page, the subsequent file reads and writes are the same as normal memory reads and writes.
Comparison: Copy-On-Write during Write
The request page of the virtual storage technology allows rapid process creation, while the mechanism of virtual storage is bypassed during write replication, which also has better performance.
Copy at write timeWhen the fork system is called, the Parent and Child processes are allowed to have the same memory page. fork () does not copy and only creates a reference. If any process modifies a shared page, this page will only be copied.
Process 1 before modifying Page C
Process 1 after modifying Page C