Memory Management, Operating Systems Part 1

Memory Management, Operating Systems Part 1

Memory Management in Operating Systems

Introduction to Memory Management

  • This video discusses memory management in operating systems, focusing on concepts from the textbook "Operating System Concepts" by Silberschatz.
  • The presenter will cover topics such as paging, segmentation, and related algorithms, with numerical examples reserved for a subsequent video.

Paging and Segmentation Overview

  • The discussion begins with an explanation of how processes are moved to and from memory through memory allocation techniques like segmentation and paging.
  • Real-world applications of these concepts will also be explored throughout the presentation.

Base and Limit Registers

  • Each process operates within its own address space, which is crucial for security; unauthorized access to another process's memory can lead to vulnerabilities.
  • The base register indicates the starting address of a process's allocated memory, while the limit register defines the maximum accessible range. Accessing outside this range results in a memory fault.

Address Space Management

  • A process assumes it is the only one running in memory, leading to logical addresses that start at zero up to a defined limit based on base plus limit values. This ensures isolation between processes.
  • Before accessing any requested address, checks against both base and limit registers are performed; failure leads to traps or errors indicating invalid access attempts.

Program Execution Flow

  • The execution flow starts with source code (e.g., C programming), which is compiled into an object module stored on disk before being linked into an executable module ready for loading into memory.

Process Management in Operating Systems

Understanding Process Allocation and Memory Management

  • A process is allocated an address in memory after being loaded, with base and limit registers defining its accessible range.
  • Dynamic libraries, such as DLLs in Windows, are involved during the loading step of a program into memory.
  • The operating system recognizes the loaded program as a running process, which can be in various states: running, waiting, or terminated.
  • The status of a process can change based on conditions like file access requests and locks that may lead to waiting states.
  • Executing a process utilizes system resources allocated by the operating system.

Logical vs. Physical Addresses

  • Logical addresses are visible to processes while physical addresses represent actual locations in memory managed by the OS.
  • Processes operate within their logical address space without awareness of their physical location assigned by the OS.
  • The relocation register helps translate logical addresses to physical addresses for CPU processing.
  • When calculating physical addresses, an offset from the logical address is added to the base address defined for that process.

Swapping Processes Between Memory and Disk

  • Swapping involves moving processes between main memory (faster but limited space) and secondary storage (slower but larger capacity).
  • When swapping out a process, it is moved from main memory to disk; conversely, swapping in brings it back into active memory.
  • The management of limited main memory requires careful handling of processes through swapping strategies implemented by the OS.
  • Terminated processes are removed from memory along with their control blocks when swapped out.

Context Switching and Its Costs

  • Context switching refers to changing CPU focus from one process to another; this incurs time costs associated with managing these transitions.

Memory Management in Operating Systems

Understanding Memory Operations

  • The operating system performs various operations, including swap in and swap out, which are crucial for memory management.
  • The concept of limit registers is introduced, where the base register is used to check if a process exceeds its allocated memory space.

Fragmentation Issues

  • When processes terminate, they can leave behind fragments or "holes" in memory that may not be usable for new processes.
  • These holes can vary in size; some may be too small to accommodate new processes, leading to inefficient memory usage.
  • As more processes are terminated, the number of holes increases while their sizes decrease, creating a fragmentation problem.

Solutions to Fragmentation

  • External fragmentation occurs when free spaces are scattered throughout memory. Efficient management strategies are needed to address this issue.
  • One costly solution involves moving all processes together to consolidate free space at one end of the memory but is inefficient due to time consumption.

Allocation Strategies

  • Different algorithms exist for selecting holes for new processes:
  • First Fit: Places a process in the first available hole that fits its size. This method starts searching from the beginning of memory.
  • Best Fit: Chooses the hole that leaves the least amount of leftover space after allocation. It aims for minimal fragmentation by evaluating all holes before placing a process.
  • Worst Fit: Opposite of Best Fit; it places a process into the largest available hole, potentially leaving larger gaps unutilized later on.

Summary of Allocation Algorithms

  • Each allocation strategy has its pros and cons:
  • First Fit is simple and quick but may lead to fragmentation over time.
  • Best Fit minimizes leftover space but can be slower as it requires scanning all holes.

Memory Management: Fragmentation and Segmentation

Understanding Fragmentation

  • The concept of "worst fit" assumes that there will be space available after placing a process in memory, while "best fit" assumes no additional processes will occupy the leftover space.
  • External fragmentation refers to unusable memory fragments between processes, whereas internal fragmentation deals with allocated but unused space within a process's address space.

Solutions to Fragmentation

  • Compaction or defragmentation is proposed as a solution for both types of fragmentation. This involves moving processes together to consolidate free memory spaces.
  • While compaction is effective, it is costly; thus, alternative methods like segmentation and paging are preferred for managing memory efficiently.

Segmentation Explained

  • Segmentation divides a program into logical units (e.g., stack, symbol table), allowing related memory usages to be placed together in consecutive memory spaces.
  • Each module of a process is allocated as a whole unit rather than being split across different locations in memory.

Optimizing Memory Usage

  • If small holes exist within segments that cannot accommodate new processes, the OS can divide these segments into smaller parts for better utilization of available memory.
  • The operating system must track all segments in physical memory to ensure accurate access when a process requests data from its modules.

Address Translation Mechanism

  • Logical addresses need conversion to physical addresses by the operating system. This requires maintaining segment tables that map logical addresses to their corresponding physical locations.

Understanding Segmentation and Paging in Memory Management

Segmentation Basics

  • The logical address being accessed is in the second segment with an offset value of 50. To find the physical address, one must convert using the limit and base values.
  • A segment table is utilized where a segment number helps convert limit and base registers. This process checks for memory faults or errors based on the operating system's approval of the limit.

Paging Fundamentals

  • In paging, fixed-size pages are used to divide physical memory into frames. For example, if each page is 1 MB, physical memory will be segmented accordingly.
  • When accessing a logical address, the CPU reads a page number from the request, searches it in the page table to find its corresponding frame number, and concatenates this frame number with an offset.

Differences Between Segmentation and Paging

  • Segmentation allows for flexible segment sizes while paging uses fixed sizes. Each process can have multiple pages leading to different page tables containing corresponding frame numbers.
  • In segmentation, addition is performed between base values and offsets; however, in paging, concatenation occurs instead of summation.

Example Scenario: Logical Address Conversion

  • An example illustrates a logical memory with four pages mapped to their respective frame numbers. Accessing these requires jumping to specific frames based on page numbers.
  • If accessing logical address 150 (which falls within page one), it translates through its mapping in the page table to frame four. The offset calculation leads to determining that physical address 450 corresponds to this access.

Conclusion of Concepts Discussed