Memory in OS
Memory in OS
A program must be brought from disk into main memory and loaded within a process for it to be run.
- Executable code (compiled) or source code (interpreted).
- Recall that a process is a program in execution.
Main memory/registers are storage that CPU can directly access.
- A register can be accessed in one CPU clock cycle.
- A main memory access can take many cycles.
- Cache sits between main memory and CPU registers to improve memory access time.
Address Binding to Memory
绑定地址是指在程序运行时将编译后的程序代码中的地址转化为内存地址的过程。
Address binding of instructions and data to memory can happen at three different stages.
编译时间
- 如果内存位置提前知道,可以生成绝对代码。
- 如果起始位置发生变化,必须重新编译代码。
加载时间
- 如果内存位置在编译时不知道,必须生成可重定位的代码。
- 地址在程序加载后定义。
The logical and physical addresses are the same in both compile-time and load-time address-binding schemes, but the logical and physical addresses differ in run-time address-binding scheme.
执行或运行时间
- 如果进程在执行过程中可以从一个内存段移动到另一个,必须推迟到运行时间进行绑定。
- 需要硬件支持动态地址映射(base and limit registers)。
Only the translation from virtual to physical address needs to be changed when a program moves around.
Logical and Physical Address
内存管理技术需要支持运行时的地址绑定。这是因为程序在执行过程中可能会四处移动。
内存管理的关键思想是逻辑地址和物理地址的分离。
逻辑地址是由CPU生成的地址,例如,一条指令指的是存储在某个特定地址的整数。当它与物理地址不同时,这也被称为虚拟地址。
物理地址是被主存储器发送的地址。
Memory Management Unit
每个虚拟地址必须被映射或翻译成运行时绑定方案中的物理地址,这在获取指令和获取每个操作数时发生一次。
这种转换必须是非常快速和有效的,所以它必须由硬件完成。
将虚拟地址映射到物理地址的硬件设备被称为内存管理单元或MMU。
User process only deals with logical addresses. It never sees the real physical addresses.
In the simplest MMU solution, the value in a special relocation register is added to every logical address generated by a user process to form the physical address when it is sent to memory. The relocation register is like the base register.「在最简单的MMU解决方案中,一个特殊的重定位寄存器中的值被添加到每个由用户进程产生的逻辑地址中,当它被发送到内存时,形成物理地址。重定位寄存器就像基础寄存器一样。」
Memory Allocation
Main memory is usually divided into two partitions.
- Resident OS is usually stored in low memory, together with interrupt vector and interrupt handlers.
- User processes are stored in high memory.
Memory allocation is concerned with「被涉及到」 where a user process is actually placed when it is swapped/moved into main memory.
We assume that the whole process will be stored in the main memory for it to be executed.
Contiguous Allocation
Contiguous allocation: allocate a single block of memory of size sufficient to hold the process. 「连续分配:分配一个大小的单个内存块,足以保持该过程。」
The most straightforward solution is to allocate a single block of memory to hold a process. This is called contiguous allocation.
- One single block to hold one process (single partition).
- Multiple blocks to hold multiple processes (multiple partition).
A pair of registers are used to protect user processes from accidentally stepping into each other and changing operating system code and data.
- The relocation register contains the value of smallest physical address for the process.
- The limit register contains the range (size) of logical address space.
- Each logical address must be less than the limit register.
- MMU maps logical address dynamically into physical address by adding it to the relocation register.
Non-contiguous allocation: chop up the process and allocate multiple blocks of memory to hold the process. The two major schemes are paging and segmentation.
Multiple fixed partition method
四十年前,最早的、经典的操作系统之一IBM MVS就使用了这个方法。
- Called MFT: Multiprogramming with a Fixed number of Tasks.
- Divide the memory into several partitions.
- When a partition is free, a job is selected to be loaded into the partition.
- Only at most n jobs can be executed if there are n partitions.
- Very simple to manage.
- 如果有许多小工作,就不太有效,因为许多分区只被使用到非常小的程度,总的可用内存仍然相当大。
Multiple variable partition method
Commonly used in IBM MVS.
- Called MVT: Multiprogramming with a Variable number of Tasks.
- A block of available memory is called a hole.
- 不同大小的孔散布在整个存储器中。「Holes of various size are scattered throughout the memory. 」
- 当一个进程到达时,它被从一个足够大的洞中分配内存,以容纳它。「When a process arrives, it is allocated memory from a hole large enough to accommodate it. 」
- OS maintains information about allocated partitions and free partitions (i.e. holes), often in the form of linked lists.