OS memory management/device management/link

memory management

Virtual Memory

Each program has its own address space, which is divided into blocks, each called a page. The pages are mapped to physical memory, but need not be mapped to contiguous physical memory, nor do all pages have to be in physical memory. When a program references a part of the address space that is not in physical memory, the hardware performs the necessary mapping, loads the missing part into physical memory and re-executes the failed instruction.

Pagination and Segmentation

1. Pagination

Most virtual memory systems use paging. The addresses generated by the program are called virtual addresses, and they constitute a virtual address space. For example, a computer can generate 16-bit addresses, and its virtual address space is 0~64K, but the computer has only 32KB of physical memory, so although 64KB programs can be written, they cannot be completely transferred into memory to run.


The virtual address space is divided into fixed-size pages, and the corresponding unit in physical memory is called a page frame. The size of the page and the page frame are usually the same, and they are mapped through the page table.

At the beginning of the program, only a part of the page is loaded into the page frame. When the program refers to a page that is not in the page frame, a page fault interrupt is generated, and the page is replaced. .

2. Segmentation


The above picture shows multiple tables created by a compiler during the compilation process. There are 4 tables that grow dynamically. If the one-dimensional address space of the paging system is used, the characteristics of dynamic growth will lead to coverage problems.


The practice of segmentation is to divide each table into segments, and a segment constitutes an independent address space. Each segment can be of different length and can grow dynamically.

Each segment needs to be divided by the programmer.

3. Paragraph

A segmented approach is used to allocate and manage virtual memory. The address space of the program is divided into basically independent segments according to logical units, and each segment has its own segment name, and then each segment is divided into several pages of fixed size.

Use paging methods to allocate and manage real storage. That is, the entire main memory is divided into storage blocks of the same size as the above-mentioned pages, which can be loaded into any page of the job.

The program calls in or out of memory by page, but it can be shared and protected by segment.

4. The difference between paging and segmentation

  • Transparency to the programmer: Paging is transparent, but segmentation requires the programmer to explicitly divide each segment.

  • Dimensions of address space: Paging is a one-dimensional address space, and segmentation is two-dimensional.

  • Whether the size can be changed: the size of the page is immutable, and the size of the segment can be changed dynamically.

  • Reason for appearance: Paging is mainly used to implement virtual memory to obtain a larger address space; segmentation is mainly to enable programs and data to be divided into logically independent address spaces and to facilitate sharing and protection.

Paging System Address Mapping

  • Memory Management Unit (MMU): Manages the translation between virtual address space and physical memory.
  • Page table: A mapping table of pages (virtual address space) and page frames (physical memory space). For example, in the following figure, the 0th entry of the page table is 010, which means that the 0th page is mapped to the 2nd page frame. The last bit of the page table entry is used to mark whether the page is in memory.

The page table in the figure below stores 16 pages, and these 16 pages need 4 bits for index positioning. So for a virtual address (0010 000000000100), the first 4 bits are used to store the page number, and the last 12 bits are used to store the offset in the page.

(0010 000000000100) According to the first 4 digits, the page number is 2, the content of the read entry is (110 1), the first 3 digits are the page frame number, and the last 1 digit indicates that the page is in memory. Finally, the physical memory address obtained by mapping is (110 000000000100).


page replacement algorithm

During the running of the program, if the page to be accessed is not in the memory, a page fault interrupt occurs and the page is loaded into the memory. At this point, if there is no free space in the memory, the system must transfer a page from the memory to the disk swap area to make room.

The main goal of the page replacement algorithm is to minimize the frequency of page replacement (or the lowest page fault rate).

1. Best

Optimal

The selected page to be swapped out will be no longer accessed for the longest time, and usually the lowest page fault rate can be guaranteed.

is a theoretical algorithm, as there is no way to know how long a page is no longer being accessed.

Example: A system allocates three physical blocks to a process and has the following sequence of page references:


When starting to run, first load the three pages 7, 0, 1 into memory. When the process wants to access page 2, a page fault interrupt occurs and page 7 is swapped out, because page 7 takes the longest time to be accessed again.

2. FIFO

FIFO, First In First Out

The page selected to be swapped out is the first page entered.

The algorithm also swaps out frequently accessed pages, increasing the page fault rate.

3. Longest time unused

LRU, Least Recently Used

Although it is impossible to know the situation of the page to be used in the future, it is possible to know the situation of the page used in the past. LRU swaps out the most recently unused pages.

This algorithm can be implemented using a stack, where the page number of the page is stored. When a process accesses a page, it removes the page number from the stack and pushes it on top of the stack. In this way, the most recently accessed page is always at the top of the stack, and the most recently unused page is always at the bottom of the stack.



4. Clock

Clock

An access bit is required. When a page is accessed, the access bit is set to 1.

First, link all pages in the memory into a circular queue. When a page fault occurs, check the access bit of the page pointed to by the current pointer. If the access bit is 0, swap out the page; otherwise, access the page. The bit is set to 0, giving the page a second chance to move the pointer to continue checking.


5. Equipment management

Disk scheduling algorithm

When multiple processes request disk access at the same time, disk scheduling is required to control disk access.

The main goal of disk scheduling is to minimize the average seek time of the disk.

1. First come first serve

FCFS, First Come First Served

Scheduling is performed according to the order in which the process requests access to the disk. The advantage is fairness and simplicity, and the disadvantage is obvious, since no optimization is done on the seek, so the average seek time can be long.

2. Shortest seek time first

SSTF, Shortest Seek Time First

The track that needs to be accessed is the closest to the track where the current head is located for scheduling first. This algorithm does not guarantee the shortest average seek time, but it is much better than FCFS.

3. Scanning Algorithm

SCAN

SSTF will experience starvation. Consider the following situation. The distance between the track requested by the new process and the track where the head is located is always closer than that of a waiting process, so the waiting process will wait forever.

The SCAN algorithm considers the moving direction of the magnetic head on top of the SSTF algorithm, and requires that the track requested to be accessed can be scheduled only in the current moving direction of the magnetic head. Because the direction of movement is considered, the track requested by a process must be scheduled.

When a magnetic head moves from the inside to the outside, moving to the outermost side will change the moving direction to be from the outside to the inside. The movement law is similar to the operation of the elevator, so the SCAN algorithm is often called the elevator scheduling algorithm.

4. Cyclic scanning algorithm

CSCAN

CSCAN makes changes to SCAN, requiring the head to always move in one direction.

6. Links

build system

The following is a hello.c program:

#include <stdio.h>

int main()
{
    printf("hello, world\n");
    return 0;
}

在 Unix 系统上,由编译器把源文件转换为目标文件。

gcc -o hello hello.c

这个过程大致如下:


  1. 预处理阶段:处理以 # 开头的预处理命令;
  2. 编译阶段:翻译成汇编程序;
  3. 汇编阶段:将汇编程序翻译可重定向目标程序,它是二进制的;
  4. 链接阶段:将可重定向目标程序和 printf.o 等单独预编译好的目标文件进行合并,得到最终的可执行目标程序。

目标文件

  1. 可执行目标文件:可以直接在内存中执行;
  2. 可重定向目标文件:可与其他可重定向目标文件在链接阶段合并,创建一个可执行目标文件;
  3. 共享目标文件:可以在运行时被动态加载进内存并链接;

静态链接

静态连接器以一组可重定向目标文件为输入,生成一个完全链接的可执行目标文件作为输出。链接器主要完成以下两个任务:

  1. 符号解析:每个符号对应于一个函数、一个全局变量或一个静态变量,符号解析的目的是将每个符号引用与一个符号定义关联起来。
  2. 重定位:编译器和汇编器生成从地址 0 开始的代码和数据节,链接器通过把每个符号定义与一个内存位置关联起来,从而重定位这些节,然后修改所有对这些符号的引用,使得它们指向这个内存位置。

动态链接

静态库有以下两个问题:

  • 当静态库更新时那么整个程序都要重新进行链接;
  • 对于 printf 这种标准函数库,如果每个程序都要有代码,这会极大浪费资源。

共享库是为了解决静态库的这两个问题而设计的,在 Linux 系统中通常用 .so 后缀来表示,Windows 系统上它们被称为 DLL。它具有以下特点:

  1. 在给定的文件系统中一个库只有一个 .so 文件,所有引用该库的可执行目标文件都共享这个文件,它不会被复制到引用它的可执行文件中;
  2. 在内存中,一个共享库的 .text 节的一个副本可以被不同的正在运行的进程共享。

参考资料

转载自 GitHub上的资源:  https://github.com/CyC2018/Interview-Notebook/blob/master/notes

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325912710&siteId=291194637