Operating Systems Memory Management Basics

comp 530 operating systems n.w
1 / 29
Embed
Share

Explore the fundamentals of memory management in operating systems, from splitting numbers to address spaces and the importance of modular arithmetic in base 2. Discover how data is handled on chip wires and learn about address spaces in physical and virtual memory.

  • Operating Systems
  • Memory Management
  • Modular Arithmetic
  • Address Spaces
  • Base 2

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. COMP 530: Operating Systems Memory Management Basics Don Porter Portions courtesy Emmett Witchel and Kevin Jeffay 1

  2. COMP 530: Operating Systems Background Detour: Splitting Numbers In elementary school, one typically learns about the ones , tens , hundreds , etc. E.g., 13 + 24, can be modeled as: (10 * (1 + 2)) + (3 + 4) One can apply the same reasoning to space: Room numbers in SN/FB: hundreds digit indicates the floor, remaining digits indicate position within floor Street address: lower 2 digits indicate house number, upper digits indicate block Or an array of 10-byte sub-arrays: 0 1 2 3 4 5 6 7 8 9 30 0 10 40 20 2

  3. COMP 530: Operating Systems Background Detour: Splitting Numbers (2) 0 1 2 3 4 5 6 7 8 9 30 0 10 40 20 One could rename tens to index E.g., byte 34 is in sub-array index #3 One could rename ones to offset E.g., byte 34 is offset 4 in sub-array #3 In this example, address 34 becomes a tuple (3,4) In base 10, this is an intuitive concept We will use this in base 2 3

  4. COMP 530: Operating Systems Splitting Numbers in Base 2 0 1 2 3 4 5 6 7 8 9 30 0 10 40 20 Same idea applies, just need to split on powers of two instead of ten Say we go to sub-arrays of size 8: 0 1 2 3 4 5 6 7 0x18 0 0x8 0x10 0x20 4

  5. COMP 530: Operating Systems Why do we care? We will see lots of variations on using modular arithmetic to calculate an index and offset in the next few lectures And why base 2? How data is carried on wires in chip Easier to implement modular arithmetic in base 2 Use cheap logical operators instead of expensive division When dividing, if n is a power of two: x / n == x >> log2 (n) x % n == x & (n-1) 5

  6. COMP 530: Operating Systems Review: Address Spaces MAXsys Physical address space The address space supported by the hardware Starting at address 0, going to address MAXsys MAXprog Program P Virtual address space A process s view of its own memory Starting at address 0, going to address MAXprog 0 But where do addresses come from? MOV r0, @0xfffa620e 0

  7. COMP 530: Operating Systems Which is bigger, physical or virtual address space? A. Physical address space B. Virtual address space C. It depends on the system.

  8. COMP 530: Operating Systems Program Relocation Program issues virtual addresses Machine has physical addresses. If virtual == physical, then how can we have multiple programs resident concurrently? Instead, relocate virtual addresses to physical at run time. While we are relocating, also bounds check addresses for safety. I can relocate that program (safely) in two registers

  9. COMP 530: Operating Systems 2 register translation MAXsys MEMORY EXCEPTION Virtual Addresses Physical Addresses no 1500 Program P s physical address space + CPU yes 1000 500 1000 Instructions MAXprog Limit Register Base Register Program P s virtual address space 0 0

  10. COMP 530: Operating Systems With base and bounds registers, the OS needs a hole in physical memory at least as big as the process. A. True B. False

  11. COMP 530: Operating Systems The Fragmentation Problem MAX External fragmentation Unused memory between units of allocation E.g, two fixed tables for 2, but a party of 4 Internal fragmentation Unused memory within a unit of allocation E.g., a party of 3 at a table for 4 Program Q s PAS Program Code ( text ) Execution Stack Data Execution Stack Program R s PAS 0

  12. COMP 530: Operating Systems Dynamic Allocation of Partitions MAX Program P1 Simple approach: Allocate a partition when a process is admitted into the system Allocate a contiguous memory partition to the process Program P2 OS keeps track of... Full-blocks Empty-blocks ( holes ) P5 Program P3 Allocation strategies First-fit Best-fit Worst-fit Program P4 0

  13. COMP 530: Operating Systems First Fit Allocation To allocate n bytes, use the first available free block such that the block size is larger than n. 1K bytes 2K bytes 2K bytes To allocate 400 bytes, we use the 1st free block available 500 bytes 500 bytes

  14. COMP 530: Operating Systems First Fit: Rationale and Implementation Simplicity! Requires: Free block list sorted by address Allocation requires a search for a suitable partition De-allocation requires a check to see if the freed partition could be merged with adjacent free partitions (if any) Advantages Simple Tends to produce larger free blocks toward the end of the address space Disadvantages Slow allocation External fragmentation

  15. COMP 530: Operating Systems Best Fit Allocation To allocate n bytes, use the smallest available free block such that the block size is larger than (or equal to) n. 1K bytes 1K bytes 2K bytes 2K bytes To allocate 400 bytes, we use the 3rd free block available (smallest) 500 bytes

  16. COMP 530: Operating Systems Best Fit: Rationale and Implementation Avoid fragmenting big free blocks To minimize the size of external fragments produced Requires: Free block list sorted by size Allocation requires search for a suitable partition De-allocation requires search + merge with adjacent free partitions, if any Disadvantages External fragmentation Slow de-allocation Tends to produce many useless tiny fragments (not really great) Advantages Works well when most allocations are of small size Relatively simple

  17. COMP 530: Operating Systems Worst Fit Allocation To allocate n bytes, use the largest available free block such that the block size is larger than n. 1K bytes 1K bytes 2K bytes To allocate 400 bytes, we use the 2nd free block available (largest) 500 bytes

  18. COMP 530: Operating Systems Worst Fit: Rationale and Implementation Avoid having too many tiny fragments Requires: Free block list sorted by size Allocation is fast (get the largest partition) De-allocation requires merge with adjacent free partitions, if any, and then adjusting the free block list Advantages Disadvantages Slow de-allocation External fragmentation Tends to break large free blocks such that large partitions cannot be allocated Works best if allocations are of medium sizes

  19. COMP 530: Operating Systems Allocation strategies First fit, best fit and worst fit all suffer from external fragmentation. A. True B. False

  20. COMP 530: Operating Systems Eliminating Fragmentation MAX Compaction Relocate programs to coalesce holes Program P1 Swapping Preempt processes & reclaim their memory Program P2 Ready Running Program P3 ? ready queue Waiting Suspended suspended queue Program P4 semaphore/condition queues 0

  21. COMP 530: Operating Systems Sharing Between Processes 2n-1 Heap Schemes so far have considered only a single address space per process A single name space per process No sharing Run-Time Stack Program P s VAS Program Data How can one share code and data between programs without paging? Program Text 0 Program P s VAS

  22. COMP 530: Operating Systems Multiple (sub) Name Spaces 2n-1 2n4-1 Heap Heap (not shared) 2n3-1 0 Run-Time Stack (not shared) Run-Time Stack 2n2-1 0 Program Data (not shared) 2n6-1 Program Data Libraries 0 2n1-1 0 Program Text 2n5-1 Program Text (shared) User Code 0 0 0

  23. COMP 530: Operating Systems Segmentation New concept: A segment a memory object A virtual address space A process now addresses objects a pair (s, addr) s segment number addr an offset within an object Don t know size of object, so 32 bits for offset? n 0 n2 0 n1 0 s addr s addr Segment + Address register scheme Two ways to encode a virtual address Single address scheme

  24. COMP 530: Operating Systems Implementing Segmentation Program P Add a segment table containing base & limit register values CPU 1500 MEMORY EXCEPTION Program P s Segment s o no 1000 n 0 32 0 + yes Virtual Addresses Limit Register Base Register 500 1000 base limit s 0 STBR Physical Memory Segment Table

  25. COMP 530: Operating Systems Segmentation: Key Design Choices Granularity of translation: Variable-sized, byte granularity Leads to external fragmentation, no internal fragmentation Hard to dynamically resize a segment, Or only use part of a segment e.g., a few functions in libc Total number of translations: Very few (~8) Easy to cache in a few registers in hardware Programmer abstractions: Can be fully transparent to programmer (encoded in address space offsets) or explicit (with registers) Designed in the era of assembly programming (stack, code, data segments) Kernel bookkeeping: Essentially all in the allocator PCB stores segment definitions 25

  26. COMP 530: Operating Systems Are we done? Segmentation allows sharing And dead simple hardware Can easily cache all translation metadata on-chip Low latency to translate virtual addresses to physical addresses Two arithmetic operations (add and limit check) but leads to poor memory utilization We might not use much of a large segment, but we must keep the whole thing in memory (bad memory utilization). Suffers from external fragmentation Allocation/deallocation of arbitrary size segments is complex How can we improve memory management?

  27. COMP 530: Operating Systems Thought experiment 1 What would be the impact of making all segments the same size? No more external fragmentation! Simpler allocation! No more best fit and friends just a bitmap But trade for internal fragmentation Segments too big waste space Segments too small may not be enough address space for an app 27

  28. COMP 530: Operating Systems Thought experiment 2 What would be the impact of having many (say thousands or millions) of segments (vs tens)? Can afford to have smaller segments with less code/data Waste less space on unused mappings Finer-grained swapping/compaction More complex mapping structure Can t just use thousands or millions of registers to cache mapping on CPU anymore 28

  29. COMP 530: Operating Systems Trivia: Revisiting fork() I promised to explain the historical reason for fork() On the original machine Unix was designed for, there was only segmented memory protection, and very, very little DRAM. Easiest way to create a new process was to: Write the relevant segments of the parent process to disk Effectively, making a copy of the process memory on disk Reload copied segments into memory to run child So they made a software abstraction that matched efficient use of early 1970s virtual memory hardware And we still (inefficiently) emulate it on modern hardware 29

More Related Content