操作系统 ShanghaiTech CS130 | Main memory

ShanghaiTech CS130 | Lecture Note 02 | Main memory

Topic: memory management algoirhtms, paging, segmentation

Keyword: paging, fragmentation, swapping, sharing, protection

说明:笔记旨在整理我校CS130课程的基本概念。由于授课及考试语言为英文,故用英文为主,中文为辅的方式整理。由于是整理,尽提供最低限度的解释,以便提供简洁快速的查阅。

全部笔记索引:【传送门】 | 上一节: Scheduling  | 下一节: Virtual Memory

目录

ShanghaiTech CS130 | Lecture Note 02 | Main memory

1 Names and Binding

1.1 Definition of names

1.2 Timing of binding to memory(恐龙书9.1.2 Address binding)

1.3 Dynamic Loading(恐龙书9.1.4)

1.4 Dynamic Linking(恐龙书9.1.5)

1.5 Overlay

2 Logical vs. Physical address(恐龙书9.1.3)

2.1 priminal defintion of logical address and physical address

2.2 Memory Mangement Unit (MMU)

3 Swapping(恐龙书9.5)

3.1 常见术语

3.2 Implementation convention

3.3 Context switch time including swapping

4 Contiguous Allocation(恐龙书9.2)

4.1 Single-partition allocation vs. Multiple-partitoin allocation

4.2 Dynamic storage allocation(恐龙书9.2.2)

4.3 Fragmentation(恐龙书9.2.3)

5 Paging(恐龙书9.3)

5.1 Basic ideas(9.3.1)

5.2 Implementation of page table

5.3 Effective Access Time (p367)

5.4 Memory Protection(9.3.3)

5.5 Hierarchical Paging(9.4.1)

5.6 ASID

5.7 Hashed Page Tables(恐龙书9.4.2)

5.8 Inverted page table(恐龙书9.4.3)

5.9 Paging and Copy on write

6 Paging advanced topics

6.1 Shared Pages

6.2 Segmentation

7 Examples

7.1 Intel A-32

7.2 Linux on Pentium

Reference


1 Names and Binding

1.1 Definition of names

Symbolic->Logical->Physical names

Def: Symbolic names: known in a context or path

  • file names, program names, printer/device names, usernames

Def: Logical names: used to label a specific entity

  • inodes, job number, major/minor device number, pid, uid, gid, ...

Def: Physical names: address of entity

  • inode address on disk or memory, entry point or variable address, PC address

1.2 Timing of binding to memory(恐龙书9.1.2 Address binding)

1.Intuition: bind symbolic address(variable name) to some address. CA(computer architecture) has taught us that a complier binds the symbolic address to relocatable address. The linker or loader in turn binds the relocatable addresses to absolute address. Each binding is a mapping from one address space to another.

2. Binding occurs at 3 different stages:

  • Compile time
    • Generate absolute code
    • Must recompile code if starting location changes
  • Load time
    • Must generate relocateable code
  • Execution time
    • Binding delayed until runtime(process can be moved during its execution)
    • Need HW support for address maps

3. Binding time tradeoffs

  • Early binding(At compile time)
    • Compiler: produces efficient code
    • Allows checking to be done early
    • Allows estimates of running time and space
  • Delayed binding(at load time)
    • linker, loader
    • produces efficient code, allows separate compliation(for library)
    • portability and sharing of object codes
  • Late binding
    • VM, dynamic linking/loading, overlaying, interpreting
    • code less efficient, checks done at runtime
    • flexible, allows dynamic reconfiguration
bind时间图示

1.3 Dynamic Loading(恐龙书9.1.4)

Def: Routine is not loaded until it is called. All routines are kept on disk in a relocatable load format.

Pros: Better memoery-space utilization. Unused routine is never loaded. Useful when large code, infrequently

Cons: No special support from OS. Implemented through program design(by providing library routines).

1.4 Dynamic Linking(恐龙书9.1.5)

Def: Linking postponed until execution time, can be shared among processes (like C std library).

Introduction to stub: to locate the memory-resident library routine. stub replaces itself with the address of the routine, and executes the routine. OS needed to check if routine in memory.

1.5 Overlay

WIKI

the process of transferring a block of program code or other data into main memory, replacing what is already stored

Keep in memory only needed. The policy is implemented by user and complex, due to the issue that process is too large. 

2 Logical vs. Physical address(恐龙书9.1.3)

2.1 priminal defintion of logical address and physical address

Def: Logical address generated by CPU.

Def: Physical address seen by memory unit.

Same when compiling and load-time binding.

Differs in execution-time address-binding(due to efficient implementation of memory loading policy). Therefore, it is "fake" w.r.t. real address in memory unit. we call it virtual address.

2.2 Memory Mangement Unit (MMU)

 Def: A HW that maps virtual to phsyical address.

Property: be transparent to user program(never get touched with real physical address)

3 Swapping(恐龙书9.5)

3.1 常见术语

Def: Swapping: process swap out/ brought back for continued execution

  • backing store: fast disk large enough to accommodate copies of all memory images for all users; must provide direct access to these memory images
  • roll out/in: lower priority out, higher ones in
  • transfer time: major part of swap time
  • ready queue: ready-to-run processes

3.2 Implementation convention

Modified versions of swapping on many systems(i.e. Unix, Linux, and Windows)

  • Normally disabled
  • Started if more memory allocated (threshold)
  • disabled once memory demand reduce.

Remark: swapping 就是用来应对内存不足的情况,把优先级低的弟弟进程放到二级存储当中。

3.3 Context switch time including swapping

If next process is not in memory,swap out a process. context switch time can be very high.

can reduce the context switch time by knowning how much memory being used.

4 Contiguous Allocation(恐龙书9.2)

4.1 Single-partition allocation vs. Multiple-partitoin allocation

1. Single partition allocation

  • relcation register contains smallest physical address
  • limit register contains range logical address

2. Multiple partitoin allocation:

  • main memory is divided into a number of fixed-sized partitions
  • each partition should contain only one process

3. def MFT: Multiprogramming with a Fixed number of Tasks.

4. def MVT: Multiprogramming with a Variable number of Tasks

5. def Holes: block of available memory

4.2 Dynamic storage allocation(恐龙书9.2.2)

  1. First-fit: Allocate the first hole that is big enough
  2. Best-fit: Allocate the smallest hole which is big enough
  3. Worst-fit: Allocate the largest hole

First-fit and best-fit are better than worst-fit in practice

4.3 Fragmentation(恐龙书9.2.3)

1. External fragmentation: total memory space exists to satisfy a request/ not contiguous.

2. Internal fragmentation: allocate memory slightly larger than requested/ memory internal to a partition, not being used

3. compact:(恐龙书p360) shuffle memory contents, place free memory toghether in one large block

5 Paging(恐龙书9.3)

5.1 Basic ideas(9.3.1)

Motivation: Physical address space of a process can be noncontiguous; process is allocated physical memory whenever the latter is available. Our goal is to avoid external fragmentation& problems of varying sized memory chunks.

1.def frame: fixed-sized blocks that divde physical memory.

2.def pages: divde logical memory into blocks called pages.

3.def page table: a tale to translate logical to physical address

4. address translation scheme: (1) page number(p) - used as an index into a per-process page table which contains base address of each page in physical memory. (2) page offset(d) - combined with base address to define the physical memory address that is sent to the memory unit.

5.2 Implementation of page table

1.def PTBR(page-table base register): points to the page table

2.def PTLR(... length register): indicates size of the page table

3. In this scheme every data/instruction access requires two memory accesses: one for the page table and one for the data /instruction

4. memory access problem remedy: associative memory (i.e: translation look-aside buffers TLB)

 

paging hardwares

5.3 Effective Access Time (p367)

1.def lookup TLB cost: \epislon\epsilon time unit

2.def hit ratio α: percentage of times that a page number is found in the associative registers; ratio related to number of associative registers

3.

assume access memory cost 1 here. In practice, \epsilon may be small enough that can be neglected

5.4 Memory Protection(9.3.3)

  • associate protection bits with each frame
  • valid/invalid bit
  • indicate if the associated page is in the process' logical address space

5.5 Hierarchical Paging(9.4.1)

Motivation: page table is too large/do not want to allocate contiguously/split it to multilevel

1. two-level page-table scheme:

(1) example: a logical address (32 bit machine, 4K page size) is divided into: a page number (20bits)/a page offset (12bits). outer page table is page with a page number(e.g:10 bits) and a page offset(10 bits, with 10 + 10 = 20(previous page num.)).

(2) a logical addr. is organized as: (p1, p2, d)

2.multilevel paging(similar)

3. caching can help performance

5.6 ASID

Motivation: different process has different address spaces / same virtual addrs. lead to different physical addr. / Solution (1)TLB fresh for each process when doing context switch. (2)require a identifier: address-space identifier(ASID).

def: an ASID uniquely identifies each process and is used to provide address-space protection for that process.

5.7 Hashed Page Tables(恐龙书9.4.2)

1.property: hash virtual page number into a page table. Each elements contains: virtual page number / value of the mapped page frame / a pointer to the next element

2. Virtual page numbers are compared in this chain searching for a match: If a match is found, the corresponding physical frame is extracted.

3.Variation for 64-bit addr. is clustered page tables: similar to hashed but each entry refers to several pages (such as 16) rather than 1 / especially useful for sparse address space (where memory references are non-contiguous and scattered)

5.8 Inverted page table(恐龙书9.4.3)

1. one entry for frame(real page) contains: (1) virtual address. (2) information about process that owns the page.

2.property: (1) decreases memory to stoarge page table(one table now). (2) increase time to search table(one table now). (3) use hash table to limit search to one page-table entries.

3. virtual address <pid, page num., offset> / inverted page table: <pid, p>.

search for match: if found: <i, d>/ if NOT found: illegal address.

5.9 Paging and Copy on write

1.Can we share memory between process? Set entries in both page tables to point to same page frames/ need core map of page frames to track which processes are pointing to which page frames (e.g. reference count)

2.Unix fork with copy on write: copy page table of parent into child process/ mark all pages as read-only (new and old page tables) / trap into kernel on write (in child or parent) / copy page / mark both as writable / resume execution.

6 Paging advanced topics

6.1 Shared Pages

1.motivation: code and data can be shared among processes: (1) reentrant (not self-modifying) code can be shared. (2) common page frame mapping. (3) single copy of read-only code (compilers, editors, etc.)

2.must appear in the same logical location.

3.private code and data: can appear anywhere in logical addr. space.

6.2 Segmentation

motivation:

From WIKI:

Segmentation is one method of implementing memory protection.Paging is another, and they can be combined. The size of a memory segment is generally not fixed and may be as small as a single byte.

1.def segmentation: a contiguous region of virtual memory

map 2D user-defined addresses (base,limit) -> 1D physical memory. table entry: <base, limit>

2.prop: (1)each process has a segment table: in hardware / entry in table = segment. (2) can be located anywhere in physical memory. (3) each segment has: start, length, access permission. (4) processes can share segments: same start, length. (4) protect each entity independently. (5) allow each segment to grow independently. (6) share each segment independently.

3. architecture: logical addr. consists of two tuples: (1) <segment-number, offset> (2) User specifies only a single addr. (3) Partitioned by HW into <page number, offset>. (4) Invisible to programmers. 

4.STBR: segment-table base register (STBR) points to the segment table's location in memory.

5.STLR: segment-table length register indicates the number of segments

6.problems: (1)sharing: shared segments must have the same segment. sharing happens at segment level. (2) allocation: dynamic allocation problem. (3) protection: r/w/e privileges. check ilegal array indexes by alloacte it in a separate segment.

7.zero-on-reference: (1) stack and heap is alloacted exactly the memory they need. 92) stack growth: segmentation fault into OS kernel/ kernel allocates some memory/zero the memory/modify segment table/resume process

8. segment-table entry contains base addr. of page table for the segment, but not the address of segment.

9. do copy on write by segment: (1) copy segment table into child. (2) mark parent & child segments read-only. (3) start child process; return to parent. (4) If child or parent writes to a segment: trap into kernel/make a copy of the segment and resume.

10. pros: share code/data segments between processes/ protect code segment from being overwritten / transparently grwo stack/heap as needed/ detect if need to copy-on write. 

11. cons: complex memoery management / may need to rearrange memory from time to time to make room for new/growing segment

7 Examples

7.1 Intel A-32

7.2 Linux on Pentium

Q&A:

1. What is TLB?

TLB(translation look aside buffer转换检测缓冲区) boosts translation of virtual addr. to physical addr. through by passing querying of page table. It is a special, small, fast-lookup HW.

Reference

1. Operating System Concepts 10th Edition. WILEY

2. ShanghaiTech CS130 Operating system, Topic 9, Main memory. Shu-Yin.

3. Operating System Concepts slides of 9th Edition

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值