Virtual Memory Flashcards

1
Q

What is the goal of Virtual Memory?

A

to allow running processes with
memory requirements larger than available RAM to
run in the computer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How does Virtual Memory work today? (very high level)

A

gives each
process its own address space.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How does Virtual Memory store memory?

A

VM only keeps in RAM the memory that is
currently in use

The remaining memory is kept in disk in a
special file called “swap space”

This also specifically works through pages

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Who created the idea of Virtual Memory?

A

Peter Denning a former head of the CS Department at Purdue

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How does VM speed up OS tasks?

A

● Loading a program. The VM will load pages of the program as
they are needed, instead of loading the program all at once.

● During fork the child gets a copy of the memory of the
parent. However, parent and child will use the same memory as
long as it is not modified, making the fork call faster. This is called
“copy-on-write”.

● Shared Libraries across processes.

● Shared memory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is VM Process Swapping?

A

The entire memory of the process is swapped in and out
of memory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is VM Segment Swapping?

A

Entire parts of the program (process) are swapped in
and out of memory (libraries, text, data, bss, etc.

Problems of process swapping and segment swapping
is that the granularity was too big and some pieces still
in use could be swapped out together with the pieces
that were not in use

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is Paging in VM?

A

Process of dividing memory into fixed size, used in modern day OS

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the size of a X86 + ARM page vs. a Sparc
Ultra Architecture/Alpha page?

A

x86+ARM usually 4KB, Spark usually 8Kb

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a Backing Store?

A

A file in disk that “backs” the page (maybe research more)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the Swap Space?

A

A designated area in disk that is used by the VM system to store transient data. Generally data that will go away when the process exits goes to swap space.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are specific examples of sections in memory stored in swap space typically?

A

Stack, Heap, BSS, modified data

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does the Memory Management Unit do? (MMU)

A

Translates Virtual Memory Addresses (vmaddr) to physical
memory addresses (phaddr) using a page table.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the two different types of addresses?

A

Virtual Memory Addresses: the address that the
CPU is using. Addresses used by programs are of
this type.

Physical Memory Addresses: The addresses of
RAM pages. This is the hardware address.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Does the cpu interact with virtual or physical addresses?

A

virtual

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are physical addresses pointing to?

A

pages in RAM

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is the page table register?

A

It points to the current page table that will be used for the translation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

How are page tables split up?

A

By process, each has their own.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

When is the page table register updated?

A

During a context switch from one process to another.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What does a page table hold?

A

the information of the memory
ranges that are valid in a process

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Do consecutive pages in virtual memory correspond to consecutive pages in physical memory.

A

No

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

How does the MMU prevent looking up the page table at every memory access?

A

the most recent translations
are stored in the Translation Look-Aside
buffer (TLB).

23
Q

What is the Translation Look Aside buffer?

A

The TLB speeds up the translation from
virtual to physical memory addresses by storing the most recent translations.

24
Q

What is a page fault interrupt generated by?

A

The MMU

25
Q

What are the different parts of the Virtual Memory address (One level page table)?

A

20 highest bits for the page number.
Lower 12 bits for the offset (Last 3hex digits)

26
Q

Is the offset of a page translated?

A

No, just the page number.

27
Q

For the VM address 0x2345, what is the offset?

A

345 is the offset, it is preserved in the physical address, so the physical address could be something like: 0x767345

28
Q

Why do we use a two level page table?

A

A one level page table requires too much memory. Modern systems use multiple levels

29
Q

How is a two level page table VM address divided?

A

10 bits for first level index
10 bits for second level index
12 bits still for offset

30
Q

How many bits are needed to store the excess characteristics of a page?

A

12

31
Q

What are some of the characteristics saved for each page?

A

● Resident Bit:
● Page is resident in RAM instead of swap space/file.
● Modified Bit:

Page has been modified since the last time the bit was cleared.
Set by the MMU.

● Access Bit:
● Page has been read since the last time the bit was cleared. Set
by MMU
● Permission:

Read 🡪 page is readable
Write 🡪 Page is writable
Execute 🡪 Page can be executed (MMU enforces permissions)

32
Q

What happens when a CPU operation exceeds the permissions of a page? (ex: writing to a read-only page)

A

the MMU will generate an interrupt (page
fault). The interrupt may be translated into a signal (SEGV, SIGBUS) to the process.

33
Q

What happens when a page is trying to be accessed but it is not resident in RAM?

A

MMU generates an
interrupt to the kernel and the kernel loads that page from disk.

34
Q

What are the two different types of page faults we learned about?

A

● Page not Resident: Page not in Physical Memory, it is in disk
● Protection Violation: Write, Read/Execute permissions
(as indicated by page bits) are violated.

35
Q

What is a Page not Resident, page fault?

A

When the page is not in physical memory, it is in disk. Requires an interrupt to reach.

36
Q

What is a Protection Violation, page fault?

A

Write, Read/Execute permissions
(as indicated by page bits) are violated.

37
Q

How does a page fault get processed?

A
  1. A program tries to read/write a location in memory that is in a non-resident page. This could happen when:
    * fetching the next instruction to execute or
    * trying to read/write memory not resident in RAM
  2. The MMU tries to look up the VM address and finds that the page is not resident using the resident bit. Then the MMU generates a page fault, that is an interrupt from the MMU
  3. Save return address and registers in the stack
  4. The CPU looks up the interrupt handler that corresponds to the page fault in the interrupt vector and jumps to this interrupt handler
  5. In the page fault handler
    If the VM address corresponds to a page that is not valid for this process, then generate a SEGV signal to the process. The default behavior for SEGV is to
    kill the process and dump core
    Otherwise, if VM address is in a valid page, then the page has to be loaded from disk.
  6. Find a free page in physical memory. If there are no free pages, then use one that has not been used recently and write to disk if modified
  7. Load the page from disk and update the page table with the address of the page replaced. Also, clear the modified and access bits. Set resident bit to 1.
  8. Restore registers, return and retry the offending instruction.
38
Q

What does “the page fault is completely transparent” mean?

A

The page fault is completely transparent to the program, that is, the program will have no knowledge that the page fault occurred.

39
Q

What does the mmapp function do?

A

The mmap() function establishes a mapping between a process’s address space and a file or shared memory
object.

40
Q

void *mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off);

What does the input addr refer to?

A

addr –
Suggested address. If NULL is passed the OS will choose the address of the mapping.

41
Q

void *mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off);

What does the input Len refer to?

A

len –
Length of the memory mapping. The mmaped file should have
this length or larger or the program gets SEGV on access.

42
Q

void *mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off);

What does the input prot refer to?

A

Prot –
Protections of the mapping: PROT_READ, PROT_WRITE, PROT_EXEC, PROT_NONE. –

43
Q

void *mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off);

What does the input flags refer to?

A

flags: - Semantics of the mapping:
MAP_SHARED – Changes in memory will be done in the file
MAP_PRIVATE – Changes in memory will be kept private to the process

and will not be reflected in the file. This is called “copy-on-
write”

MAP_FIXED – Force to use “addr” as is without changing. You should
know what you are doing since the memory may be already in use.
Used by loaders
MAP_NORESERVE– Do not reserve swap space in
advance. Allocate swap space as needed.
MAP_ANON – Anonimous mapping. Do not use any fd (file).
Use swap as the backing store. This option
is used to allocate memory

44
Q

void *mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off);

What does the input Fd refer to?

A

The file descriptor of the file that will be memory mapped. You need to call open() to open the file and get the fd. Pass –1 if MAP_ANON is used.

45
Q

void *mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off);

What does the input offset refer to?

A

Offset in the file where the mapping will start. It has to be a multiple of a page size.

46
Q

When using mmap, does the file in disk get updated immediately?

A

No, ● The OS will “cache” the change until it is necessary to flush the changes.
● When the file is closed
● Periodically (every 30secs or so)
● When the command “sync” is typed

47
Q

What happens when you try to read the value from the file of a page that has not been flushed to disk,

A

the OS will give
you the most recent value from the memory instead of the disk.

48
Q

Asides from preventing running out of memory, what is VM used for?

A

give each process a
separate Virtual Memory Address space.

49
Q

How does VM speed up the execution of programs?

A
  1. Mmap the text segment of an executable or shared
    library
  2. Mmap the data segment of a program
  3. Use of VM during fork to copy memory of the parent into
    the child
  4. Allocate zero-initialized memory. it is used to allocate space
    for bss, stack and sbrk()
  5. Shared Memory
50
Q

What are some of the things that happen during an Mmap the text segment of
an executable or a shared
library

A

● initially mmap does not read any pages
● any pages will be loaded on demand when they are
accessed
● startup time is fast because only the pages needed
will be loaded instead of the entire program
● It also saves RAM because only the portions of the
program that are needed will be in RAM
● Physical pages where the text segment is
stored is shared by multiple instances of the
same program.
● Protections: PROT_READ|PROT_EXEC
● Flags: MAP_PRIVATE

51
Q

What happens when you Mmap the data segment of a program?

A

● During the loading of a program, the OS mmaps the
data segment of the program
● The data segment contains initialized global
variables.
● Multiple instances of the same program will share
the same physical memory pages where the data
segment is mapped as long as the page is not
modified
● If a page is modified, the OS will create a copy of
the page and make the change in the copy. This is
called “copy on write”

52
Q

What are some specific things that happen after the Use of VM during fork to copy memory of the parent into the child

A

● After forking, the child gets a copy of the memory of
the parent
● The page table of the parent is copied into the child
page table, and both parent and child share the
same RAM pages (physical memory) as long as
they are not modified
● When a page is modified by either parent or child,
the OS will create a copy of the page in RAM and
will do the modifications on the copy
● The copy on write in fork is accomplished by
making the common pages read-only.
● The OS will catch the modifications. During
the page fault will create a copy and update
the page table of the writing process.
● Then it will retry the modify instruction.

53
Q

How does Allocate zero-initialized
memory work in VM?.

A

● It is used to allocate space for bss, stack and sbrk() for
the heap.
● When allocating memory using sbrk or mmap with the
MMAP_ANON flag, all the VM pages in this
mapping will map to a single page in RAM that has
zeroes and that is read only.
● When a page is modified the OS creates a copy of the
page (copy on write) and retries the modifying
instruction
● This allows fast allocation. No RAM is initialized to
O’s until the page is modified
● This also saves RAM. only modified pages use RAM.
● This is implemented by making the entries in the
same page table point to a page with 0s and making
the pages read only.
● An instruction that tries to modify the page will get a
page fault.
● The page fault allocates another physical page with
0’s and updates the page table to point to it.
● The instruction is retried and the program continues
as it never happened.

54
Q

How does shared memory work?

A

● Processes may communicate using shared memory
● Both processes share the same physical
pages
● A modification in one page by one process will be reflected by a change in the same page in the other process.