Memory Virtualization: Memory Management Flashcards

1
Q

Why do we virtualize memory?

A

Virtual memory serves two purposes: - First, it allows us to extend the use of physical memory by using disk. - Second, it allows us to have memory protection, because each virtual address is translated to a physical address.

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

What does virtual address space contain?

A

Program Code (Text Segment): Contains the program’s executable instructions. It’s read-only to prevent accidental changes.

Heap: Used for dynamically allocated memory during runtime, like when using malloc() in C. It expands as more memory is needed.

Stack: Stores local variables and manages function calls. Operates in a last-in, first-out manner and shrinks or grows in the opposite direction of the heap.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Why do we use virtual address space?

A

To be able to locate where in physical memory the data is stored. It is translated from Virtual Address to Physical Address

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

What component does the translation of virtual to physical address?

A

MMU (memory-managment unit)

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

What is the goal of virtualization?

A

Transparency: user programs should not be aware of the messy details-

Efficiency: minimize overhead and wastage in term of memory space and access time-

Isolation and protection: A user process should not be able to access anything outside its address space

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

Does the OS have its own address space? If not, where is it?

A

OS is not separate process with its own address space- Instead, OS code is part of the address space of every process- A process sees OS as part of its code (library)- Page table map the OS addresses to OS code

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

What does the stack contain? What does the heap contain?

A

The Stack:

Contains local variables, function parameters, return addresses, and control data.
Automatically adjusts in size as functions are called and completed.

The Heap:

Used for memory that is allocated and freed during program run time, like with malloc in C.
Stores global data and large data structures.
Its size is managed manually by the programmer
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does malloc() do? What does free() do?

A

malloc(): Allocates a specified amount of memory during a program’s run time.

free(): Releases previously allocated memory back to the system.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the procedures during address translation?

A

Determine Address Space and Page Size:
Address space: 64 bytes.
Page size: 16 bytes.
Total pages: 64 bytes / 16 bytes/page = 4 pages.

Split Virtual Address into VPN and Offset (Example: 21):
    Convert to binary: 21 is "01 0101".
    Virtual Page Number (VPN): First 2 bits (01).
    Offset: Last 4 bits (0101).

Translate VPN to Physical Frame Number (PFN):
    Use page table. Example: VPN 1 maps to PFN 7.

Combine PFN and Offset for Physical Address:
    PFN 7 in binary is 111.
    Combine with offset: "111 0101".
    Convert to decimal: Physical address is 117.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the role of OS in the translation?

A

Maintains Free List of Memory: Tracking unallocated memory.

Allocates Space During Process Creation; Cleans Up When Done: Allocating and deallocating memory for processes.

Maintains Information in PCB: Storing information about allocated space in the Process Control Block.

Sets Address Translation Information in Hardware: This is similar to managing page tables and TLB.

Updates Information Upon Context Switch: Adjusting memory management structures when switching between processes.

Handles Traps Due to Illegal Memory Access: Part of memory protection and similar to handling page faults.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the role of hardware in address translation?

A

The CPU provides privileged mode of execution, this makes it so the instructions set has priviliged instructions to set translation information (base and bounds).

Then the Hardware(MMU) uses this information to perform translation on every memory access (instruction fetch, load or store)If the translation is illegal, the MMU generates traps to OS(eg, VA is out of bounds)

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

What is the formula for physical address?

A

PA = base register + VA

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

What are the problems with base and bounds?

A

If stack and heap are small, space between them is wasted. This is called internal fragmentation.This is caused by fixed size slots (due to our assumptions as same size address space). - Requires a big chunk of free space in the middle

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

How does the virtual address know which segment contains what (segmentation storing)?

A

Using the two first bits”00” = Code”01” = Heap”10” = Stack”11” = Unused

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

How can processes share memory (segmentation)?

A

Protection in segmentation includes extra bits per segment for read, write, and execute permissions. Processes perceive their memory as private, while the OS may share it secretly. The hardware checks if access is permissible.

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

Why do we use segmentation?

A

Unused space between stack and heap need not be allocated in physical memory- Allowing for more address spaces in physical memory we can use segmentation

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

What are the problems with free space management within segmentation?

A

Physical memory becomes full of holes of free space (external fragmentation)

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

What is external fragmentation?

A

Physical memory becomes full of holes of free space (external fragmentation)

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

What is a solution to external fragmentation within segmentation?

A

One solution to manage memory is to compact physical memory by rearranging segments. This involves stopping processes, moving their segments, and updating segment registers to new locations, which is memory-intensive and time-consuming.

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

Is there a better approach to avoid external fragmentation with segmentation?

A

Use a free list management algorithm, like the best fit algorithm, which maintains a list of free memory spaces and allocates the chunk closest in size to the requested amount.

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

What are the benefits of segmentation?

A

Support sparse address spaces (address spaces with a lot of empty spaces)- Avoid wasting memory between logical segments of an address space- Fast translation- Code sharing- No internal fragmentation

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

What are the problems with segmentation?

A

External fragmentation occurs in segmentation, leaving odd-sized free memory pieces.
This fragmentation issue doesn’t happen with paging.
Memory allocation becomes more difficult due to fragmentation.
It’s fundamentally challenging to avoid external fragmentation in segmentation.
Segmentation lacks flexibility for sparse address spaces.
If the heap is sparsely used, the entire heap must be retained in memory.

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

How to manage free space, when satisfying variable-sized requests?

A

Splitting: Divide larger memory segments to fit the requested size.
Coalescing: Merge adjacent free chunks into larger blocks.
Fragmentation Management: Combine smaller free spaces to fulfill larger requests.
Size Tracking: Monitor the sizes of allocated and free memory regions.
Free List: Use a list to efficiently track and allocate free memory segments.

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

What is splitting? What is coalescing?

A

Example, trying to allocate 1 bytes when we have two 10 bytes segment available:- Find a free chunk that satisfy the request and split it into two- Return the first chunk to the caller (1 byte)Second chunk remain on the list (9 bytes)

It allows us to combine free segment next to each other in memory

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

Why would we need coalescing?

A

It occur when we for example have three free segment next to each other with size 5. If we try to allocate 7 bytes it will fail. That’s why we coalesce them to get 15 byte available in one segment

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

In free space management, how do we track the size of allocated regions?

A

In free space management, the size of allocated regions is tracked by creating a header field in each allocation containing size information and sometimes pointers for quicker freeing.

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

What are some of the strategies to manage free space?

A

Best Fit: Searches for the smallest available chunk that meets the request size. This method can be performance-heavy.
Worst Fit: Finds the largest available chunk, aiming to leave larger chunks free, which can prevent fragmentation.
First Fit: Chooses the first chunk that is large enough. It’s fast but can lead to small, unusable spaces at the start of the free list.
Next Fit: Similar to First Fit but starts searching from the last allocation point, not the beginning, making it more efficient in some cases.

28
Q

What is buddy allocation?

A

To allocate 7 bytes from 64KB of free space, repeatedly halve the space until the chunks are just larger than the request. Begin with 64KB, then split into 32KB, 16KB, and finally 8KB chunks. Select the 8KB chunk for the allocation, as it’s the smallest size that can accommodate the request. Any remaining, unneeded smaller chunks are then merged back together through coalescing.

Basically. split memory into chunks, and when they are small enough and the request is met, use that. The rest gets coalesced

29
Q

How does paging differ from segmentation?

A

Instead of allocation of variable sized chunks we use fixed size chunks

30
Q

What is a page table? What does a page table store?

A

Page table is a per process datastructure to help with address translation Keeps track of where each virtual page is in memory.

Stores address translations for each virtual page of the address space.Basically mappings from virtual page number to physical frame number

31
Q

What does a page table entry (PTE) contain?

A

PFN (Physical Frame Number): Identifies the physical memory location.
Valid Bit: Indicates whether the page is being used by the process.
Protection Bit: Specifies read/write permissions for the page.
Present Bit: Shows if the page is currently in physical memory.
Dirty Bit: Marks if the page has been modified.
Accessed Bit: Tracks whether the page has been accessed recently.

32
Q

Why is paging slow?

A

Paging requires perform ONE extra memory acccess

33
Q

How do we determine the size of VPN?

A

Formula: 2^xExample: - 64 bit = 2^6 - So we need 6 bits- If the page size is 16 bytes we need to be able to access 4 pages. Therefore we need 2 bits in the VPN and the rest 4 bytes will be the offset

34
Q

How to we calculate VPN and offset if we have a virtual address 21?

A

21 in binary: 010101VPN : 01Offset: 0101

35
Q

Explain how a page table is used.

A

The OS uses the Virtual Page Number (VPN) to index an array and find the Page Table Entry (PTE), which provides the Physical Frame Number (PFN).

36
Q

Why do we use TLB’s?

A

To speed up translation

36
Q

What does the Translation Lookaside Buffer (TLB) contain?

A

-A cache of recent VA-PA mappings

37
Q

How does the TLB work?

A

HW checks if desired virtual address to physical address mapping is in TLB (cache)- If so (tlb hit), use TLB translation (without consulting the page table in memory)- Else tlb miss, consult page table (which has all the translation

38
Q

What are the problems with TLB’s?

A

The TLB (Translation Lookaside Buffer) keeps a limited number of translation entries. When switching between processes, we must ensure that the new process doesn’t use the old process’s translations by mistake. The TLB can’t tell which entry belongs to which process, so we need to manage it carefully during process switches to prevent errors.

39
Q

What are some of the TLB policies we can do to ensure an up-to-date TLB?

A

Two approaches:- Evict entries at random - Evict least recently used (minst brukt)

40
Q

Why can’t we just increase the page size to reduce the size of a page table?

A

Leads to waste within each page (internal fragmentation)- Therefore most systems use relatively small pages- So our problem will not be solved that easily

41
Q

How can we solve the problem with page tables being too big?

A

Combine page table and segmentation- Multi level page table- Inverted page table (only open pagetable with all the physical addresses)

42
Q

What are the problems of using segmentation and page table (hybrid)?

A

External fragmentation- Not flexible- If we use large page table we might still get a lot of waste

43
Q

What is multi-level page table?

A

Multilevel page tables are like having multiple maps stacked on top of each other to find a location. Here’s a simple breakdown:

When an application wants to find a memory location, it looks at the first map (pagetable).
The first 10 bits of the location tell us which page in the second map to look at.
In the second map, the next 10 bits tell us the exact spot in physical memory.
The remaining bits are like following a sign to find the exact item (offset).

So, it’s like using multiple maps, one inside the other, to find the right place in memory.

44
Q

What are the advantages and disadvantages of multi-level page table?

A

Advantages:

(a) Page number lookups are faster.
(b) The page table can consume much less space if there are large regions of unused memory.
(c) Each segment (code, data, stack) can be managed separately.

Disadvantages:
Extra memory references to access address translation tables can slow programs down by a factor of two or more.

45
Q

What is an inverted page table?

A

Instead of having separate page tables for each process, we use a single page table that keeps track of every physical page in the system. Each entry in this single table tells us which process is using that page and which virtual page of that process maps to the physical page. So, to find the right information, we just need to search through this one big table. It’s like having one universal table that knows where everything is. ❤️

46
Q

What is swapping, within the page table section?

A

Usually a page table is big and will be stored on disk. The goal of swapping is to have the illusion of large virtual memory for multiple concurrently running processes.
Swapping is the process of moving data between the computer’s RAM and its hard drive (or another storage device) to free up RAM.

47
Q

Explain the mechanism of swapping within the page table section?

A

Page table “present bit” indicates if a memory page is in RAM. If it’s present, the MMU directly accesses it. If not, a “page fault” occurs, and the OS moves the needed page from disk to RAM, updating the page table. This enables efficient memory use.

48
Q

How do we handle a page fault?

A

1.Page fault traps OS and switches to kernel mode.
2.OS selects a page to evict from RAM and writes it to disk.
3.If the page is dirty, it’s written back to disk first.
4.OS reads the needed page from disk and puts it in RAM.
5.Page table is updated to map the new page.
6.OS returns to the instruction that caused the page fault and loads it.

49
Q

What are some of the problems that can occur with swapping mechanisms during a page fault?

A

When servicing page fault, what is OS finds that there is no free page to swap in the faulting page- OS must swap ut an existing page and then swap in the faulting page – to much work!

50
Q

What page replacement policy do we have?

A

Page Replacement Algorithms:

Optimal: Replace the page not needed for the longest time in the future (impractical).

FIFO: Replace the page brought into memory earliest (may remove popular pages).

LRU/LFU: Replace the page least recently or frequently used in the past.
51
Q

What is the LRU policy? What is LFU policy?

A

LRU the page that is used least recently will be replaced

LFU is a cache eviction algorithm called least frequently used cache

52
Q

What is the virtualization of memory represented by? And what is the virtualization of the CPU represented by?

A

Memory- The address space.| Cpu - Processes

53
Q

Which signal is used to synchronize the CPU with an I/O device when the device wishes to notify that it is ready?

A

Interrupts signal the CPU of external events, and the CPU checks for interrupts while executing instructions. If an interrupt occurs, it either completes the current instruction or handles the interrupt immediately.

54
Q

Consider an interrupt while a processor is executing an instruction. What happens?

A

The processor finishes the execution of the instruction before handling the interrupt.

55
Q

Why is memory protection important? Name a simple technique we can use and how it works.

A

It prevents users from accessing kernel or other processes’ memory. It uses a base and bounds technique, where the base is the start of process memory, and the bounds are the process length. The CPU checks if the address is within the range (base, base+bounds). If it’s in range, it proceeds; otherwise, it raises an exception.

56
Q

What is base and bounds technique?

A

The base and bounds technique is a memory management method that uses two CPU registers, the base register and the bounds register, to control a program’s memory access. The base register defines the starting point of the program’s memory, while the bounds register sets the memory limit. This technique ensures that a program can only access the memory within the specified range, preventing it from exceeding its allocated memory space.

57
Q

What translates from virtual address to physical address?

A

Memory management unit MMU.

58
Q

Why does increasing the RAM of a computer typically improve computer performance?

A

A page fault is the act of accessing a page that is not in physical memory. This means that adding more ram will make us have more virtual memory and therefore less page faults.

59
Q

Why does the translation look-aside buffer not necessarily be saved on a context switch between processes?

A

TLB helps the CPU translate addresses faster but can become outdated when switching tasks. To handle this, it’s easiest to clear the TLB entirely, ensuring it’s ready for the new task.

60
Q

What is the purpose of the free list in paged memory?

A

Keeping track of unused physical page frames.

61
Q

What is the working set model attempting to model?

A

The working set model attempt to model the critical mass of a program’s memory pages, such that most memory references will result in a cache hit, and thus improve the program’s performance.

62
Q

Belady’s anomaly occurs when?

A

The number of page faults increase despite adding more page frames.Will ONLY happen with FIFO page scheduling algorithm.Will NOT happen with LRU, LFU or Optimal page scheduling algorithms.

63
Q

What differs segmentation from basic address space?

A

Segmentation offers flexibility by using multiple base/bounds pairs, one for each logical segment of the address space like code, stack, and heap. This approach eliminates internal fragmentation in segmentation.

64
Q
A