Memory management is a critical function of an operating system (OS) that involves managing the computer's primary memory. The OS is responsible for allocating memory to processes when they need it and deallocating it when they are done. This ensures efficient utilization of memory resources and system stability.

Key Concepts in Memory Management

  1. Memory Allocation

    • Static Allocation: Memory is allocated at compile time.
    • Dynamic Allocation: Memory is allocated at runtime.
  2. Memory Deallocation

    • Automatic Deallocation: Managed by the OS or runtime environment.
    • Manual Deallocation: Managed by the programmer (e.g., using free() in C).
  3. Memory Hierarchy

    • Registers: Fastest, smallest, and most expensive.
    • Cache: Faster than main memory, used to store frequently accessed data.
    • Main Memory (RAM): Primary storage for currently running processes.
    • Secondary Storage: Non-volatile storage like hard drives and SSDs.
  4. Memory Management Techniques

    • Contiguous Allocation: Each process is allocated a single contiguous block of memory.
    • Non-Contiguous Allocation: Memory is divided into fixed or variable-sized blocks (pages or segments).

Memory Allocation Strategies

  1. Fixed Partitioning

    • Memory is divided into fixed-sized partitions.
    • Each partition can hold exactly one process.
    • Pros: Simple to implement.
    • Cons: Inefficient use of memory, internal fragmentation.
  2. Dynamic Partitioning

    • Memory is divided into partitions dynamically based on process needs.
    • Pros: Better memory utilization.
    • Cons: External fragmentation.
  3. Paging

    • Memory is divided into fixed-size pages.
    • Processes are divided into pages of the same size.
    • Pros: Eliminates external fragmentation.
    • Cons: Can cause internal fragmentation.
  4. Segmentation

    • Memory is divided into segments based on logical divisions (e.g., code, data, stack).
    • Pros: Supports logical division of processes.
    • Cons: Can cause external fragmentation.

Paging and Segmentation

Paging

Paging is a memory management scheme that eliminates the need for contiguous allocation of physical memory. This avoids the problem of fitting varying sized memory chunks onto the backing store.

  • Page Table: A data structure used by the OS to keep track of the mapping between virtual addresses and physical addresses.
  • Page Frame: A fixed-size block of physical memory.

Example:

// Example of a simple page table entry structure in C
struct page_table_entry {
    unsigned int frame_number : 20; // Frame number in physical memory
    unsigned int present : 1;       // Page present in memory
    unsigned int rw : 1;            // Read/Write permission
    unsigned int user : 1;          // User/Supervisor mode
    unsigned int reserved : 9;      // Reserved bits
};

Segmentation

Segmentation divides the process into segments, which are logical units such as functions, arrays, and objects. Each segment has a base address and a length.

  • Segment Table: A table that contains the base address and length of each segment.
  • Segment Descriptor: Contains information about a segment, such as its base address, size, and access permissions.

Example:

// Example of a simple segment descriptor structure in C
struct segment_descriptor {
    unsigned int base_address; // Base address of the segment
    unsigned int limit;        // Length of the segment
    unsigned int type : 4;     // Segment type
    unsigned int s : 1;        // Descriptor type (0 = system, 1 = code/data)
    unsigned int dpl : 2;      // Descriptor privilege level
    unsigned int p : 1;        // Segment present
};

Practical Exercises

Exercise 1: Understanding Paging

Task: Given a virtual address and a page table, determine the corresponding physical address.

Virtual Address: 0x1234 Page Table: | Page Number | Frame Number | |-------------|--------------| | 0 | 5 | | 1 | 9 | | 2 | 3 | | 3 | 7 |

Solution:

  1. Convert the virtual address to binary: 0x1234 -> 0001 0010 0011 0100
  2. Split the address into page number and offset:
    • Page Number: 0001 (binary) -> 1 (decimal)
    • Offset: 0010 0011 0100 (binary) -> 564 (decimal)
  3. Look up the frame number in the page table: Page 1 -> Frame 9
  4. Combine the frame number with the offset to get the physical address:
    • Frame Number: 9 (decimal) -> 1001 (binary)
    • Physical Address: 1001 0010 0011 0100 (binary) -> 0x9234

Exercise 2: Implementing a Simple Page Table in C

Task: Write a C program to simulate a simple page table lookup.

#include <stdio.h>

#define PAGE_SIZE 1024 // Assume page size is 1024 bytes

// Page table entry structure
struct page_table_entry {
    unsigned int frame_number;
    unsigned int present;
};

// Function to translate virtual address to physical address
unsigned int translate_address(unsigned int virtual_address, struct page_table_entry *page_table, int num_pages) {
    unsigned int page_number = virtual_address / PAGE_SIZE;
    unsigned int offset = virtual_address % PAGE_SIZE;

    if (page_number >= num_pages || !page_table[page_number].present) {
        printf("Invalid virtual address!\n");
        return -1;
    }

    unsigned int frame_number = page_table[page_number].frame_number;
    unsigned int physical_address = frame_number * PAGE_SIZE + offset;

    return physical_address;
}

int main() {
    // Example page table with 4 entries
    struct page_table_entry page_table[4] = {
        {5, 1}, // Page 0 -> Frame 5
        {9, 1}, // Page 1 -> Frame 9
        {3, 1}, // Page 2 -> Frame 3
        {7, 1}  // Page 3 -> Frame 7
    };

    unsigned int virtual_address = 0x1234;
    unsigned int physical_address = translate_address(virtual_address, page_table, 4);

    if (physical_address != -1) {
        printf("Virtual Address: 0x%x -> Physical Address: 0x%x\n", virtual_address, physical_address);
    }

    return 0;
}

Explanation:

  • The translate_address function takes a virtual address, a page table, and the number of pages as input.
  • It calculates the page number and offset from the virtual address.
  • It checks if the page number is valid and if the page is present in memory.
  • It then calculates the physical address by combining the frame number and offset.

Summary

In this section, we covered the fundamental concepts of memory management, including memory allocation and deallocation, memory hierarchy, and various memory management techniques such as paging and segmentation. We also provided practical exercises to reinforce the concepts learned. Understanding memory management is crucial for optimizing system performance and ensuring efficient utilization of memory resources.

© Copyright 2024. All rights reserved