Memory Management Techniques

Without virtual memory 

  • In a multiprogramming environment, multiple processes are loaded into RAM to maximize CPU utilization and overall system efficiency. Let's consider an example where three processes, P1, P2, and P3, are allocated specific memory spaces in RAM. The operating system occupies the initial portion of RAM, followed by P1 in the memory range 50-60, P2 in the range 60-70, and P3 in the range 90-100.
  • Now, suppose P1 contains a line of code that attempts to access a memory location by adding 10 to the current address. If the current address is 55, this calculation would result in an address of 65, which falls within the memory space allocated to P2. This situation would violate memory protection and process isolation principles, as P1 should not be aware of or able to access the memory allocated to P2.
  • To prevent such violations, there must be mechanisms in place to ensure that each process operates within its allocated memory space without interfering with others.
With virtual memory

  • In a multiprogramming environment, the OS doesn't directly allocate processes to specific physical locations in RAM. Instead, it introduces a layer of abstraction between processes and physical memory to ensure memory protection and isolation.
  • For instance, suppose we have two processes, P1 and P2, each requiring 16KB of memory. In virtual memory, both P1 and P2 start their address space at 0, making them independent from each other. The OS then maps these virtual addresses to physical addresses in RAM. For example, P1's virtual address space from 0 to 16KB might be mapped to physical addresses 128 to 144, and P2's space might be mapped to addresses 64 to 80.
  • This abstraction allows each process to operate as if it has its own dedicated memory starting from address 0. However, behind the scenes, the OS manages the actual physical addresses. If, for example, a programmer in P2 tries to access an address that, when translated, falls outside its allocated range (e.g., 125, which is within P1's range), the OS detects this using the base (here 64) and offset values (here 16) for P2. The OS then throws an exception, indicating an illegal memory access attempt, thereby preventing any violation of process isolation.

The memory Management Unit (MMU) is a crucial component of the computer's hardware responsible for handling memory access and virtual memory management. The MMU translates virtual addresses used by programs into physical addresses used by the hardware. Components of the MMU:
  • Relocation Register (Base Register):  Holds the base address of the process’s allocated memory in physical memory.
  • Limit Register (Bounds Register): Specifies the size (offset) of the process’s allocated memory space.
1. Process Information:
  • Virtual Address Space: The process P1 has a virtual memory range from 0 to 10.
  • Base Value: The base address in physical memory is 50.
  • Limit Register: The limit register value is 10, indicating the maximum offset allowed in the process's address space.
2. CPU Generates Logical Address:
  • Logical Address: The CPU generates a logical address of 5.
3. Limit Register Check: 
  • The limit register is used to ensure that the logical address does not exceed the bounds of the process's allocated virtual memory, if the value exceeds the bound then the addressing error will be thrown.
  • In this case, the limit register value is 10, and the logical address 5 is less than 10. Therefore, the address is within the allowed range.
4. Address Translation: 
  • Base Value Addition: To convert the logical address to a physical address, the base value (50) is added to the logical address (5).
  • Physical Address Calculation:
  • Physical Address=Base Value+Logical Address
  • Physical Address=50+5=55
  • Accessing Physical Memory: The calculated physical address is 55. The CPU will access this address in RAM.




Comments

Popular posts from this blog

Indexed allocation

Thrashing

Basics of paging