Virtual Memory III

Created

Linux

Big Picture

  • VM is a close co-operation between hardware and software.
  • Each process has its own vitual address space, defined by a page table.
  • Translation look-up buffer.
    • When the OS kernel needs to switch from one process to another, you can flush the TLB (translation lookahead buffer).
  • Page Table
    • When switching process, update the pointer to the top-level page table.
  • Page table are always in physical memory, so the pointers in the page table do not require translation.

Handling Page Fault

page faults trigger an exception (hardware) exception is handled by software (Linux kernel), and this kernel must determine what to do.

Memory Mapping

Objects on Disk

Area of virtual memory = file on disk.

Regular file in file system is divided up into pages.

  • demand loading: just mapped to addresses, not actually loaded
  • cxould be code, shared library, data file.

Anonymous file (e.g., general memory or heap)

  • typically allocated memory
  • when used for the first time: set all values to zero
  • never on disk, except when swapped out

Shared Object

A shared object is a file on a disk

Private Object

  • Only its processes can read/write
  • changes not visible to other processes.
  • This works because a copy is made, so the changes in the data mapping dont ever get written back to the file.

Shared object

  • Multiple process can read/write
  • Changes are visible to other processed.

Fork

Creates a new child process by copying all virtual memory area structures, memory mapping structures, and page tables.

New process has identical access to existing memory.

The child will read the same files as the parent, but when it has to write, it will make a copy. (copy-on-write).

Execve

execve() is a complete address space tear down. Used when you want a child to run a completely different program.

  • creates a new process
  • deletes all user areas
  • map private areas (.data, .code, .bss)
  • maps shared libraries
  • set program counter

User-Level Memory Mapping

Processes can create virtual memory areas with mmap Protection options (handled by kernel/hardware): executable code, read, write, inaccessible

Mapping options: anonymous: data object initailly zerroed out, private, shared.

Dynamic Memory Allocation

In C, you need to track a list of allocated memory.

  • malloc() allocates a specified amount of data.
    • return pointer to (virtual) address
    • memory is alloced on heap
  • free() frees memory allocated at pointer location.
    • may be between other allocated memory

Fragmentation may occur. Internal: unused space due to padding for alginment or minimum blcok size. External: as memory is allocated and freed:

  • allocated blocks are scattered over the heap area
  • there are gaps of various sizes between allocated blocks.
  • it might not be possible to find a large enough gap to satisfy an allocation request, even though enough aggregate memory is available.

Free list maintains a list of free memory areas

  • Implicit: space between allocated memory
  • explicit: maintain a separate list.