Modul 4 - Virtuelltminne Flashcards
(42 cards)
In which direction does heap and stack grow?
- Heap grows downward
- Stack grows upward
What is the address space?
It’s an abstraction of the physical memory and it’s the programs view of memory in the system.
For example the OS can think that it start at the physical address 0, but instead it is loaded at some arbitrary physical address(es).
What does the address space of a process contain?
It contains all of the memory state of the running program.
- The code of the program (the instructions) (static)
- Stack (used by the program) (grows)
- Heap (used by the program) (grows)
- Other things too such as statically-initialized variables
What does it mean when the OS is visualizing memory?
It’s when the OS gives the program an illusion of being loaded at a particular address (say 0) and has a potentially very large address space (say 64-bits), when the reality is different.
What is transparency (in terms of virtual memory)?
The OS implements the virtual memory in a way that is invisible to the running program. Thus, the program isn’t aware of the fact that memory is virtualized; rather, the program behaves as if it has its own private physical memory.
Goals with the virtual memory?
- Transparency (processes should be
unaware of virtualization.) - Efficiency (make virtualization as efficient as possible both in terms of time and space. Execution should be as close to real execution as possible.)
- Protection (processes should not be
able to interfere with each other or the OS, i.e., affect their memory)
What is the hardware-based address translation?
- Transforming a virtual address into a physical address.
- The relocation of the address happens in runtime and is therefore often referred to as dynamic relocation.
With address translation, the hardware transforms each memory access (e.g., an instruction fetch, load, or store), changing the virtual address provided by the instruction to a physical address where the desired information is actually located.
How does base and bounds/dynamic relocation work?
- Two hardware registers within each CPU: one called base register and one called bounds.
- This base-and-bounds pair allow us to place the address space anywhere in physical memory and ensuring the process only can access its own address space.
- When any memory reference is generated by the process, it’s translated by the processor in the following manner:
- physical address = virtual address + base
Each memory reference generated by the process is a virtual address; the hardware in turn adds the contents of the base register to this address and the result is a physical address that can be issued to the memory system.
- The bounds register is there to help with protection.
Specifically, the processor will first check that the memory reference is within bounds to make sure it is legal.
What is the memory management unit MMU?
- It’s the part of the processor that helps with address translation.
- For example the base and bounds registers are hardware structures kept there.
Hardware requirements for address translation?
- Privileged mode (kernel) - Needed to prevent user-mode processes from executing privileged operations
- Base/bounds registers - Need pair of registers per CPU to support address translation and bounds checks.
- Ability to translate virtual addresses and check if within bounds - Circuitry to do translations and check limits; in this case, quite simple.
- Privileged instruction(s) to update base/bounds - OS must be able to set these values before letting a user program run.
- Privileged instruction(s) to register exception handlers - OS must be able to tell hardware what code to run if exception occurs.
- Ability to raise exceptions - When processes try to access privileged instructions or out-of-bounds memory.
Pros and cons with base and bounds?
Pros:
- Transparent to a process.
- Simple to implement.
- Easy to change process.
- Offers protection
Cons:
- How do we share data?
- Hard to run a program when the entire address space dosen’t fit into memory.
- Wasted memory. (Internal fragmentation). Because there becomes a big chunk of “free” space between the stack and the heap.
OS requirements for address translation?
- Memory management:
- Need to allocate memory for new processes;
- Reclaim memory from terminated processes;
- Generally manage memory via free list
- Base/bounds management - Must set base/bounds properly upon context switch (save and restore)
- Exception handling - Code to run when exceptions arise; likely action is to terminate offending process
What is the process control block (PCB)
It’s a per-process structure where the OS saves and store the values of the base and bounds registers in memory.
How to solve the base-bounds cons? That is support large address space and without wasting “free” space between the stack and the heap?
- Segmentation: Generalized Base/Bounds
How is the idea of segmentation?
Instead of having just one base and bounds pair in the MMU, have a base and bounds pair per logical segment of the address space. That is one for the code, stack and heap of a process. This lets the OS place each segment in different parts of physical memory and avoids filling physical memory with unused virtual address space.
In a 14-bit virtual address, which bits tells the hardware which segment an address refers to? (explicit approach)
The top two bits are used to select the segment. The rest of the bits (12) are the offset.
For example if the top two bits are (01) then the hardware knows the address is in the heap. Thus uses the heap base and bounds.
How does the hardware handle negative growth (stack that grows upwards)?
- Has one bit that shows if it grows negative (0) or positive (1).
- The correct negative offset is obtained by subtracting the offset to the maximum segment size.
=> maximum segment size - offset = correct negative offset.
What extra support from the hardware is needed so that segments can share memory?
- Protection bits are needed so that a process knows if it can read, write, execute the shared code.
- By setting a code segment to read-only it can be shared by multiple processes without harming isolation.
- Processes don’t know that they are sharing code (transparency is kept)
Coarse-grained and fine-grained segmentation ?
Coarse-grained - Chops the address space into relatively large coarse chunks. (easier to implement)
Fine-grained - Allows address spaces to consists of more smaller segments, requires more hardware support with a segment table. (allows the compiler and operating system to do a better job)
Which issues does segmentation arise?
- Upon context switch, each segment registers must be saved and restored correctly.
- Isn’t flexible enough to support sparse address space. For example if we have a large but sparsely-used heap all in one logical segment, then the entire heap must be in memory in order to access it.
- External fragmentation because the different segments varies in size. Thus the physical memory quickly becomes full of little holes of free space making it hard to allocate new segments or grow existing ones.
Solution:
- Use a free-list management algorithm that tries to keep large extents of memory available for allocation.
- Paging
What is paging?
-It’s a space-management approach where the memory is chopped up into fixed-sized pieces to avoid external fragmentation (which is a problem in different sized chunks).
What is a page frame? (Paging)
- The physical memory is viewed as an array of fixed-sized slots called page frames.
What is a page? (Paging)
- A process’s address space is divided into fixed-sized units is called a page. (Instead of dividing in logical segments: code, heap stack)
What role does the page table have?
- It’s a data structure that keeps track of each virtual page of the address space placed in physical memory.
- Stores address translations for each of the virtual pages of the address space, thus letting us know where in physical memory each page resides.
Ex. (Virtual Page 0 → Physical Frame 3)
- Is an per-process data structure (each process has one page table)
- It has the virtual page numbers (VPNs) and corresponding Physical page numbers (PFNs)
- A valid bit that indicate whether a particular translation is valid. Ex. All unused space in-between different parts of the process are marked invalid.
- Protection bits, indicating if the page could be read from, written to or executed from.
- Present bit indicated whether a page is in physical memory or on disk.
- A dirty bit indicates if a page has been modified since it was brought into memory.
- A reference bit is used to track whether a page has been accessed, useful to determine which pages are popular which is critical during page replacement.