1
Sharing Main Memory,Segmentation,Simple Paging
ECE469, Mar 02
Aravind Machiry
2
Reading assignment
● Dinosaur Chapter 8
● Comet Chapter 13, 15, 16
3
Connecting the dots
main.c math.c
main.o math.o a.out
Load a.out to memManage mem for proc
Instructionexecution
compiler linker loader
memorymanagement
arch
4
OS
Segment 1
address 0
1. Simple uniprogramming:Single segment (code, data, stack heap) per process
Physical memory
5
OS
Segment 1
Segment 2
2. Simple multiprogramming:Single segment per process, static relocation
Static Relocation
foo() {..
main() {..
call N+0x30
0x30
0x20
0x0
N+ 0x30
N
Compiler View Loaded at N
OS Will relocate ONCE during the load time!
7
● Four drawbacks1. No protection2. Low utilization -- Cannot relocate dynamically
● Binary is fixed (after loading)● Cannot do anything about holes
3. No sharing -- Single segment per process● Cannot share part of process address space (e.g. text)
4. Entire address space needs to fit in mem● Need to swap whole, very expensive!
Simple multiprogramming:Single segment per process, static relocation
What else can we do?
● Already tried● Compile time / linking time● Loading time
● Let us try execution time!
8
9
3. Dynamic memory relocation
● Instead of changing the address of a program before it’s loaded, change the address dynamically during every reference● Under dynamic relocation, each
program-generated address (called a logical address or virtual address) is translated in hardware to a physical or real address
10
Translation overview
● Actual translation process is usually performed by hardware
● Translation table is set up by software
● CPU view● what program sees, virtual
addresses
● Memory view● physical memory addresses
Translation
CPU
virtual address
Physicalmemory
physical address
I/Odevice
Compilation and Linking is simplified
● Generate executable assuming load address is always 0x0
11
12
Base and bound
● OS can load at the executable starting at arbitrary physical address: base.
● A program can only access physical memory in [base, base+bound]
● On a context switch: save/restore base, bound registers
virtual address
base
bound
error
+
>
physical address
13
Base and bound
● The essence: ● A level of indirection
● Phy. Addr = Vir. Addr + basevirtual address
base
bound
error
+
>
physical address
14
Base and bound
● Pros: ● simple, fast translation,
cheap● Can relocate segment: just
change the base!!● Protects process from
corrupting each other:● How? (Hint: error)
virtual address
base
bound
error
+
>
physical address
15
Base and bound
● Cons:● Only one segment per
process
● How can two processes share code while keeping private data areas (shared editors)?● Can it be done safely with
a single-segment scheme?
virtual address
base
bound
error
+
>
physical address
16
● Four drawbacks1. No protection2. Low utilization -- Cannot relocate dynamically
● Binary is fixed (after loading)● Cannot do anything about holes
3. No sharing -- Single segment per process● Cannot share part of process address space (e.g. text)
4. Entire address space needs to fit in mem● Need to swap whole, very expensive!
What have we solved?
17
Multiple Segments
● Split the process address space into multiple segments each of same size.
● Example:14-bit address space, i.e., 0x0000 - 0x3FFF (16 KB): 4 Segments of 4K bytes each.● 0x0000 - 0x0FFF● 0x1000 - 0x1FFF● 0x2000 - 0x2FFF● 0x3000 - 0x3FFF
14-bit address: Top 2-bits indicate segment number. Rest 12-bits indicate offset with in the segment.
18
Multiple Segments
● Have a table of (seg, size)
● Further protection: each entry has(nil, read, write, exec)
● On a context switch: save/restore the table (or a pointer to the table) in kernel memory physical address
+
segment offset
Virtual address
Seg base size
...
>
errorYes
No
19
Segmentation example text segment [0x0000, 0x04B0] foo: bar procedure 019A: LD R1, 15DC 0320: bar: 01C2: jmp 01F4 01E0: call 0320 Data segment [0x1000, 0x16A0] 01F4: X: 15DC: _Y:
2-bit segment number, 12-bit offset Segment Base Bounds RW 0 4000 4B0 10 ← Code: Read Only
1 0 6A0 11 ← Date: Read Write
2 3000 FFF 11 ← Heap: Read Write
3 -- -- 00 ← Unused
20
Segmentation example text segment [0x0000, 0x04B0] foo: bar procedure 019A: LD R1, 15DC 0320: bar: 01C2: jmp 01F4 01E0: call 0320 Data segment [0x1000, 0x16A0] 01F4: X: 15DC: _Y:
2-bit segment number, 12-bit offset Segment Base Bounds RW 0 4000 4B0 10 -> 4000 + 1F4 = 41F4 1 0 6A0 11 2 3000 FFF 11 3 -- -- 00
🡺 Where is 01F4 in physical memory?
21
Segmentation example text segment [0x0000, 0x04B0] foo: bar procedure 019A: LD R1, 15DC 0320: bar: 01C2: jmp 01F4 01E0: call 0320 Data segment [0x1000, 0x16A0] 01F4: X: 15DC: _Y:
2-bit segment number, 12-bit offset Segment Base Bounds RW 0 4000 4B0 10 1 0000 6A0 11 -> 0000 + 5DC = 05DC 2 3000 FFF 11 3 -- -- 00
🡺 Where is 15DC in physical memory?
How does this allow two processes to share code segment?
22
Seg Base Bounds RW
0 4000 4B0 10
1 0000 6A0 11
2 3000 FFF 11
3 -- -- 00
Seg Base Bounds RW
0 5000 1C0 11
1 -- -- 00
2 4000 4B0 10
3 -- -- 00
Process A Process B
● Process A and B have their different segments map to the same physical segment i.e., One copy of physical code segment (0x4000-0x44B0) shared between 2 processes.
23
Pros/cons of segmentation
● Pros: ● Process can be split among several segments
● Allows sharing● Segments can be assigned, moved, or swapped
independently
● Cons:● External fragmentation: many holes in physical
memory● Also happens in base and bound scheme
24
OS
Segment 2
Simple multiprogramming:Single segment per process, static relocation
Segment 3
Segment 4?
External fragmentation
25
What fundamentally causes external fragmentation?
1. Segments of many different sizes
2. Each has to be allocated contiguously