Notes/Operating Systems/Virtual Memory.md

105 lines
3.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
type: theoretical
backlinks:
- "[[Memory Management]]"
---
Separating logical memory from physical memory.
- Allows **very large** virtual memory, when only a smaller physical memory is available
The **virtual address space** of a process refers to the logical view of how a process is stored
## Implementation
Maintain a copy of the process (including code,data heap and stack segments) in a special area of the HDD named **virtual memory**
> [!IMPORTANT]- Refresher - code, data, heap and stack segments
> ![](Pasted%20image%2020250505042419.png)
>[!NOTE]- Another refresher - stack vs. heap allocation
> ![](Pasted%20image%2020250505042548.png)
- Process is divided into pages
- If a page is referred to, it is brought into main memory (**DEMAND PAGING**)
## Page faults
When a page is referenced but not present in memory - a main memory access is required.
## Page replacement
The act of replacing a frame in memory with another one (which we need) from the main memory.
### Basic Page Replacement
Linear search for free frame, if none -> select victim page via **a page replacement algorithm** and do your thing!
## Typical pages
Contain:
- A P (or V for valid) bit -> page is in RAM (else page fault)
- M (dirty) bit - page in RAM modified
- R bit - referenced since last time R was cleared
## Beladys Anomaly
Increasing the number of page frames (or memory pages) allocated to a process can lead to an increase in the number of page faults, rather than a decrease.
## Frame-allocation algorithms
How many frames to give to each process
### Fixed
Each process receives the same number of frames -> **Equal Allocation**
### Proportional
Allocate according to size of process.
## Page-replacement algorithms
Which page to replace, optimizing for lowest page fault rate.
### FIFO
Read title.
### Least Recently Used (LRU)
Replace page that has not been used recently.
**Counter**:
- Every page entry has a counter; every time page is referenced through this entry, copy the clock into the counter.
- When a page is to be changed, find smallest value in counters.
**Stack**:
- Keep a stack of page numbers in a double link form[^1]
- On page reference - move to top
### LRU Approximation
Since LRU is slow as shit, we need an approximation.
We introduce a **Reference bit**:
- Each page has `R = 0` at first
- Whenever referenced `R = 1`
- Replace any page with `R = 0`
#### Second-chance (Clock)
Do the above. It's called a clock cuz it's like a `itertools.cycle()`. It holds the state of last accessed page (clock hand) and moves on from there on next iteration.
## The Working Set Model
We want to have the set of pages "in use" in RAM!
- Working set changes over time
- Usually approximated by the set of pages used by some amount of most recent references
## Thrashing
When a process is busy swapping pages.
Multiprogramming[^2] is high, CPU utilization[^3] is low.
### Solving it
Give the process(es) more memory by:
- Taking it from another process
- Swap out shit until demand is lower (scheduler)
## Inverted page table
Inverted page tables are a memory management method that uses a single table for the entire system to map physical memory addresses to virtual addresses. Instead of each process having its own page table, the inverted page table has entries for each physical page.
## I/O Interlock
Pages must sometimes be locked into memory (i.e. I/O). This is what this is called.
---
[^1]: Doubly-linked list where we can go `prev` and `next`
[^2]: Many processes wanting resources
[^3]: Shit actually happening