Docsity

Virtual Memory and Address Spaces: Uni-programming vs. Multi-programming - Prof. Peter M. , Study notes of Electrical and Electronics Engineering

Toggle sidebar
bg1
EECS 482 1 Peter M. Chen
Address spaces and memory management
Review of processes
• process = one or more threads in an address space
• thread: stream of execution
unit of concurrency
• address space: memory space that threads use
unit of data
Address space abstraction
• address space: all the data the process can use as it runs.
Includes program code, stack, data segment
• hardware interface (physical reality): one memory of
small size, shared between processes
• application interface (abstraction provided by OS): each
process has its own memory, as large as the virtual
address space
Illusions provided by address spaces
• address independence: same numeric address can be used
in different address spaces (i.e. different processes), yet
remain logically distinct
• virtual memory: an address space can be larger than the
amount of physical memory on the machine
• protection: one address spaces can’t access data in
another address space (actually controlled sharing)
Uni-programming
1 process runs at a time (viz. one process occupies memory at
a time)
Always load process into the same spot in memory (and
reserve some space for the OS)
fffff (high memory)
.
. operating system
.
80000
7ffff
.
. user process
.
00000 (low memory)
Achieves address independence by always loading process into
same physical memory location
Problems with uni-programming?
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download Virtual Memory and Address Spaces: Uni-programming vs. Multi-programming - Prof. Peter M. and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

EECS 482

1

Addr

ess spaces and memory management

Review of processes

  • process = one or more threads in an address space• thread: stream of execution

unit of concurrency

  • address space: memory space that threads use

unit of data

Address space abstraction

  • address space: all the data the process can use as it runs.

Includes program code, stack, data segment

  • hardware interface (physical reality): one memory of

small size, shared between processes

  • application interface (abstraction provided by OS): each

process has its own memory, as large as the virtualaddress space

Illusions provided by address spaces

  • address independence: same numeric address can be used

in different address spaces (i.e. different processes), yetremain logically distinct

  • virtual memory: an address space can be larger than the

amount of physical memory on the machine

  • protection: one address spaces can’t access data in

another address space (actually controlled sharing)

Uni-pr

ogramming

1 process runs at a time (viz. one process occupies memory at

a time)

Always load process into the same spot in memory (and

reserve some space for the OS) fffff (high memory)

..

operating

system

. 80000 7ffff

..

user process

. 00000 (low memory)

Achieves address independence by always loading process into

same physical memory location

Problems with uni-programming?

EECS 482

2

Multi-pr

ogramming and addr

ess translation

Multi-programming: more than 1 process is in memory at a

time• need to support address translation• need to support protection

Must translate addresses issued by a process so they don’t con-

flict with addresses issued by other processes• static address translation: translate addresses before exe-

cution (translation remains constant during execution)

  • dynamic address translation: translate addresses during

execution (translation may change during execution)

Is it possible to run two processes at the same time (both are in

memory) and provide address independence with onlystatic address translation?

Does this achieve the other address space abstractions? Achieving all the address space abstractions requires doing

some work on every memory reference

EECS 482

4

Base & bounds

Load each process into contiguous regions of physical mem-

ory, prevent each process from accessing data outside itsregion if

(virtual address > bound) {kill

process (core dump)

}

else

{

physical address = virtual address + base }

Process has illusion of running on its own dedicated machine,

with memory [0, bound)

This is similar to linker-loader, but also protect processes from

each other.

As with all translation data, only kernel can change base and

bounds

During context switch, must change all translation data (base

and bounds registers)

What to do when address space grows?Low hardware cost (2 registers, adder, comparator), low over-

head (add and compare on each memory reference)

virtualmemory 0

bound

physicalmemory 0

physical memory size

base + bound

base

EECS 482

5

Peter M. Chen

Hard for a single address space to be larger than physical

memory

But sum of all address spaces can be larger than physical mem-

ory• swap an entire address space out to disk, swap address

space for new process in

Can’t share part of an address space between processes

External fragmentation

  • processes come and go, leaving a mishmash of available

memory regions process 1 start:

KB

(phys.

mem.

0-

KB)

process 2 start:200 KB (phys. mem. 100-299 KB)process 3 start:300 KB (phys. mem. 300-599 KB)process 4 start:400 KB (phys. mem. 600-999 KB)process 3 exits

(frees

phys.

mem.

300-

KB)

process 5 start:100 KB (phys. mem. 300-399 KB)process 1 exits

(frees

phys.

mem.

0-

KB)

process 6 start:

KB

300 KB are

free

(400-

KB;

0-

KB),

but

not contiguous

  • this is called “external fragmentation”: wasted memory

between allocated regions. Can waste lots of memory.

Allocation strategies to minimize external fragmentation

  • best fit: allocate the smallest memory region that can sat-

isfy the request (least amount of wasted space)

  • first fit: allocate the memory region that you find first that

can satisfy the request

  • in worst case, must re-allocate existing memory regions

(by copying them to another area)

virtualmemory

physicalmemory 0

data code code

data (P1)code

virtual address(process 1)

virtualmemorydata code^ code virtual address(process 2)

data (P2)

EECS 482

7

Segmentation

Segment: a region of contiguous memoryBase & bounds used a single segment. Let’s generalize this to

allow multiple segments, described by a table of base &bound pairs.

In segmentation, a virtual address takes the form:

(virtual segment #, offset)• could specify virtual segment # via the high bits of the

segment # address, or via a special register, or implicit to theinstruction opcode

base

bound

description

0

4000

700

code segment

1

0

500

data segment

2

unused

3

2000

1000

stack segment

virtualmemory 0 4ff

physicalmemory

2fff 2000

virtualmemory segment 1

fff^0

segment 3

stack

stack

data

4ff

data

virtualmemory 0 6ff

segment 0

code

code

46ff 4000

EECS 482

8

Note that not all virtual addresses are

valid

  • e.g. no valid data in segment 2; no valid data in segment 1

above 4ff

  • valid means the region is part of the process’s virtual

address space. Invalid means this virtual address is ille-gal for the process to access (and will cause a core dumpif accessed).

  • possible to deliberately allow invalid addresses to auto-

matically extend the address space (e.g. in Unix, access-ing invalid stack address right above stack bound willtrap to the kernel and automatically increase the stacksize).

Protection: different segments can have different protection

  • e.g. code can be read-only (allows instruction fetch, load)• e.g. data is read/write (allows fetch, load, store)• in contrast, base&bounds gives same protection to entire

address space

What must be changed on a context switch?

Pros and cons

  • works well for sparse address spaces (with big gaps ofinvalid areas)+ easy to share whole segments without sharing entireaddress space- complex memory allocation

Can a single address space be larger than physical memory?How to make memory allocation easy and allow an address

space easily be larger than physical memory?

EECS 482

10

How does processor know that virtual page is not in physical

memory?

Like segments, pages can have different protections

  • e.g. read, write, execute

V

alid vs. r

esident

Resident means a virtual page is in memory. It is NOT an error

for a program to access a non-resident page

Valid means a virtual page is not currently legal for the pro-

gram to access

Who makes a virtual page resident/non-resident?Who makes a virtual page valid/invalid?Why would a process want one of its virtual pages to be

invalid?

EECS 482

11

P

age size

What happens if page size is small?What happens if page size is really big?Could we use a large page size but let other processes use the

leftover space in the page?

Page size is typically a compromise, e.g. 4 KB or 8 KB

Fixed vs. variable size partitions

  • fixed size (pages) must be compromise (e.g. 4 or 8 KB).

Too small a size leads to a large translation table, whiletoo large a size leads to internal fragmentation

  • variable size (segments) can adapt to the need, but it’s

hard to pack these variable size partitions into physicalmemory (leading to external fragmentation)

What happens to paging if the virtual address space is sparse

(most of the address space is invalid, with scattered validregions)?

Paging pros and cons

  • simple memory allocation+ can share lots of small pieces of an address space+ easy to grow the address space. Simply add a virtualpage to the page table, and find a free physical page to holdthe virtual page before accessing it.- big page tables

EECS 482

13

This is a two-level tree How does this allow the translation data to take less space?

How to use share memory when using multi-level page tables?What must be changed on a context switch?Another alternative: use segments in place of the level-1 page

table. This uses pages on level 2 (i.e. break each segmentinto pages)

level

1

0

1

3

level

2

page

table

page

tables

virtualaddressbits 21-

12

physicalpage #

0

10

1

15

2

20

3

2

virtualaddressbits 21-

12

physicalpage #

0

30

1

4

2

8

3

3

NULL

2 NULL

EECS 482

14

Pros and cons

  • space-efficient for sparse address spaces+ easy memory allocation+ lots of ways to share memory- two extra lookups per memory reference

T

ranslation lookaside b

uffer (TLB)

Translation when using paging involves 1 or more additional

memory references. How to speed up the translation pro-cess?

TLB caches translation from virtual page # to physical page #

(TLB conceptually caches the entire page table entry, e.g.dirty bit, reference bit, protection)

If TLB contains the entry you’re looking for, can skip all the

translation steps above

On TLB miss, figure out the translation by getting the user’s

page table entry, store in the TLB, then restart the instruc-tion.

Does this change what happens on a context switch?

EECS 482

16

Clock

Most MMUs maintain “referenced” bit for each resident page,

which is set automatically when the page is referenced.Reference bit can be cleared by OS.

Why is hardware support needed to maintain the reference bit?How can you identify an “old” page?Try to do this work incrementally (rather than all at once)

To find a page to evict:

  • look at page being pointed to by clock hand• reference=0 means page hasn’t been accessed in a long

time (since last sweep), so this is your victim.

  • reference=1 means page has been accessed since your

last sweep. What to do?

Can this infinite loop? What if it finds all pages referenced

since the last sweep?

New pages are put behind the clock hand, with reference=

A

B

F

C

E

D

EECS 482

17

P

ageout

What to do with page when it’s evicted? Why not write pages to disk on every store?

While evicted page is being written to disk, the page being

brought into memory must wait• may be able to reduce total work by giving preference to

dirty pages (e.g. could evict clean pages before dirtypages)

  • if system is idle, might spend time profitably by writing

back dirty pages

EECS 482

19

K

er

nel vs. user mode

Who sets up the data used by translator?Kernel is allowed to modify any memory (including translation

tables)

How can kernel refer to translation table? Translation table is

not really in any process’s address space. It is often inphysical (i.e. untranslated) memory.• kernel can issue untranslated addresses (i.e. bypass the

translator)

  • kernel can map physical memory into a portion of its

address space

How does machine know that the kernel is running?

  • machine must know to allow kernel to bypass translator,

and to allow kernel to execute privileged instructions(e.g. halt, I/O)

  • need hardware support: two processor modes (kernel and

user)

How have we handled the problem of protection so far?

  • implement protection by translating all addresses. But

who can modify data used by translator?

  • only kernel can modify translator’s data, but how does

processor know if kernel is running?

  • mode bit distinguishes between kernel and user. But who

is allowed to modify mode bit?

EECS 482

20

Switching fr

om user pr

ocess into k

er

nel

What causes a switch from a user process into the kernel?

Sequence of events that take place when C++ program calls

cin• C++ code calls cin• cin is a standard library function that calls read()• read() is a standard library function that executes the

assembly-language instruction “syscall”, with parame-ters (SYS_read, file number, size) in registers or on thestack

-^

when processor executes “syscall” instruction, it trapsto the kernel at a pre-specified location

  • kernel syscall handler receives the trap, and calls the ker-

nel’s read() function

Details of what happens when trapping to kernel

  • set processor mode bit to kernel• save current registers (SP, PC, general purpose registers)• set SP to the kernel’s stack• change address spaces to the kernel’s address space (by

changing some data used by the translator)

  • jump to kernel exception handler

Does this look familiar?How does processor know exception handler’s address?