cisc 7310x c06: memory management - github pages · cisc 7310x c06: memory management hui chen...

43
CISC 7310X C06: Memory Management Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/8/2018 1 CUNY | Brooklyn College

Upload: others

Post on 02-Jun-2020

12 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CISC 7310X C06: Memory Management - GitHub Pages · CISC 7310X C06: Memory Management Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/8/2018 CUNY | Brooklyn

CISC 7310X

C06: Memory ManagementHui Chen

Department of Computer & Information Science

CUNY Brooklyn College

3/8/2018 1CUNY | Brooklyn College

Page 2: CISC 7310X C06: Memory Management - GitHub Pages · CISC 7310X C06: Memory Management Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/8/2018 CUNY | Brooklyn

Outline

• Recap & issues

• Project 1 feedback

• Memory management: main memory

• No memory abstraction

• Memory abstraction: address space

• Memory segmentation

• Assignment

3/8/2018 CUNY | Brooklyn College 2

Page 3: CISC 7310X C06: Memory Management - GitHub Pages · CISC 7310X C06: Memory Management Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/8/2018 CUNY | Brooklyn

Project 1 Feedback

• Via commenting on git commits at Github

3/8/2018 CUNY | Brooklyn College 3

Page 4: CISC 7310X C06: Memory Management - GitHub Pages · CISC 7310X C06: Memory Management Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/8/2018 CUNY | Brooklyn

Questions?

• Project 1?

• Project 2?

3/8/2018 CUNY | Brooklyn College 4

Page 5: CISC 7310X C06: Memory Management - GitHub Pages · CISC 7310X C06: Memory Management Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/8/2018 CUNY | Brooklyn

Memory Hierarchy

2/1/2018 CUNY | Brooklyn College 5

Secondary Memory (e.g., Magnetic Disk)

Main Memory

Cache

Registers

~10ms

~10ns

~2ns

~1ns

~TB

~GB

~MB

<KB

Page 6: CISC 7310X C06: Memory Management - GitHub Pages · CISC 7310X C06: Memory Management Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/8/2018 CUNY | Brooklyn

Memory Management

• How does an OS provide an abstraction of the memory hierarchy to make it “useful”?

• Main memory

• Virtual memory

• Caching

3/8/2018 CUNY | Brooklyn College 6

Page 7: CISC 7310X C06: Memory Management - GitHub Pages · CISC 7310X C06: Memory Management Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/8/2018 CUNY | Brooklyn

Main Memory Management

• Without abstraction

• With abstraction

• Segmentation

3/8/2018 CUNY | Brooklyn College 7

Page 8: CISC 7310X C06: Memory Management - GitHub Pages · CISC 7310X C06: Memory Management Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/8/2018 CUNY | Brooklyn

No Abstraction

• Directly address physical memory

• three variations

3/8/2018 CUNY | Brooklyn College 8

• Simple memory organization [Figure 3-1 in Tanenbaum & Bos, 2014]

Page 9: CISC 7310X C06: Memory Management - GitHub Pages · CISC 7310X C06: Memory Management Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/8/2018 CUNY | Brooklyn

What would happen if …

• Three variations

int *p = malloc(…)

while (1) {

*p = value;

p ++;

}

3/8/2018 CUNY | Brooklyn College 9

• Simple memory organization [Figure 3-1 in Tanenbaum & Bos, 2014]

Page 10: CISC 7310X C06: Memory Management - GitHub Pages · CISC 7310X C06: Memory Management Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/8/2018 CUNY | Brooklyn

Protection and Parallelism

• When without abstraction,

• How to provide protection: prevent a program overwrite another program or the OS?

• How to run multiple programs?

• Need additional hardware for protection

3/8/2018 CUNY | Brooklyn College 10

Page 11: CISC 7310X C06: Memory Management - GitHub Pages · CISC 7310X C06: Memory Management Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/8/2018 CUNY | Brooklyn

Example: IBM 360

• Allow access only when PSW key matches RAM block key

3/8/2018 CUNY | Brooklyn College 11

1101

2KB Block0000PSW 2KB Block0001

2KB Block0010

2KB Block0011

KEY

KEY

PWS Key == RAM Block Key?

NoYes

Stop ill-behaved program

Page 12: CISC 7310X C06: Memory Management - GitHub Pages · CISC 7310X C06: Memory Management Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/8/2018 CUNY | Brooklyn

Example: Running Multiple Programs

• Naively loading two programs to run

3/8/2018 CUNY | Brooklyn College 12

• Loading two programs [Figure 3-2 in Tanenbaum & Bos, 2014]

Page 13: CISC 7310X C06: Memory Management - GitHub Pages · CISC 7310X C06: Memory Management Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/8/2018 CUNY | Brooklyn

Example: Static Relocation

• OS adds an offset to every address when loading

• If the program is to load to 16384, then add 16380 to every address

• JMP 28 JMP 28 + 16384 JMP 16412

• Needs to know which is an address, which is not

• MOV REGISTER1, 28

• Is “28” an address or a constant?

3/8/2018 CUNY | Brooklyn College 13

Page 14: CISC 7310X C06: Memory Management - GitHub Pages · CISC 7310X C06: Memory Management Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/8/2018 CUNY | Brooklyn

Directly Addressing of Physical Memory• Simple embedded devices

• Examples: radios, washing machines, microwave ovens, coffee machines

• A library on ROM loads a program to run and the program addresses physical memory without abstraction

• Example: the eCos system (http://ecos.sourceware.org/)

• “eCos is a single process, multiple thread operating environment. As such, memory management is not required. … There is no notion of separate address "spaces" in eCos like there would be in a system like Linux. All threads share the same address space and access capabilities. ”

• https://sourceware.org/viewvc/ecos/

3/8/2018 CUNY | Brooklyn College 14

Page 15: CISC 7310X C06: Memory Management - GitHub Pages · CISC 7310X C06: Memory Management Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/8/2018 CUNY | Brooklyn

Questions?

• No abstraction: directly addressing physical memory

• How to run multiple programs?

• How to provide protection?

• How to provide relocation (static relocation)?

• Where is it commonly used?

3/8/2018 CUNY | Brooklyn College 15

Page 16: CISC 7310X C06: Memory Management - GitHub Pages · CISC 7310X C06: Memory Management Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/8/2018 CUNY | Brooklyn

Memory Abstraction

• Notion of address space

• Base and limit registers

• Swapping

• Managing free memory

3/8/2018 CUNY | Brooklyn College 16

Page 17: CISC 7310X C06: Memory Management - GitHub Pages · CISC 7310X C06: Memory Management Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/8/2018 CUNY | Brooklyn

Address Space

• Two problems

• Protection and relocation

• A logical concept

• A process has its own independent “address space”, a set of addresses the process can use to address memory

• How do we establish the correspondence between the “logical address” and the address of the physical memory?

• Dynamic relocation

3/8/2018 CUNY | Brooklyn College 17

Page 18: CISC 7310X C06: Memory Management - GitHub Pages · CISC 7310X C06: Memory Management Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/8/2018 CUNY | Brooklyn

Dynamic Relocation

• Example: using base and limit registers

• Map each process’s address space onto a different part of physical memory

• Processor has two registers

• Base register loaded with beginning physical address

• Limit register loaded with length of the program

3/8/2018 CUNY | Brooklyn College 18

Page 19: CISC 7310X C06: Memory Management - GitHub Pages · CISC 7310X C06: Memory Management Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/8/2018 CUNY | Brooklyn

Base and Limit Registers

• Hardware support, when reference to a memory, CPU does the following,

3/8/2018 CUNY | Brooklyn College 19

Base register Address in address space in process

Within length of the limit register

Yes No

Fault

Page 20: CISC 7310X C06: Memory Management - GitHub Pages · CISC 7310X C06: Memory Management Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/8/2018 CUNY | Brooklyn

Running Multiple Programs

• Base and limit registers realize a simple dynamic relocation and protection

• Base register: dynamic relocation done by CPU

• Limit register: protection done by CPU

3/8/2018 CUNY | Brooklyn College 20

• Dynamic relocation via base and limit registers [Figure 3-3 in Tanenbaum & Bos, 2014]

Page 21: CISC 7310X C06: Memory Management - GitHub Pages · CISC 7310X C06: Memory Management Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/8/2018 CUNY | Brooklyn

Base and Limit Registers: Examples

• Implementation vary

• Whether base and limit registers are protected? Who can modify these registers?

• CDC 6600 provides protection; Intel 8088 does not

• Whether a processor has different base and limit registers to protect for programs (instructions) and data, respectively

3/8/2018 CUNY | Brooklyn College 21

Page 22: CISC 7310X C06: Memory Management - GitHub Pages · CISC 7310X C06: Memory Management Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/8/2018 CUNY | Brooklyn

Swapping

• A corollary to Parkinson’s law: “Programs expands to fill the memory available to hold them”

• Running many programs may require more memory than is available

• Two solutions

• Swapping

• Virtual memory

3/8/2018 CUNY | Brooklyn College 22

Page 23: CISC 7310X C06: Memory Management - GitHub Pages · CISC 7310X C06: Memory Management Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/8/2018 CUNY | Brooklyn

Swapping & Virtual Memory

• Swapping

• Brining in a process in entirety from the “disk”, run the process, and putting it back on the “disk”

• Idle processes are mostly stored on disk

• Virtual memory

• Allow a process to run even if it is only partially in main memory

• To be discussed next

3/8/2018 CUNY | Brooklyn College 23

Page 24: CISC 7310X C06: Memory Management - GitHub Pages · CISC 7310X C06: Memory Management Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/8/2018 CUNY | Brooklyn

Swapping: Example

• Swapping of a few processes

3/8/2018 CUNY | Brooklyn College 24

• Swapping of multiple processes [Figure 3-4 in Tanenbaum & Bos, 2014]

Page 25: CISC 7310X C06: Memory Management - GitHub Pages · CISC 7310X C06: Memory Management Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/8/2018 CUNY | Brooklyn

Memory Compaction

• Swapping creates multiple holes in memory

• Sometimes it is necessary to combine multiple holes into big one

3/8/2018 CUNY | Brooklyn College 25

Page 26: CISC 7310X C06: Memory Management - GitHub Pages · CISC 7310X C06: Memory Management Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/8/2018 CUNY | Brooklyn

Growing Program Data Segment

• In many programming languages, program data can grow in size

• Example:

• int *a = malloc(…)

• Object o = new Object()

• What if memory hole is not big enough to accommodate the growing program data?

• Move process

• Compact memory

• Swap out one or more processes

• Suspend the process until more memory is available

• Killed

3/8/2018 CUNY | Brooklyn College 26

Page 27: CISC 7310X C06: Memory Management - GitHub Pages · CISC 7310X C06: Memory Management Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/8/2018 CUNY | Brooklyn

Proactive Approach

• Process growing in size expected, allocate extra memory when a process is swapped in or moved

• Growing data segment

• Growing stack segment

3/8/2018 CUNY | Brooklyn College 27

Page 28: CISC 7310X C06: Memory Management - GitHub Pages · CISC 7310X C06: Memory Management Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/8/2018 CUNY | Brooklyn

Proactive Approach: Examples

• For data segment; and for data & stack segments

3/8/2018 CUNY | Brooklyn College 28

• Growing program data [Figure 3-5 in Tanenbaum & Bos, 2014]

Page 29: CISC 7310X C06: Memory Management - GitHub Pages · CISC 7310X C06: Memory Management Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/8/2018 CUNY | Brooklyn

Managing Free Memory

• Keep track of memory usage

• Data structures & algorithms

• Bitmaps

• Linked lists

3/8/2018 CUNY | Brooklyn College 29

Page 30: CISC 7310X C06: Memory Management - GitHub Pages · CISC 7310X C06: Memory Management Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/8/2018 CUNY | Brooklyn

Bitmaps

• A data structure keep tracks memory allocation

• Specify an allocation unit, have many allocation units

• Example: 4 KB

• A bit in a bitmap indicate an allocation unit is free or allocated

• Example

• Free: 1

• Allocated: 0

3/8/2018 CUNY | Brooklyn College 30

Page 31: CISC 7310X C06: Memory Management - GitHub Pages · CISC 7310X C06: Memory Management Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/8/2018 CUNY | Brooklyn

Bitmap: Example

• An example allocation and its bitmap

3/8/2018 CUNY | Brooklyn College 31

• Linked list of memory allocation [Figure 3-6 in Tanenbaum & Bos, 2014]

Page 32: CISC 7310X C06: Memory Management - GitHub Pages · CISC 7310X C06: Memory Management Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/8/2018 CUNY | Brooklyn

Design of Bitmap

• What should be the size of the allocation unit?

• Two competing factors

• When allocation unit become smaller, bitmap larger

• How big is a bitmap?

• When allocation unit becomes larger, memory waste larger

• How much waste is there?

• How about search the bitmap to locate free memory?

3/8/2018 CUNY | Brooklyn College 32

Page 33: CISC 7310X C06: Memory Management - GitHub Pages · CISC 7310X C06: Memory Management Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/8/2018 CUNY | Brooklyn

Linked List

• Maintain a linked list of allocated memory and free memory segments

3/8/2018 CUNY | Brooklyn College 33

• Linked list of memory allocation [Figure 3-6 in Tanenbaum & Bos, 2014]

Page 34: CISC 7310X C06: Memory Management - GitHub Pages · CISC 7310X C06: Memory Management Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/8/2018 CUNY | Brooklyn

Linked List: Example

• Allocation and deallocation scenarios

3/8/2018 CUNY | Brooklyn College 34

• Linked list of memory allocation [Figure 3-7 in Tanenbaum & Bos, 2014]

Page 35: CISC 7310X C06: Memory Management - GitHub Pages · CISC 7310X C06: Memory Management Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/8/2018 CUNY | Brooklyn

Design of Linked List

• Single-linked list or double-linked list?

• Separate list of processes or holes?

• A few algorithms

• First fit: starting from beginning

• Next fit: starting from last time

• Best fit: takes the smallest hole

• Worst fit: takes the largest hole

• Quick fit: based on allocation pattern

• Additional data structure?

• Example: Holes can be sorted in size (any data structure for ordered list)

3/8/2018 CUNY | Brooklyn College 35

Page 36: CISC 7310X C06: Memory Management - GitHub Pages · CISC 7310X C06: Memory Management Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/8/2018 CUNY | Brooklyn

Questions?

• Address space

• Concept?

• Simple mechanism for dynamic allocation and protection

• Swapping

3/8/2018 CUNY | Brooklyn College 36

Page 37: CISC 7310X C06: Memory Management - GitHub Pages · CISC 7310X C06: Memory Management Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/8/2018 CUNY | Brooklyn

Address Space and Process

• One process one address space?

3/8/2018 CUNY | Brooklyn College 37

Page 38: CISC 7310X C06: Memory Management - GitHub Pages · CISC 7310X C06: Memory Management Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/8/2018 CUNY | Brooklyn

Compiled Tables

• A compiler typically builds many tables when processing a program

1. The source text being saved for the printed listing

2. The symbol table, names and attributes of variables.

3. The table containing integer and floating-point constants used.

4. The parse tree, syntactic analysis of the program.

5. The stack used for procedure calls within compiler.

• Each of these grows continuously as compilation proceeds

3/8/2018 CUNY | Brooklyn College 38

Page 39: CISC 7310X C06: Memory Management - GitHub Pages · CISC 7310X C06: Memory Management Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/8/2018 CUNY | Brooklyn

Growing Tables: Example

• One address space for all?

3/8/2018 CUNY | Brooklyn College 39

• One address space for all [Figure 3-30 in Tanenbaum & Bos, 2014]

Page 40: CISC 7310X C06: Memory Management - GitHub Pages · CISC 7310X C06: Memory Management Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/8/2018 CUNY | Brooklyn

Segmentation

• Provide many completely independent address spaces, called segments

• Each segment consists of a linear sequence of address

• Different segments can have different lengths

• Segments can grow or shrink independently without affection others

3/8/2018 CUNY | Brooklyn College 40

Page 41: CISC 7310X C06: Memory Management - GitHub Pages · CISC 7310X C06: Memory Management Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/8/2018 CUNY | Brooklyn

Segmentation: Example

• Segments can grow or shrink independently without affection others

3/8/2018 CUNY | Brooklyn College 41

• Multiple segments [Figure 3-31 in Tanenbaum & Bos, 2014]

Page 42: CISC 7310X C06: Memory Management - GitHub Pages · CISC 7310X C06: Memory Management Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/8/2018 CUNY | Brooklyn

Questions

• Concept of segmentation

3/8/2018 CUNY | Brooklyn College 42

Page 43: CISC 7310X C06: Memory Management - GitHub Pages · CISC 7310X C06: Memory Management Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/8/2018 CUNY | Brooklyn

Assignment

• Practice assignment

• Review Guide #1 and Take-Home Test #1

3/8/2018 CUNY | Brooklyn College 43