KEMBAR78
Lecture notes for Memory, Scheduler, Boot Sequence | PDF
4.1 Memory Management
INFO1112
Bob Kummerfeld
Programs in memory
When a program is ready to run as a
process it is stored in memory in a
particular layout:
stack
heap
static and global data
program code instructions
The stack is for temporary variables.
The heap is for data structures.
address 0
Programs in memory
When a program is ready to run as a
process it is stored in memory in a
particular layout:
stack
heap
static and global data
program code instructions
The stack is for temporary variables.
The heap is for data structures.
address 0
...
The complete physical memory
processes
Memory Mapping
The problem with this is that there are many processes in the memory at the same
time. How can they all be located at address zero?
This is achieved by hardware in the CPU that remaps the address space for each
process when it is running.
For example, if a process memory image is stored starting at real address 1000,
then the kernel of the operating system can use privileged instructions to set a
special register that says "all references to addresses are offset by 1000" that is, a
reference to address 0 will actually refer to address 1000.
In addition, there is a register that puts an upper limit on the addresses the
process can use. This prevents the process code from accessing beyond its
allotted memory area.
...
last address
start address
mapped to 0
for the
process
Virtual Memory
This simple memory mapping scheme was the early approach to memory
management and protection. Now we use a more sophisticated (and complex)
scheme that involves remapping smaller blocks of memory called "pages".
There is a table in the system that contains an entry for every page of the process.
This table maps the virtual address of the page used by the process to the
physical address in the main memory or an indication that the page is currently on
mass storage. This is known as the page table.
The kernel manages this table and moves pages to and from mass storage.
Page Table Example
Process
ID
Process
(Virtual)
Address
Physical
Memory
Address
Mass
Storage
Address
1456 100000 12000000 0
1456 120000 0 32100
... ... ... ...
... ... ... ...
By Ehamberg - Own work, CC BY-SA 3.0,
https://commons.wikimedia.org/w/index.php?curid=8352077
address zero (virtual)
mapping done via page table
address zero (physical)
pages mapped to mass storage
Address space for one process
Virtual Memory
What happens if a program attempts to use memory that the page table has
mapped to mass storage?
In that case, the access will cause an interrupt that will transfer control to the
kernel. An interrupt is like a forced function call to the kernel that occurs between
two instructions in such a way that the CPU state is saved and can be restored
when the kernel restarts the process. In this way the process has no indication
that it was interrupted.
When the kernel gains control, it will find the page on disk (stored in the swap
area) and read it in to a free part of main memory. Then setup up the page table to
point to the new physical location and return to the process.
Virtual Memory
If the kernel needs to read in a page from disk and discovers there is no free
physical memory available, it has to first write an existing block out to the swap
area to free up the memory.
If this happens too frequently, the system will become very slow and inefficient.
This is because the mass storage is much much slower than RAM and it takes a
long time to position a disk and read the page into memory.
Virtual Memory
Virtual memory has a number of neat optimisations.
● "shared memory" means that some processes have pages that are mapped
to the same physical memory. In this way they can either share readonly data,
or readonly code such as library functions. Or share writable memory for
sharing data (careful!).
● "copy on write" means that when a process forks, it shares physical memory
pages between the processes until one or other wants to change (write) a
page. At that point a writable copy is made.
Summary
In this segment we covered:
memory layout of processes
simple memory mapping
virtual memory
paging and page tables
paging/swapping to mass storage

Lecture notes for Memory, Scheduler, Boot Sequence

  • 1.
  • 2.
    Programs in memory Whena program is ready to run as a process it is stored in memory in a particular layout: stack heap static and global data program code instructions The stack is for temporary variables. The heap is for data structures. address 0
  • 3.
    Programs in memory Whena program is ready to run as a process it is stored in memory in a particular layout: stack heap static and global data program code instructions The stack is for temporary variables. The heap is for data structures. address 0
  • 4.
    ... The complete physicalmemory processes
  • 5.
    Memory Mapping The problemwith this is that there are many processes in the memory at the same time. How can they all be located at address zero? This is achieved by hardware in the CPU that remaps the address space for each process when it is running. For example, if a process memory image is stored starting at real address 1000, then the kernel of the operating system can use privileged instructions to set a special register that says "all references to addresses are offset by 1000" that is, a reference to address 0 will actually refer to address 1000. In addition, there is a register that puts an upper limit on the addresses the process can use. This prevents the process code from accessing beyond its allotted memory area.
  • 6.
  • 7.
    Virtual Memory This simplememory mapping scheme was the early approach to memory management and protection. Now we use a more sophisticated (and complex) scheme that involves remapping smaller blocks of memory called "pages". There is a table in the system that contains an entry for every page of the process. This table maps the virtual address of the page used by the process to the physical address in the main memory or an indication that the page is currently on mass storage. This is known as the page table. The kernel manages this table and moves pages to and from mass storage.
  • 8.
    Page Table Example Process ID Process (Virtual) Address Physical Memory Address Mass Storage Address 1456100000 12000000 0 1456 120000 0 32100 ... ... ... ... ... ... ... ...
  • 9.
    By Ehamberg -Own work, CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=8352077 address zero (virtual) mapping done via page table address zero (physical) pages mapped to mass storage Address space for one process
  • 10.
    Virtual Memory What happensif a program attempts to use memory that the page table has mapped to mass storage? In that case, the access will cause an interrupt that will transfer control to the kernel. An interrupt is like a forced function call to the kernel that occurs between two instructions in such a way that the CPU state is saved and can be restored when the kernel restarts the process. In this way the process has no indication that it was interrupted. When the kernel gains control, it will find the page on disk (stored in the swap area) and read it in to a free part of main memory. Then setup up the page table to point to the new physical location and return to the process.
  • 11.
    Virtual Memory If thekernel needs to read in a page from disk and discovers there is no free physical memory available, it has to first write an existing block out to the swap area to free up the memory. If this happens too frequently, the system will become very slow and inefficient. This is because the mass storage is much much slower than RAM and it takes a long time to position a disk and read the page into memory.
  • 12.
    Virtual Memory Virtual memoryhas a number of neat optimisations. ● "shared memory" means that some processes have pages that are mapped to the same physical memory. In this way they can either share readonly data, or readonly code such as library functions. Or share writable memory for sharing data (careful!). ● "copy on write" means that when a process forks, it shares physical memory pages between the processes until one or other wants to change (write) a page. At that point a writable copy is made.
  • 13.
    Summary In this segmentwe covered: memory layout of processes simple memory mapping virtual memory paging and page tables paging/swapping to mass storage