Operating Systems Lecture 7: Introduction to virtual memory

Operating Systems Lecture 7: Introduction to virtual memory

Understanding Virtual Memory Management

Introduction to Virtual Memory

  • The upcoming lectures will focus on memory management within operating systems, specifically the concept of virtual memory as an abstraction of physical RAM.
  • The necessity for virtualizing memory arises from the complexity and messiness of actual physical memory, which cannot be directly exposed to user processes.

Evolution of Memory Usage

  • Historically, machines had a simpler memory structure where only one process's code and data resided in RAM alongside some OS code.
  • Modern systems run multiple processes concurrently, leading to a more complex arrangement where different processes share the same memory space.

Complexity of Memory Layout

  • In reality, many processes exist simultaneously in various non-contiguous segments of memory, complicating how programs access their data.
  • This complexity must be abstracted away by the operating system so that programmers do not need to manage or understand these details.

Address Space Abstraction

  • The operating system provides each process with a clean abstraction known as an address space or virtual address space.
  • Each process perceives it has access to a large contiguous block of memory starting from address zero up to a maximum value.

Components of Address Space

  • A process's memory image includes its code, variables, heap (for dynamic allocation), and stack (for function calls).
  • When executing instructions, the CPU interacts with these virtual addresses without needing awareness of their physical locations in RAM.

Translation from Virtual to Physical Addresses

  • Accessing specific addresses involves translating virtual addresses into physical addresses through hardware mechanisms.
  • The operating system tracks where each process's components reside in physical memory and communicates this information to the Memory Management Unit (MMU).

Role of the Memory Management Unit (MMU)

  • The MMU is responsible for translating requests from virtual addresses issued by the CPU into corresponding physical addresses in RAM.

Memory Management and Address Translation

Understanding Address Translation

  • The user perceives accessing a specific virtual address, while the actual memory accesses a different physical address.

Introduction to Paging

  • Paging is a technique utilized in modern operating systems, contrasting with the idea of storing all process code and data as one chunk.

Memory Allocation Process

  • The operating system divides the entire address space into fixed-size chunks called pages, while physical memory is divided into physical frames.
  • When a process requests memory, it allocates pages mapped to free physical frames (e.g., page zero maps to frame three).

Page Table and MMU Functionality

  • The operating system maintains mappings in a page table that links virtual pages to physical frames for efficient access.
  • The Memory Management Unit (MMU) uses these mappings to translate virtual addresses into physical addresses.

Goals of Memory Virtualization

  • Key goals include:
  • Transparency: User programs should not be aware of the underlying details of memory management.
  • Efficiency: Minimize waste of physical memory and overhead during translation processes.
  • Isolation: Ensure processes can only access their own allocated memory space for protection.

Programmer's Perspective on Memory Allocation

  • Programmers allocate memory through various means within their code, including static global variables at compile time and local variables on the stack during function calls.

Different Memory Areas in Programs

  • Memory allocation occurs in several areas:
  • Static/global variables are allocated at compile time within executables.
  • Local variables are allocated on the stack when functions are called.
  • Dynamic allocations occur in the heap during runtime using functions like malloc.

Strategic Decisions for Memory Allocation

Understanding Memory Allocation and System Calls

Dynamic Memory Allocation in C

  • When allocating memory dynamically, the malloc function is used. It is important to note that malloc itself is not a system call but a function from the C library.
  • The malloc function allocates memory on the heap and returns a pointer to this allocated memory.
  • The C library's implementation of malloc employs various algorithms for managing heap memory and free space efficiently.

Handling Heap Memory Exhaustion

  • If the heap runs out of memory, the C library makes use of system calls like BRK or its variant sbrk to grow the heap size.
  • Users are discouraged from directly using these system calls; instead, they should rely on functions like malloc, which manage heap growth automatically.

Alternative Memory Management with mmap

  • For obtaining additional pages of memory outside of the heap or stack, programmers can utilize the mmap system call, which allocates a page in the process's address space.
  • This allocated page is referred to as an "anonymous page," allowing users to store any data structure within it.

Address Space and Operating System Integration

  • Each running process has its own address space consisting of virtual addresses mapped to physical addresses.
  • In modern operating systems, kernel code operates within each process's address space rather than as a separate entity. This integration allows processes to view OS code similarly to their own executable code.

Memory Allocation for Operating System Structures

Video description

Based on the book Operating Systems: Three Easy Pieces (http://pages.cs.wisc.edu/~remzi/OSTEP/) For more information please visit https://www.cse.iitb.ac.in/~mythili/os/