Docsity

Understanding Memory Management in OS: Address Spaces, Swapping, Segmentation, Study notes of Operating Systems

Partial preview of the text

Download Understanding Memory Management in OS: Address Spaces, Swapping, Segmentation and more Study notes Operating Systems in PDF only on Docsity!

Operating Systems

Memory Management

Lecture 9

Michael O’Boyle

1

Memory Management

  • Background
  • Logical/Virtual Address Space vs Physical

Address Space

  • Swapping
  • Contiguous Memory Allocation
  • Segmentation

Background

  • Program must be brought (from disk) into memory and placed

within a process for it to be run

  • Main memory and registers are only storage CPU can access

directly

  • Memory unit only sees a stream of addresses + read requests,

or address + data and write requests

  • Register access in one CPU clock (or less)
  • Main memory can take many cycles, causing a stall
  • Cache sits between main memory and CPU registers
  • Protection of memory required to ensure correct operation

Base and Limit Registers

  • A pair of base and limit registers define the logical address

space

  • CPU must check every memory access generated in user

mode to be sure it is between base and limit for that user

Virtual addresses for multiprogramming

  • To make it easier to manage memory of multiple processes,

make processes use logical or virtual addresses

  • Logical/virtual addresses are independent of location in physical memory data lives - OS determines location in physical memory
  • Instructions issued by CPU reference logical/virtual

addresses

  • e.g., pointers, arguments to load/store instructions, PC …
  • Logical/virtual addresses are translated by hardware into

physical addresses (with some setup from OS)

7

Logical/Virtual Address Space

  • The set of logical/virtual addresses a process can reference

is its address space

  • many different possible mechanisms for translating logical/virtual addresses to physical addresses
  • Program issues addresses in a logical/virtual address space
  • must be translated to physical address space
  • Think of the program as having a contiguous logical/virtual address space that starts at 0,
  • and a contiguous physical address space that starts somewhere else
  • Logical/virtual address space is the set of all logical

addresses generated by a program

  • Physical address space is the set of all physical

addresses generated by a program

8

MMU as a relocation register

Swapping

  • What if not enough memory to hold all processes?
  • A process can be swapped temporarily
    • out of memory to a backing store,
    • brought back into memory for continued execution
    • Total physical memory space of processes can exceed physical memory
  • Backing store – fast disk
    • large enough to accommodate copies of all memory images for all users;
    • must provide direct access to these memory images
  • Roll out, roll in – swapping variant
    • used for priority-based scheduling algorithms;
    • lower-priority process is swapped out so higher-priority process can be loaded and executed
  • Major part of swap time is transfer time;
    • total transfer time is directly proportional to the amount of memory swapped
  • System maintains a ready queue
    • ready-to-run processes which have memory images on disk

Context Switch Time including Swapping

  • If next processes to be put on CPU is not in memory,
    • need to swap out a process and swap in target process
  • Context switch time can then be very high
  • Can reduce cost
    • reduce size of – by knowing how much memory really being used
    • inform OS of memory use via request_memory() and release_memory()
  • Other constraints as well on swapping
    • Pending I/O – can’t swap out as I/O would occur to wrong process
  • Or always transfer I/O to kernel space, then to I/O device
    • Known as double buffering , adds overhead
  • Standard swapping not used in modern operating systems
    • But modified version common
      • Swap only when free memory extremely low

Contiguous Allocation

  • Main memory must support both OS and user processes
  • Limited resource, must allocate efficiently
  • Contiguous allocation is one early method
  • Main memory usually into two partitions :
    • Resident operating system, usually held in low memory with interrupt vector
    • User processes then held in high memory
    • Each process contained in single contiguous section of memory

Hardware Support for Relocation and Limit Registers

Multiple-partition allocation

  • Multiple-partition allocation
    • Degree of multiprogramming limited by number of partitions
    • Exam 2 approaches
      • Fixed partition
      • Variable partition

Mechanics of fixed partitions

19 partition 0 partition 1 partition 2 partition 3 0 2K 6K 8K 12K physical memory offset + Logical address P2’s base: 6K base register 2K <? no raise protection fault limit register yes

Old technique #2: Variable partitions

  • Obvious next step: physical memory is broken up into

partitions dynamically – partitions are tailored to programs

  • hardware requirements: base register, limit register
  • physical address = logical address + base register
  • Advantages
  • no internal fragmentation
  • simply allocate partition size to be just big enough for process (assuming we know what that is!)
  • Problems
  • external fragmentation
  • as we load and unload jobs, holes are left scattered throughout physical memory 20