Basic Concepts

Fundamentals of Memory Management

Memory management is one of the core functions of an operating system. It manages the computer’s main memory, allocating memory space to processes and reclaiming it when it is released, and making sure that memory is used safely.

Program Loading and Linking

The Program Loading Process

The process of bringing a program from secondary storage into memory is called program loading. It consists of the following steps:

  1. Compile stage: compile the source code into object code
  2. Link stage: link several object modules into an executable program
  3. Load stage: load the executable program into memory

Linking Modes

1. Static Linking

// Link all library functions at compile time
gcc -static program.c -o program
  • Advantage: fast execution, no dependency on external libraries
  • Disadvantage: a large executable and low memory utilization

2. Dynamic Linking

// Link library functions at run time
gcc program.c -o program
  • Advantage: saves memory and makes library updates easy
  • Disadvantage: slightly slower execution, depends on external libraries

3. Run-Time Dynamic Linking

// Load the library on demand while the program is running
dlopen("libmath.so", RTLD_LAZY);

Loading Modes

1. Absolute Loading

  • The program is loaded at a specified absolute address
  • Suitable for single-program systems
  • Addresses are fixed at compile time

2. Relocatable Loading

  • The program is loaded at any location in memory
  • Requires a relocation register
  • Supports multiprogramming

3. Dynamic Run-Time Loading

  • The load location is decided only while the program is running
  • Supports virtual memory management
  • Address translation is deferred until the access happens

Logical and Physical Address Spaces

Address Space Concepts

Logical address space:

  • Addresses generated by the CPU
  • A contiguous address space that starts at 0
  • The addresses a programmer sees

Physical address space:

  • The actual addresses of the memory cells
  • Possibly non-contiguous
  • The addresses the hardware sees

The Address Translation Process

Logical Address
Address Translation Unit
Physical Address

Address translation mechanisms:

  1. Relocation register: a base register
  2. Page table: the address mapping in a paging system
  3. Segment table: the address mapping in a segmentation system

Address Translation Examples

Base relocation:

// Logical address: 1000
// Base register: 5000
// Physical address = logical address + base = 1000 + 5000 = 6000

Paged address translation:

// Logical address: 0x1234
// Page number: 0x12, page offset: 0x34
// Look up the physical frame number in the page table
// Physical address = frame number x page size + page offset

Memory Protection

Protection Mechanisms

1. Base and Limit Register Protection

// Base register: 5000
// Limit register: 2000
// Valid address range: [5000, 7000)
if (address < base || address >= base + limit) {
    // Address out of bounds, raise a protection fault
    raise_protection_fault();
}

2. Page Table Protection Bits

typedef struct {
    int frame_number;    // physical frame number
    int valid;          // valid bit
    int read;           // read permission
    int write;          // write permission
    int execute;        // execute permission
} PageTableEntry;

3. Segment Table Protection Bits

typedef struct {
    int base;           // segment base address
    int limit;          // segment length
    int valid;          // valid bit
    int read;           // read permission
    int write;          // write permission
    int execute;        // execute permission
} SegmentTableEntry;

Glossary

TermAbbreviationExplanation
Memory Management-Allocation, reclamation, and protection of memory
Logical Address-The address used by the program
Physical Address-The address in the real memory
Address Translation-The mapping from a logical to a physical address
Relocation-Address adjustment when a program is loaded
Static Relocation-Address translation done at load time
Dynamic Relocation-Address translation done at run time
Program Loading-Bringing a program into memory
Program Linking-Linking object modules together
Static Linking-Linking completed before loading
Dynamic Linking-Linking at load time or at run time
Run-Time Dynamic Linking-Linking on demand while the program runs
Memory Protection-Preventing a process from reaching out of range
Base Register-Holds the start address of the process
Limit Register-Holds the size of the process address space
Base and Bound Register-The base register together with the limit one
Storage Protection Key-A memory protection mechanism
Swapping-Moving a process between memory and disk
Compaction-Moving processes to merge the free areas
Fragmentation-Small memory blocks that cannot be used
Internal Fragmentation-Waste inside an allocation unit
External Fragmentation-Waste outside an allocation unit
Virtual Address Space-The logical address space of a process

Found an issue with this content?

Tell us what could be improved.