Operating Systems 2026F Lecture 18: Difference between revisions

From Soma-notes
Created page with "==Video== Video from the lectures given on November 24th and 25th, 2026 are now available: * [https://homeostasis.scs.carleton.ca/~soma/os-2026f/lectures/comp3000-2026f-lec18a-20261124.mp4 Lecture 18A] * [https://homeostasis.scs.carleton.ca/~soma/os-2026f/lectures/comp3000-2026f-lec18b-20261125.mp4 Lecture 18B] ==Notes== ===Lecture 18A=== <pre> </pre> ===Lecture 18B=== <pre> </pre>"
 
Line 10: Line 10:


<pre>
<pre>
Lecture 18A
-----------
Virtual memory (cont.)
In main memory we have a bunch of physical memory,
divided into pages.
Which processes should get which pages?
How does a process ask for more memory? via system calls of course
- note that all processes get a "full" address space
- but most of it will not be valid memory until memory is allocated
- allocated via sbrk or mmap
But what does the kernel do with these requests?
- because of the virtual->physical memory mapping, processes
  (mostly) don't care what pages they get
- so the kernel just has to keep track of what pages are available
  or not, and hand them out as needed
What happens when we run out of pages?
The kernel is optimistic in its allocation of memory
- and if that optimism isn't warranted, it may have to
  kill one or more processes (the oom killer)
On a smartphone, when it runs out of memory it will aggressively kill apps that aren't being used
- all apps have to be ready to be terminated at any time, and will be restarted when a user selects the app again
But aren't there other options?
What is swap?
- auxiliary storage (e.g., file on disk or partition)
  that is used to store data in RAM (physical memory)
  when we run out of RAM
Traditionally swap was for moving entire processes out to disk,
but nowadays it is more "paging" rather than "swapping", as only portions of processes are often moved to disk.
working set
- set of pages (code & data) needed for a process to make progress
  on whatever its work is
Why can we use working sets as a way to think about memory usage?
- because programs exhibit high degrees of locality
  (temporal and spatial)
- patterns of locality are different for code and data
  - caches are often divided into code & data for this reason
  - not used for virtual memory, swap
Example: Modern processors are carefully engineered to have very good branch predictors
We need algorithms that predict the future to decide what pages to keep in RAM and what to get rid of (to disk in some form)
What is the optimal page replacement strategy?
- one that can perfectly predict the future!
- used to benchmark page replacement algorithms
  (done retrospectively of course)
Mostly page replacement algorithms are some form of LRU: least recently used
- ideally, label each page with a timestamp that is
  updated when it was last used
Maintaining time stamps for every page access is infeasible
- too much data, too many accesses, would be too slow
- but we want to approximate this
So we approximate it with a couple of bits
- but where do we put the bits?
- in the page table entries!
But what bits do we want?
First need to classify pages in RAM
- data pages that are actively being read and written to
- code pages that are being read and executed
  (normally NOT written)
- code pages that are not being actively used
- data pages that are not being actively used
If I want to evict from RAM a data page, what do I do?
- save it (to swap)
- kill the process
What about code pages?
- kill the process
- or just kick the page out
  (because we already have a copy)
Pages have two key properties for virtual memory management:
- accessed (recently)
- dirty (only copy of that information)
So classic page management algorithms are often described as "two handed clocks"
- the clock face is the array of pages (treat it as a circular array)
- the fast hand marks accessed pages,
  i.e., marks the accessed bit to 0
- the slow hand cleans up dirty pages,
  i.e., writes any pages dirty to disk then marks them clean
In practice you only want to clean dirty pages that have not been recently accessed
But to get maximum performance you gotta take into account lots of special cases
- model the different patterns of memory usage
- can benefit from lots of tweaking
Note that many systems today run without swap!
- embedded devices/smartphone don't because
  flash memory only supports a limited number of writes
    - wear leveling only gets you so far
So how does virtual memory interact with virtual machines and containers?
Virtual machines
- each has its own kernel
- page tables for processes on one kernel are
  distinct from those on another kernel
- so, no shared pages between virtual machines
  unless there is deduplication code
  - which there often is now
  - but otherwise memory is just partitioned
    (so the sets of physical pages are not
      shared between virtual machines generally)
- CPUs (cores) are shared between virtual machines
  - but can be assigned to specific virtual machines
 
Containers
- all running on one kernel
- so page management is across all processes in all containers
- so is CPU time
So which has better isolation, virtual machines or containers?
- virtual machines!
Which is more efficient?
- containers!
</pre>
</pre>



Revision as of 16:52, 27 November 2026

Video

Video from the lectures given on November 24th and 25th, 2026 are now available:

Notes

Lecture 18A

Lecture 18A
-----------

Virtual memory (cont.)

In main memory we have a bunch of physical memory,
divided into pages.

Which processes should get which pages?

How does a process ask for more memory? via system calls of course
 - note that all processes get a "full" address space
 - but most of it will not be valid memory until memory is allocated
 - allocated via sbrk or mmap

But what does the kernel do with these requests?
 - because of the virtual->physical memory mapping, processes
   (mostly) don't care what pages they get
 - so the kernel just has to keep track of what pages are available
   or not, and hand them out as needed

What happens when we run out of pages?

The kernel is optimistic in its allocation of memory
 - and if that optimism isn't warranted, it may have to
   kill one or more processes (the oom killer)

On a smartphone, when it runs out of memory it will aggressively kill apps that aren't being used
 - all apps have to be ready to be terminated at any time, and will be restarted when a user selects the app again

But aren't there other options?

What is swap?
 - auxiliary storage (e.g., file on disk or partition)
   that is used to store data in RAM (physical memory)
   when we run out of RAM

Traditionally swap was for moving entire processes out to disk,
but nowadays it is more "paging" rather than "swapping", as only portions of processes are often moved to disk.

working set
 - set of pages (code & data) needed for a process to make progress
   on whatever its work is

Why can we use working sets as a way to think about memory usage?
 - because programs exhibit high degrees of locality
   (temporal and spatial)
 - patterns of locality are different for code and data
   - caches are often divided into code & data for this reason
   - not used for virtual memory, swap

Example: Modern processors are carefully engineered to have very good branch predictors

We need algorithms that predict the future to decide what pages to keep in RAM and what to get rid of (to disk in some form)

What is the optimal page replacement strategy?
 - one that can perfectly predict the future!
 - used to benchmark page replacement algorithms
   (done retrospectively of course)

Mostly page replacement algorithms are some form of LRU: least recently used
 - ideally, label each page with a timestamp that is
   updated when it was last used

Maintaining time stamps for every page access is infeasible
 - too much data, too many accesses, would be too slow
 - but we want to approximate this

So we approximate it with a couple of bits
 - but where do we put the bits?
 - in the page table entries!
But what bits do we want?

First need to classify pages in RAM
 - data pages that are actively being read and written to
 - code pages that are being read and executed
   (normally NOT written)
 - code pages that are not being actively used
 - data pages that are not being actively used

If I want to evict from RAM a data page, what do I do?
 - save it (to swap)
 - kill the process

What about code pages?
 - kill the process
 - or just kick the page out
   (because we already have a copy)

Pages have two key properties for virtual memory management:
 - accessed (recently)
 - dirty (only copy of that information)

So classic page management algorithms are often described as "two handed clocks"
 - the clock face is the array of pages (treat it as a circular array)
 - the fast hand marks accessed pages,
   i.e., marks the accessed bit to 0
 - the slow hand cleans up dirty pages,
   i.e., writes any pages dirty to disk then marks them clean

In practice you only want to clean dirty pages that have not been recently accessed

But to get maximum performance you gotta take into account lots of special cases
 - model the different patterns of memory usage
 - can benefit from lots of tweaking

Note that many systems today run without swap!
 - embedded devices/smartphone don't because
   flash memory only supports a limited number of writes
    - wear leveling only gets you so far

So how does virtual memory interact with virtual machines and containers?

Virtual machines
 - each has its own kernel
 - page tables for processes on one kernel are
   distinct from those on another kernel
 - so, no shared pages between virtual machines
   unless there is deduplication code
   - which there often is now
   - but otherwise memory is just partitioned
     (so the sets of physical pages are not
      shared between virtual machines generally)
 - CPUs (cores) are shared between virtual machines
   - but can be assigned to specific virtual machines
  
Containers
 - all running on one kernel
 - so page management is across all processes in all containers
 - so is CPU time

So which has better isolation, virtual machines or containers?
 - virtual machines!

Which is more efficient?
 - containers!

Lecture 18B