CS 300 - Operating Systems - Exam 2
Multi-level Page Table: Level of indirection
-A multi-level structure can adjust level of indirection through use of the page directory. -Indirection place page-table pages wherever we would like in physical memory.
Swapping: The page fault
-Accessing page that is not in physical memory. -If a page is not present and has been swapped disk, the OS need to swap the page into memory in order to service the page fault.
Swapping: Present Bit
-Add some machinery higher up in the system in order to support swapping pages to and from the disk. -When the hardware looks in the PTE, it may find that the page is not present in physical memory. Value = 1 The page is present in physical memory Value = 0 The page is not in memory but rather on disk.
Segregated List: Slab Allocator
-Allocate a number of object caches. -The objects are likely to e requested frequently. -e.g., locks, file-system inodes, etc. -Request some memory from a more general memory allocator when a given cache is running low on free space.
Single large address for a process
-Always need to first arrange for the code or data to be in memory when before calling a function or accessing data. -To Beyond just a single process. -The addition of swap space allows the OS to support the illusion of a large virtual memory for multiple concurrently-running process
Inefficiency of the Base and Bound Approach
-Big chunk of "free" space -"free" space takes up physical memory. -Hard to run when an address space does not fit into physical memory
TLB Issue: Context Switching
-Can't Distinguish which entry is meant for which process To solve: -Provide an address space identifier(ASID) field in the TLB.
void *realloc(void *ptr, size_t size)
-Change the size of memory block. -A pointer returned by _________ may be either the same as ptr or a new. Argument -void *ptr: Pointer to memory block allocated with malloc, calloc or realloc -size_t size: New size for the memory block(in bytes) Return -Success: Void type pointer to the memory block -Fail : Null pointer
Benefits of Memory Virtualization
-Ease of use in programming -Memory efficiency in terms of times and space -The guarantee of isolation for processes as well as OS -Protection from errant accesses of other processes
Virtual Address
-Every address in a running program is virtual. -OS translates the virtual address to physical address
TLB Replacement Policy: LRU(Least Recently Used)
-Evict an entry that has not recently been used. -Take advantage of locality in the memory-reference stream.
Splitting
-Finding a free chunk of memory that can satisfy the request and _______ it into two. -When request for memory allocation is smaller than the size of free chunks
Managing Free Space: Best Fit
-Finding free chunks that are big or bigger than the request -Returning the one of smallest in the chunks in the group of candidates
Managing Free Space: First Fit
-Finding the first chunk that is big enough for the request -Returning the requested amount and remaining the rest of the chunk.
Managing Free Space: Next Fit
-Finding the first chunk that is big enough for the request. -Searching at where one was looking at instead of the beginning of the list.
Managing Free Space: Worst Fit
-Finding the largest free chunks and allocation the amount of the request -Keeping the remaining chunk on the free list.
Coalescing
-If a user requests memory that is bigger than free chunk size, the list will not find such a free chunk. -Merge returning a free chunk with existing chunks into a large single free chunk if addresses of them are nearby.
Segmentation Fault or Violation
-If an illegal address such as 7KB which is beyond the end of heap is referenced, the OS occurs segmentation fault. -The hardware detects that address is out of bounds.
Problem of Hybrid Approach
-If we have a large but sparsely-used heap, we can still end up with a lot of page table waste. -Causing external fragmentation to arise again.
Hybrid Approach: Paging and Segments
-In order to reduce the memory overhead of page tables. -Using base not to point to the segment itself but rather to hold the physical address of the page table of that segment. -The bounds register is used to indicate the end of the page table.
Inverted Page Tables
-Keeping a single page table that has an entry for each physical page of the system. -The entry tells us which process is using this page, and which virtual page of that process maps to this physical page.
Managing Free Space: Segregated List
-Keeping free chunks in different size in a separate list for the size of popular request. New Complication: -How much memory should dedicate to the pool of memory that serves specialized requests of a given size? -Slab allocator handles this issue.
Multiprogramming and Time Sharing
-Load multiple processes in memory. -Execute one for a short while. -Switch processes between them in memory. -Increase utilization and efficiency. -Cause an important protection issue. -Errant memory accesses from other processes
In newer languages how is memory dealt with
-Many newer languages have support for automatic memory management. -While you call something akin to malloc() to allocate memory (ex: new), you never have to call something to free space -A garbage collector runs and figures out what memory you no longer have references to and frees it for you.
Beyond Physical Memory: Policies
-Memory pressure forces the OS to start paging out pages to make room for actively-used pages. -Deciding which page to evict is encapsulated within the replacement policy of the OS.
Memory Virtualizing with Efficiency and Control
-Memory virtualizing takes a similar strategy known as limited direct execution(LDE) for efficiency and control. -In memory virtualizing, efficiency and control are attained by hardware support. -e.g., registers, TLB(Translation Look-aside Buffer)s, page-table
Growing the Heap
-Most allocators start with a small-sized heap and then request more memory from the OS when they run out.
Address Space
-OS creates an abstraction of physical memory. -The address space contains all about a running process. -That is consist of program code, heap, stack and etc.
What is memory virtualization?
-OS virtualizes its physical memory. -OS provides an illusion memory space per each process. -It seems to be seen like each process uses the whole memory.
When Replacements Really Occur
-OS waits until memory is entirely full, and only then replaces a page to make room for some other page -This is a little bit unrealistic, and there are many reasons for the OS to keep a small portion of memory free more proactively.
Paging: Smaller Tables
-Page table are too big and thus consume too much memory. -Assume that 32-bit address space with 16KB pages and 4-byte page-table entry. -Big pages lead to internal fragmentation
Translation Lookaside Buffer (TLB)
-Part of the chip's memory-management unit(MMU). -A hardware cache of popular virtual-to-physical address translation.
Goals of virtual memory system
-Protection -Efficiency -Transparency
Beyond Physical Memory: Mechanisms
-Require an additional level in the memory hierarchy. -OS need a place to stash away portions of address space that currently aren't in great demand. -In modern systems, this role is usually served by a hard disk drive
Swap Space
-Reserve some space on the disk for moving pages back and forth. -OS need to remember to the swap space, in page-sized unit
Segmentation
-Segment is just a contiguous portion of the address space of a particular length. -Logically-different segment: code, stack, heap -Each segment can be placed in different part of physical memory. -Base and bounds exist per each segment.
Swapping: What if memory is full?
-The OS like to page out pages to make room for the new pages the OS is about to bring in. -The process of picking a page to kick out, or replace is known as page-replacement policy
OS Issues for Memory Virtualizing
-The OS must take action to implement base-and-bounds approach. -Three critical junctures: 1. When a process starts running: -Finding space for address space in physical memory 2.When a process is terminated: -Reclaiming the memory for use 3.When context switch occurs: -Saving and storing the base-and-bounds pair
TLB Miss on Hybrid Approach
-The hardware get to physical address from page table. -The hardware uses the segment bits(SN) to determine which base and bounds pair to use. -The hardware then takes the physical address therein and combines it with the VPN as follows to form the address of the page table entry(PTE) .
Tracking The Size of Allocated Regions
-The interface to free(void *ptr) does not take a size parameter. -How does the library know the size of memory region that will be back into free list? -Most allocators store extra information in a header block. -MUST contain the size of the allocated memory region. -The header may also contain -Additional pointers to speed up deallocation -A magic number for integrity checking
Embedding a Free List
-The memory-allocation library initializes the heap and puts the first element of the free list in the free space. -The library can't use malloc() to build a list within itself. -If a chunk of memory is requested, the library will first find a chunk that is large enough to accommodate the request. -The library will 1. Split the large free chunk into two. (One for the request and the remaining free chunk) 2. Shrink the size of free chunk in the list.
Multi-level Page Tables: Page directory entries
-The page directory contains one entry per page of the page table. -It consists of a number of page directory entries(PDE). -PDE has a valid bit and page frame number(PFN).
The Header of Allocated Memory Chunk
-The size for free region is the size of the header plus the size of the space allocated to the user. -If a user request N bytes, the library searches for a free chunk of size N plus the size of the header
Swap daemon/page Daemon
-There are fewer than LW pages available, a background thread that is responsible for freeing memory runs. -The thread evicts pages until there are HW pages available.
Paging: Too Slow
-To find a location of the desired PTE, the starting location of the page table is needed. -For every memory reference, paging requires the OS to perform one extra memory reference.
Multi Level Page Tables
-Turns the linear page table into something like a tree. -Chop up the page table into page-sized units. -If an entire page of page-table entries is invalid, don't allocate that page of the page table at all. -To track whether a page of the page table is valid, use a new structure, called page directory.
Common Flags Of Page Table Entry
-Valid Bit: Indicating whether the particular translation is valid. -Protection Bit: Indicating whether the page could be read from, written to, or executed from -Present Bit: Indicating whether this page is in physical memory or on disk(swapped out) -Dirty Bit: Indicating whether the page has been modified since it was brought into memory -Reference Bit(Accessed Bit): Indicating that a page has been accessed
Paging: Linear Tables
-We usually have one page table for every process in the system. -Assume that 32-bit address space with 4KB pages and 4-byte page-table entry. -Page tables are too big and thus consume too much memory.
Dynamic(Hardware base) Relocation
-When a program starts running, the OS decides where in physical memory a process should be loaded. -Set the base register a value. physical address = virtual address + base -Every virtual address must not be greater than bound and negative. 0 ≤ virtual address < bounds
System Calls int brk(void *addr) void *sbrk(intptr_t increment); void *mmap(void *ptr, size_t length, int port, int flags, int fd, off_t offset)
-malloc library call use ___1___ system call. -___1___ is called to expand the program's break. -break: The location of the end of the heap in address space -___2___ is an additional call similar with ___1___. -Programmers should NEVER directly call either ___1___ or ___2___. ___3___ system call can create an anonymous memory region.
Concept of Paging
-splits up address space into fixed-zed unit called a page. -Segmentation: variable size of logical segments(code, stack, heap, etc.) -With _____, physical memory is also split into some number of pages called a page frame. -Page table per process is needed to translate the virtual address to physical address.
Multi-level Page Table Control Flow
1. extract the virtual page number(VPN) 2. check if the TLB holds the translation for this VPN 3. extract the page frame number from the relevant TLB entry, and form the desired physical address and access memory 4. extract the Page Directory Index(PDIndex) 5. get Page Directory Entry(PDE) 6. Check PDE valid flag. If valid flag is true, fetch Page Table entry from Page Table
TLB Basic Algorithms
1. extract the virtual page number(VPN). 2. check if the TLB holds the translation for this VPN. 3. extract the page frame number from the relevant TLB entry, and form the desired physical address and access memory. 4. The hardware accesses the page table to find the translation. 5. updates the TLB with the translation.
Memory Leak
A program runs out of memory and eventually dies.
Multi-level Page Tables: Advantage & Disadvantage
Advantage -Only allocates page-table space in proportion to the amount of address space you are using. -The OS can grab the next free page when it needs to allocate or grow a page table. Disadvantage -Multi-level table is a small example of a time-space trade-off. -Complexity.
In heap memory...
All allocations and deallocations are explicitly handled by you, the programmer.
void* malloc(size_t size)
Allocate a memory region on the heap. Argument -size_t size : size of the memory block(in bytes) -size_t is an unsigned integer type. Return -Success : a void type pointer to the memory block allocated by ________ -Fail : a null pointer
void *calloc(size_t num, size_t size)
Allocate memory on the heap and zeroes it before returning. Argument -size_t num : number of blocks to allocate -size_t size : size of each block(in bytes) Return -Success : a void type pointer to the memory block allocated by ________ -Fail : a null pointer
Stack memory is often called automatic memory because
Allocations and deallocations of it are managed implicitly by the compiler for you, the programmer
Temporal Locality
An instruction or data item that has been recently accessed will likely be re-accessed soon in the future.
Not allocating enough memory is also known as
Buffer Overflow
Referring to Segment: Explicit Approach
Chop up the address space into segments based on the top few bits of virtual address.
What address space consists of
Code -Where instructions live Heap -Dynamically allocate memory. -malloc in C language -new in object-oriented language Stack -Store return addresses or values. -Contain local variables arguments to routines.
OS support: Fragmentation
External Fragmentation: little holes of free space in physical memory that make difficulty to allocate new segments. -Compaction: rearranging the existing segments in physical memory. -Compaction is costly. 1. Stop running process. 2. Copy data to somewhere. 3. Change segment register value.
Advantages Of Paging
Flexibility: Supporting the abstraction of address space effectively -Don't need assumption how heap and stack grow and are used. Simplicity: ease of free-space management -The page in address space and the page frame are the same size. -Easy to allocate and keep a free list
Double Free
Free memory that was freed already. (Leads to undefined error)
Dangling Pointer
Freeing memory before it is finished using -A program accesses to memory with an invalid pointer
TLB is managed by _________ method.
Full Associative
Who handles the TLB miss?
HARDWARE ON CISC -Hardware handle the TLB miss entirely on CISC. -The hardware has to know exactly where the page tables are located in memory. -The hardware would "walk" the page table, find the correct page-table entry and extract the desired translation, update and retry instruction. -hardware-managed TLB. SOFTWARE ON RISC -RISC have what is known as a software-managed TLB. -On a TLB miss, the hardware raises exception( trap handler ). -Trap handler is code within the OS that is written with the express purpose of handling TLB miss.
Spatial Locality
If a program accesses memory at address x, it will likely soon access memory near x.
Dynamic Relocation: Operating System Responsibilities
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 Exception handling -Code to run when exceptions arise; likely action is to terminate offending process
Hardware Requirements
Privileged mode -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
sizeof()
Routines and macros are utilized for _____ in malloc instead typing in a number directly. Two types of results of ________ with variables 1. The actual _______ 'x' is known at run-time. 2. The actual ______ 'x' is known at compile-time.
Managing Free Space: One major problem with many of the approaches described above is their lack of ________.
Scaling
Support for Sharing
Segment can be shared between address space. -Code sharing is still in use in systems today. -by extra hardware support. Extra hardware support is need for form of Protection bits. -A few more bits per segment to indicate permissions of read, write and execute.
Base and Bounds (Dynamic Reallocation)
Specifically, we'll need two hardware registers within each CPU: one is called the base register, and the other the bounds (sometimes called a limit register). This base-and-bounds pair is going to allow us to place the address space anywhere we'd like in physical memory, and do so while ensuring that the process can only access its own address space. 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.
Segment Registers (With Negative-Growth Support)
Stack grows backward. Extra hardware support is need. -The hardware checks which way the segment grows. 1: positive direction, 0: negative direction
Page Fault Control Flow - Software
The OS must find a physical frame for the soon-be-faulted-in page to reside within. If there is no such page, waiting for the replacement algorithm to run and kick some pages out of memory.
OS Issues: When a Process Starts Running
The OS must find a room for a new address space. -free list : A list of the range of the physical memory which are not in use.
OS Issues: When a Process Is Terminated
The OS must put the memory back on the free list.
OS Issues: When Context Switch Occurs
The OS must save and restore the base-and-bounds pair. -In process structure or process control block(PCB)
Managing Free Space: Binary Buddy Allocation
The allocator divides free space by two until a block that is big enough to accommodate the request is found. -Can suffer from internal fragmentation. -Makes coalescing simple. -Coalescing two blocks in to the next level of block.
Address Translation
The hardware takes a virtual address the process thinks it is referencing and transforms it into a physical address which is where the data actually resides -The OS must get involved at key points to set up the hardware. -The OS must manage memory to judiciously intervene.
What's in the page table?
The page table is just a data structure that is used to map the virtual address to physical address. -Simplest form: a linear page table, an array -The OS indexes the array by VPN, and looks up the page-table entry. -Page number: To find the entry point for that page in the page table. -Frame address: To get the address where the page is located. -Present bit: To know whether the page is in memory. -Dirty bit: To know if the page has been modified. -Disk address: To know where the page is in the hard drive.
MMU (Memory Management Unit)
The part of the processor that helps with address translation
When the OS decides to stop running a process, it must save the values of the base and bounds registers to memory, in some per-process structure such as ________.
The process structure or process control block (PCB)
Protection enables us to deliver what?
The property of isolation among processes; each process should be running in its own cocoon, safe from the ravages of other faulty or even malicious processes.
Paging: Address Translation
Two components in the virtual address -VPN: virtual page number -Offset: offset within the page
Forgetting to initialize allocated memory
Your program will eventually encounter an uninitialized read, where it reads from the heap some data of unknown value.
Not Compacted vs Compacted
___1___ Lots of "slices" of allocated and free (inefficient) ___2___ One slice for allocated, one for free (efficient)
Fine-Grained and Coarse-Grained
___2___ means segmentation in a small number. -e.g., code, heap, stack. ___1___ segmentation allows more flexibility for address space in some early system. -To support many segments, Hardware support with a segment table is required.
void free(void* ptr)
______ a memory region allocated by a call to malloc. Argument -void *ptr : a pointer to a memory block allocated with malloc Return -none
The general problem that arises is that physical memory quickly be- comes full of little holes of free space, making it difficult to allocate new segments, or to grow existing ones. We call this problem _______.
external fragmentation
Where are page tables stored?
memory
Address Translation on Segmentation
physical address = offset + base -The offset of virtual address 100 is 100. -The code segment starts at virtual address 0 in address space. Virtual address+base is not the correct physical address. -The offset of virtual address 4200 is 104. -The heap segment starts at virtual address 4096 in address space.
Forgetting To Allocate Memory
segfault
Unused address spaces are also called
sparse address spaces
The TLB improves performance due to __________.
spatial locality
To speed address translation...
use TLB
Sharing of pages is ______ as it reduces the number of physical pages in use.
useful