![Page 1: CS 241 Spring 2007 System Programming 1 Introduction to Memory Management Lecture 28 Lawrence Angrave/Klara Nahrstedt](https://reader035.vdocument.in/reader035/viewer/2022062517/56649f385503460f94c55017/html5/thumbnails/1.jpg)
CS 241 Spring 2007System Programming
1
Introduction to Memory Management
Lecture 28
Lawrence Angrave/Klara Nahrstedt
![Page 2: CS 241 Spring 2007 System Programming 1 Introduction to Memory Management Lecture 28 Lawrence Angrave/Klara Nahrstedt](https://reader035.vdocument.in/reader035/viewer/2022062517/56649f385503460f94c55017/html5/thumbnails/2.jpg)
2
CS241 Administrative Read Stallings Chapter 7 & 7A about Memory
Management Regular Quiz 9 this week on Memory Management Discussion Sessions this week (on Memory Management
– needed for LMP2) LMP2 (Long Machine Problem) starts today
LMP 2 is part of three major assignments (LMP1, LMP2, LMP3) LMP2 will be split into two parts, PART I and overall LMP2 delivery. You will need to deliver PART I by Monday, April 9 and the final
overall LMP2 will be due by April 16. For each delivery you will receive points.
LMP2 quiz will be on April 16. The graders will provide feedback on the Part I that should assist you
in the overall LMP2 delivery.
![Page 3: CS 241 Spring 2007 System Programming 1 Introduction to Memory Management Lecture 28 Lawrence Angrave/Klara Nahrstedt](https://reader035.vdocument.in/reader035/viewer/2022062517/56649f385503460f94c55017/html5/thumbnails/3.jpg)
3
Contents
Addressing Requirements for a ProcessNames and Binding, Loading, Compiling, Run-Time
Storage hierarchySimple Programming
OverlaysResident monitorMulti-Programming
Fixed PartitioningRelocation RegisterSwapping
![Page 4: CS 241 Spring 2007 System Programming 1 Introduction to Memory Management Lecture 28 Lawrence Angrave/Klara Nahrstedt](https://reader035.vdocument.in/reader035/viewer/2022062517/56649f385503460f94c55017/html5/thumbnails/4.jpg)
4
Names and Binding
Naming = very deep theme that comes up everywhere
IndirectionSymbolic names
Logical names
Physical names
Most used by application programmers
Converted by system programmers of compilers, OS
![Page 5: CS 241 Spring 2007 System Programming 1 Introduction to Memory Management Lecture 28 Lawrence Angrave/Klara Nahrstedt](https://reader035.vdocument.in/reader035/viewer/2022062517/56649f385503460f94c55017/html5/thumbnails/5.jpg)
5
Symbolic Names
Symbolic names: known in a context or path File names
Program names
Printer/device names
User names
Variables
Convenient to use/reference
![Page 6: CS 241 Spring 2007 System Programming 1 Introduction to Memory Management Lecture 28 Lawrence Angrave/Klara Nahrstedt](https://reader035.vdocument.in/reader035/viewer/2022062517/56649f385503460f94c55017/html5/thumbnails/6.jpg)
6
Logical Names
Logical names: label a specific entity
… but independent of specific physical entity
Inode number
Job number
Major, minor device numbers
uid, pid, gid
Absolute address in program
![Page 7: CS 241 Spring 2007 System Programming 1 Introduction to Memory Management Lecture 28 Lawrence Angrave/Klara Nahrstedt](https://reader035.vdocument.in/reader035/viewer/2022062517/56649f385503460f94c55017/html5/thumbnails/7.jpg)
7
Physical Names
Physical names: address of entityPhysical Inode address on disk or memory (track,
cylinder, …)
Entry point or variable address
Memory-mapped register addresses
Process control block address
Difficult to use by programmersTherefore, system software (compilers, loaders, OS)
are usually the ones that convert symbolic names/logic names to physical names
![Page 8: CS 241 Spring 2007 System Programming 1 Introduction to Memory Management Lecture 28 Lawrence Angrave/Klara Nahrstedt](https://reader035.vdocument.in/reader035/viewer/2022062517/56649f385503460f94c55017/html5/thumbnails/8.jpg)
8
Binding Time of Names
Binding: map names to locations (values)Late binding … Early binding
Different timesSource program (.c file)
Compile time
Object module (.o file)Link time
Load module Load time
Run-time
![Page 9: CS 241 Spring 2007 System Programming 1 Introduction to Memory Management Lecture 28 Lawrence Angrave/Klara Nahrstedt](https://reader035.vdocument.in/reader035/viewer/2022062517/56649f385503460f94c55017/html5/thumbnails/9.jpg)
9
Binding at Compile & Link time
Create object codegcc -c part1.c
gcc -c part2.c
gcc -o wowee part1.o part2.o
gcc uses linker program 'ld' to link object code together.
![Page 10: CS 241 Spring 2007 System Programming 1 Introduction to Memory Management Lecture 28 Lawrence Angrave/Klara Nahrstedt](https://reader035.vdocument.in/reader035/viewer/2022062517/56649f385503460f94c55017/html5/thumbnails/10.jpg)
10
What does a process look like? (Unix)
Process address space divided into “segments” text (code), data, heap (dynamic data), and stack
heap
stack
code
initialized data
address 2^n-1
address >= 0
![Page 11: CS 241 Spring 2007 System Programming 1 Introduction to Memory Management Lecture 28 Lawrence Angrave/Klara Nahrstedt](https://reader035.vdocument.in/reader035/viewer/2022062517/56649f385503460f94c55017/html5/thumbnails/11.jpg)
11
Who Binds What?
Heap: constructed and layout by allocator (malloc)compiler, linker not involved other than saying where it can start namespace constructed dynamically and managed by
programmer (names stored in pointers, and organized using data structures)
Stack: alloc dynamic (proc call), layout by compilernames are relative off of stack pointermanaged by compiler (alloc on proc entry, dealloc on exit)linker not involved because name space entirely local: compiler
has enough information to build it.
Global data & code: allocation static (compiler), layout (linker)compiler emits them and can form symbolic references between
them (“jalr _printf”)linker lays them out, and translates references
![Page 12: CS 241 Spring 2007 System Programming 1 Introduction to Memory Management Lecture 28 Lawrence Angrave/Klara Nahrstedt](https://reader035.vdocument.in/reader035/viewer/2022062517/56649f385503460f94c55017/html5/thumbnails/12.jpg)
12
Compiling• Compiler:
– doesn’t know where data/code should be placed in the process’s address space
– assumes everything starts at zero– emits symbol table that holds the
name and offset of each created object
– routine/variables exported by the file are recorded global definitionglobal definition
• Simpler perspective:– code is in a big char array– data is in another big char array– compiler creates (object name,
index) tuple for each interesting thing
– linker then merges all of these arrays
40
foo: call printf ret bar: … ret
0
foo: 0bar: 40
![Page 13: CS 241 Spring 2007 System Programming 1 Introduction to Memory Management Lecture 28 Lawrence Angrave/Klara Nahrstedt](https://reader035.vdocument.in/reader035/viewer/2022062517/56649f385503460f94c55017/html5/thumbnails/13.jpg)
13
Linkers (Linkage editors)
Unix: ldusually hidden behind compiler
Three functions:collect together all pieces of a programcoalesce like segmentsfix addresses of code and data so the program can run
Result: runnable program stored in new object fileWhy can’t compiler do this?
Limited world view: one file, rather than all files
Note *usually*:linkers only shuffle segments, but do not rearrange their internals. E.g., instructions not reordered; routines that are never called are not
removed from a.out
![Page 14: CS 241 Spring 2007 System Programming 1 Introduction to Memory Management Lecture 28 Lawrence Angrave/Klara Nahrstedt](https://reader035.vdocument.in/reader035/viewer/2022062517/56649f385503460f94c55017/html5/thumbnails/14.jpg)
14
Loading before Running
On Unix systems, read by “loader”
reads all code/data segs into buffer cache; maps code (read only) and initialized data (r/w) into addr space
Optimization opportunities:Zero-initialized data does not need to be read in.Demand load: wait until code used before get from diskCopies of same program running? Share codeMultiple programs use same routines: share code (harder)
ld loader Cache
Compile time runtime
![Page 15: CS 241 Spring 2007 System Programming 1 Introduction to Memory Management Lecture 28 Lawrence Angrave/Klara Nahrstedt](https://reader035.vdocument.in/reader035/viewer/2022062517/56649f385503460f94c55017/html5/thumbnails/15.jpg)
15
Run-time
Maps virtual addresses to physical addresses
VM, overlaying, recursion, relocation
![Page 16: CS 241 Spring 2007 System Programming 1 Introduction to Memory Management Lecture 28 Lawrence Angrave/Klara Nahrstedt](https://reader035.vdocument.in/reader035/viewer/2022062517/56649f385503460f94c55017/html5/thumbnails/16.jpg)
16
Binding Time Trade-offs(1)
Early binding Compiler
+ Produces efficient code
+ Allows checking to be done early
+ Allows estimates of running time and space
- inflexible
- require fixed hardware
- difficult to support multiprogramming
![Page 17: CS 241 Spring 2007 System Programming 1 Introduction to Memory Management Lecture 28 Lawrence Angrave/Klara Nahrstedt](https://reader035.vdocument.in/reader035/viewer/2022062517/56649f385503460f94c55017/html5/thumbnails/17.jpg)
17
Binding Time Trade-offs(2)
Delayed binding Linker, loader
+ Produces efficient code
+ Allows separate compilation
+ Provides libraries, system interfaces
+ Portability and sharing of object code
+ Checking of consistency
- inflexible
- medium difficult to support multiprogramming
![Page 18: CS 241 Spring 2007 System Programming 1 Introduction to Memory Management Lecture 28 Lawrence Angrave/Klara Nahrstedt](https://reader035.vdocument.in/reader035/viewer/2022062517/56649f385503460f94c55017/html5/thumbnails/18.jpg)
18
Binding Time Trade-offs (3)
Late binding VM, dynamic linking and loading, overlaying, interpreting
- Code less efficient
- Checks must be done at run time
+ Flexible
+ Provides abstraction of hardware
+ Allows dynamic reconfiguration
![Page 19: CS 241 Spring 2007 System Programming 1 Introduction to Memory Management Lecture 28 Lawrence Angrave/Klara Nahrstedt](https://reader035.vdocument.in/reader035/viewer/2022062517/56649f385503460f94c55017/html5/thumbnails/19.jpg)
19
Storage Hierarchy
Cost
$400 a chip
$10-4 per byte
$10-8 per byte
$10-9 per byte
Size
213 bytes
227 bytes
230 bytes
240 bytes
CPU Reg
Cache
Memory
Secondary Storage
32-64 bits
4-128 words
512-16k words
![Page 20: CS 241 Spring 2007 System Programming 1 Introduction to Memory Management Lecture 28 Lawrence Angrave/Klara Nahrstedt](https://reader035.vdocument.in/reader035/viewer/2022062517/56649f385503460f94c55017/html5/thumbnails/20.jpg)
20
Single Programming
Overlay Manager
Overlay Area
Main Program
Overlay 1
Overlay 2
Overlay 3
Secondary Storage
Overlay 1Overlay 2Overlay 3Overlay 1
0K
5k
7k
12k
![Page 21: CS 241 Spring 2007 System Programming 1 Introduction to Memory Management Lecture 28 Lawrence Angrave/Klara Nahrstedt](https://reader035.vdocument.in/reader035/viewer/2022062517/56649f385503460f94c55017/html5/thumbnails/21.jpg)
21
User
Resident Monitor
Free
8k
32k
User
UserUser
Monitor
UserUserUser
0K
![Page 22: CS 241 Spring 2007 System Programming 1 Introduction to Memory Management Lecture 28 Lawrence Angrave/Klara Nahrstedt](https://reader035.vdocument.in/reader035/viewer/2022062517/56649f385503460f94c55017/html5/thumbnails/22.jpg)
22
Multiprogramming
How to keep multiple programs in memory?
![Page 23: CS 241 Spring 2007 System Programming 1 Introduction to Memory Management Lecture 28 Lawrence Angrave/Klara Nahrstedt](https://reader035.vdocument.in/reader035/viewer/2022062517/56649f385503460f94c55017/html5/thumbnails/23.jpg)
23
Multiprogramming with Fixed Partitions
Divide memory into n (possible unequal) partitions.
Problem:Fragmentation
Free Space
0k
4k
16k
64k
128k
![Page 24: CS 241 Spring 2007 System Programming 1 Introduction to Memory Management Lecture 28 Lawrence Angrave/Klara Nahrstedt](https://reader035.vdocument.in/reader035/viewer/2022062517/56649f385503460f94c55017/html5/thumbnails/24.jpg)
24
Fixed Partitions
Legend
Free Space0k
4k
16k
64k
128k
Internalfragmentation
(cannot be reallocated)
![Page 25: CS 241 Spring 2007 System Programming 1 Introduction to Memory Management Lecture 28 Lawrence Angrave/Klara Nahrstedt](https://reader035.vdocument.in/reader035/viewer/2022062517/56649f385503460f94c55017/html5/thumbnails/25.jpg)
25
Fixed Partitions
Legend
Free Space0k
4k
16k
64k
128k
![Page 26: CS 241 Spring 2007 System Programming 1 Introduction to Memory Management Lecture 28 Lawrence Angrave/Klara Nahrstedt](https://reader035.vdocument.in/reader035/viewer/2022062517/56649f385503460f94c55017/html5/thumbnails/26.jpg)
26
Fixed Partitions
Legend
Free Space0k
4k
16k
64k
128k
![Page 27: CS 241 Spring 2007 System Programming 1 Introduction to Memory Management Lecture 28 Lawrence Angrave/Klara Nahrstedt](https://reader035.vdocument.in/reader035/viewer/2022062517/56649f385503460f94c55017/html5/thumbnails/27.jpg)
27
Fixed Partitions
Legend
Free Space0k
4k
16k
64k
128k
![Page 28: CS 241 Spring 2007 System Programming 1 Introduction to Memory Management Lecture 28 Lawrence Angrave/Klara Nahrstedt](https://reader035.vdocument.in/reader035/viewer/2022062517/56649f385503460f94c55017/html5/thumbnails/28.jpg)
28
Fixed Partitions
Legend
Free Space0k
4k
16k
64k
128k
![Page 29: CS 241 Spring 2007 System Programming 1 Introduction to Memory Management Lecture 28 Lawrence Angrave/Klara Nahrstedt](https://reader035.vdocument.in/reader035/viewer/2022062517/56649f385503460f94c55017/html5/thumbnails/29.jpg)
29
Fixed Partitions
Legend
Free Space0k
4k
16k
64k
128k
![Page 30: CS 241 Spring 2007 System Programming 1 Introduction to Memory Management Lecture 28 Lawrence Angrave/Klara Nahrstedt](https://reader035.vdocument.in/reader035/viewer/2022062517/56649f385503460f94c55017/html5/thumbnails/30.jpg)
30
Fixed Partition Allocation Implementation Issues
Separate input queue for each partitionRequires sorting the incoming jobs and putting them
into separate queues
Inefficient utilization of memorywhen the queue for a large partition is empty but
the queue for a small partition is full. Small jobs have to wait to get into memory even though plenty of memory is free.
One single input queue for all partitions. Allocate a partition where the job fits in.
Best Fit
Available Fit
![Page 31: CS 241 Spring 2007 System Programming 1 Introduction to Memory Management Lecture 28 Lawrence Angrave/Klara Nahrstedt](https://reader035.vdocument.in/reader035/viewer/2022062517/56649f385503460f94c55017/html5/thumbnails/31.jpg)
31
Relocation
Correct starting address when a program should start in the memory
Different jobs will run at different addressesWhen a program is linked, the linker must know at what address the
program will begin in memory.
Logical addresses, Virtual addresses Logical address space , range (0 to max)
Physical addresses, Physical address spacerange (R+0 to R+max) for base value R.
User program never sees the real physical addresses
Memory-management unit (MMU)map virtual to physical addresses.
Relocation register Mapping requires hardware (MMU) with the base register
![Page 32: CS 241 Spring 2007 System Programming 1 Introduction to Memory Management Lecture 28 Lawrence Angrave/Klara Nahrstedt](https://reader035.vdocument.in/reader035/viewer/2022062517/56649f385503460f94c55017/html5/thumbnails/32.jpg)
32
Relocation Register
Memory
Base Register
CPU Instruction
Address
+
BA
MA MA+BA
PhysicalAddress
LogicalAddress
![Page 33: CS 241 Spring 2007 System Programming 1 Introduction to Memory Management Lecture 28 Lawrence Angrave/Klara Nahrstedt](https://reader035.vdocument.in/reader035/viewer/2022062517/56649f385503460f94c55017/html5/thumbnails/33.jpg)
33
Question 1 - Protection
Problem:How to prevent a malicious process to write or jump
into other user's or OS partitions
Solution:Base bounds registers
![Page 34: CS 241 Spring 2007 System Programming 1 Introduction to Memory Management Lecture 28 Lawrence Angrave/Klara Nahrstedt](https://reader035.vdocument.in/reader035/viewer/2022062517/56649f385503460f94c55017/html5/thumbnails/34.jpg)
34
Base Bounds Registers
Memory
Bounds Register Base Register
CPUAddress
< +MemoryAddress MA
LogicalAddress LA
Physical AddressPA
Fault
Base Address
Limit Address
MA+BA
BaseAddressBA
![Page 35: CS 241 Spring 2007 System Programming 1 Introduction to Memory Management Lecture 28 Lawrence Angrave/Klara Nahrstedt](https://reader035.vdocument.in/reader035/viewer/2022062517/56649f385503460f94c55017/html5/thumbnails/35.jpg)
35
Question 2
What if there are more processes than what could fit into the memory?
![Page 36: CS 241 Spring 2007 System Programming 1 Introduction to Memory Management Lecture 28 Lawrence Angrave/Klara Nahrstedt](https://reader035.vdocument.in/reader035/viewer/2022062517/56649f385503460f94c55017/html5/thumbnails/36.jpg)
36
Swapping
Monitor
Disk
UserPartition
![Page 37: CS 241 Spring 2007 System Programming 1 Introduction to Memory Management Lecture 28 Lawrence Angrave/Klara Nahrstedt](https://reader035.vdocument.in/reader035/viewer/2022062517/56649f385503460f94c55017/html5/thumbnails/37.jpg)
37
Swapping
Monitor
Disk
User 1
UserPartition
![Page 38: CS 241 Spring 2007 System Programming 1 Introduction to Memory Management Lecture 28 Lawrence Angrave/Klara Nahrstedt](https://reader035.vdocument.in/reader035/viewer/2022062517/56649f385503460f94c55017/html5/thumbnails/38.jpg)
38
Swapping
Monitor
User 1
Disk
User 1
UserPartition
![Page 39: CS 241 Spring 2007 System Programming 1 Introduction to Memory Management Lecture 28 Lawrence Angrave/Klara Nahrstedt](https://reader035.vdocument.in/reader035/viewer/2022062517/56649f385503460f94c55017/html5/thumbnails/39.jpg)
39
Swapping
Monitor
User 2
User 1
Disk
User 1
UserPartition
![Page 40: CS 241 Spring 2007 System Programming 1 Introduction to Memory Management Lecture 28 Lawrence Angrave/Klara Nahrstedt](https://reader035.vdocument.in/reader035/viewer/2022062517/56649f385503460f94c55017/html5/thumbnails/40.jpg)
40
Swapping
Monitor
Disk
User 2
User 2
UserPartition
User 1
![Page 41: CS 241 Spring 2007 System Programming 1 Introduction to Memory Management Lecture 28 Lawrence Angrave/Klara Nahrstedt](https://reader035.vdocument.in/reader035/viewer/2022062517/56649f385503460f94c55017/html5/thumbnails/41.jpg)
41
Swapping
Monitor
Disk
User 2
User 2
UserPartition
User 1
![Page 42: CS 241 Spring 2007 System Programming 1 Introduction to Memory Management Lecture 28 Lawrence Angrave/Klara Nahrstedt](https://reader035.vdocument.in/reader035/viewer/2022062517/56649f385503460f94c55017/html5/thumbnails/42.jpg)
42
Swapping
Monitor
Disk
User 1
User 2
UserPartition
User 1
![Page 43: CS 241 Spring 2007 System Programming 1 Introduction to Memory Management Lecture 28 Lawrence Angrave/Klara Nahrstedt](https://reader035.vdocument.in/reader035/viewer/2022062517/56649f385503460f94c55017/html5/thumbnails/43.jpg)
43
Summary
Concept of Names/Addresses during CompileLinkLoadRunning
Simple Programming Overlays
Relocation Register
Multi-Programming Fixed Partitions
Swapping