Operating Systems 2026F Lecture 18: Difference between revisions
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>" |
|||
| (One intermediate revision by the same user not shown) | |||
| 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> | ||
| Line 15: | Line 151: | ||
<pre> | <pre> | ||
Lecture 18B | |||
----------- | |||
Topics | |||
- virtual memory | |||
- page replacement algorithms | |||
- working sets | |||
- containers vs virtual machines | |||
With virtual memory, every memory access from userspace is to a | |||
virtual address | |||
- must be translated to a physical address on each access | |||
page tables: data structure mapping virtual to physical addresses | |||
- maps the top bits of the address (top 20 bits on 32-bit systems) | |||
- lower bits are copied, are the offset into the page | |||
(4K, 8K, so 12+ bits) | |||
- implemented as a very wide tree (fan out of 1024 with 32-bit | |||
systems) | |||
- page directory, page table on 32 bit systems | |||
- more levels for 64 bit | |||
How does the kernel decide what to place in RAM? | |||
- in general, there can be more demand for RAM than is available, | |||
so physical pages must be carefully managed | |||
Why don't we just load all of a process's code and data into memory? | |||
- it isn't needed generally! | |||
Basic idea is the kernel is mostly lazy when it comes to memory management | |||
- doesn't reserve memory when asked, just allocates when absolutely necessary | |||
But this means the kernel can overcommit memory | |||
- made promises that can't all be kept at the same time | |||
How do processes allocate memory? | |||
- system calls! | |||
- sbrk, mmap | |||
So what happens when the bill comes due? | |||
- out of memory (oom) killer <--- last resort | |||
- swap! | |||
The swap space (or page file) is where the kernel can store | |||
data that is normally in RAM | |||
- old systems would literally "swap" processes between | |||
disk storage and RAM | |||
- today processes are "paged" to disk, i.e., only portions | |||
of their address space end up there | |||
Working set of a program | |||
- code & data the program needs to make progress | |||
- if a process doesn't keep the working set available, | |||
performance suffers | |||
- note that a working set operates at multiple scales, | |||
depending on the speed of the storage | |||
working sets are based on the ideas that programs exhibit locality | |||
- temporal locality: if the memory has been accessed recently | |||
it will get accessed again in the near future | |||
- spatial locality: if memory has been accessed, bytes nearby | |||
are likely to be accessed | |||
So what is the best we can do for page management? | |||
- know the future! | |||
This is how we benchmark page replacement algorithms | |||
- get a trace of what pages a program needed when | |||
- see how close your algorithm gets to this optimal pattern | |||
We can't predict the future, so what do we do instead? | |||
- least recently used | |||
- guess: the page that was used the longest time ago | |||
is the one we are least likely to need now | |||
To implement LRU, we'd ideally need a timestamp for every page | |||
- which told us when it was last used | |||
- but that would be too much bookkeeping, so need to approximate | |||
We do have some bits to keep track of each page: the extra | |||
bits in the page table entries (12 bits on 32-bit systems) | |||
- we only need 2 to keep track of the page state | |||
What kind of pages do we have? | |||
- code, data pages | |||
- code pages are normally read only, but can be executed | |||
- data pages are read write, but normally cannot be executed | |||
If we need to kick code or data out of RAM, what do we need to do first? | |||
- make sure we have another copy! | |||
With code, we almost always already have a copy - in the binary | |||
in the filesystem | |||
But with data, what's in memory is generally our only copy unless we write to swap | |||
Writing to swap can happen in the background, but if we're waiting on a page to be read from swap, program execution will be slowed down | |||
When a page in RAM is our only copy, it is said to be "dirty"\ | |||
- we "clean" it by writing a copy to disk | |||
So a page replacement algorithm will look at two properties of every page | |||
- when it was last accessed | |||
- is it dirty | |||
Classic page replacement algorithms are implemented as a "two handed clock" | |||
- face of the clock is the circular array of pages | |||
(loop around) | |||
- fast hand goes through memory setting the accessed bit to 0 | |||
- access bit gets set to 1 every time the page is accessed | |||
- slow hand tries to clear dirty bits, but only for | |||
pages that haven't been recently accessed | |||
The slow hand depends on there being swap | |||
- and not all systems have swap! | |||
Why not? | |||
- because flash memory isn't really good for swap | |||
So how do smartphones deal with not having swap? | |||
- apps have to be ready to be terminated at any time | |||
So how can memory be shared? | |||
- mainly with mmap | |||
- used mostly for dynamic libraries | |||
Dynamic linking is less used nowadays because of containers & virtual machines | |||
So what is the difference between containers & VMs? | |||
With virtual machines | |||
- many kernels, one for each virtual machine | |||
- each kernel has its own page tables for its set of processes | |||
- while CPUs are shared, RAM is generally partitioned | |||
between VMs (not shared) | |||
- to share, we need deduplication mechanisms | |||
With containers | |||
- only one kernel | |||
- only one set of page tables | |||
- lots of opportunities of share RAM | |||
- esp with shared files | |||
- but sharing depends on how files are being stored | |||
But why don't containers do so much dynamic linking? | |||
- because they are already stripped down | |||
</pre> | </pre> | ||
Latest 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
Lecture 18B
-----------
Topics
- virtual memory
- page replacement algorithms
- working sets
- containers vs virtual machines
With virtual memory, every memory access from userspace is to a
virtual address
- must be translated to a physical address on each access
page tables: data structure mapping virtual to physical addresses
- maps the top bits of the address (top 20 bits on 32-bit systems)
- lower bits are copied, are the offset into the page
(4K, 8K, so 12+ bits)
- implemented as a very wide tree (fan out of 1024 with 32-bit
systems)
- page directory, page table on 32 bit systems
- more levels for 64 bit
How does the kernel decide what to place in RAM?
- in general, there can be more demand for RAM than is available,
so physical pages must be carefully managed
Why don't we just load all of a process's code and data into memory?
- it isn't needed generally!
Basic idea is the kernel is mostly lazy when it comes to memory management
- doesn't reserve memory when asked, just allocates when absolutely necessary
But this means the kernel can overcommit memory
- made promises that can't all be kept at the same time
How do processes allocate memory?
- system calls!
- sbrk, mmap
So what happens when the bill comes due?
- out of memory (oom) killer <--- last resort
- swap!
The swap space (or page file) is where the kernel can store
data that is normally in RAM
- old systems would literally "swap" processes between
disk storage and RAM
- today processes are "paged" to disk, i.e., only portions
of their address space end up there
Working set of a program
- code & data the program needs to make progress
- if a process doesn't keep the working set available,
performance suffers
- note that a working set operates at multiple scales,
depending on the speed of the storage
working sets are based on the ideas that programs exhibit locality
- temporal locality: if the memory has been accessed recently
it will get accessed again in the near future
- spatial locality: if memory has been accessed, bytes nearby
are likely to be accessed
So what is the best we can do for page management?
- know the future!
This is how we benchmark page replacement algorithms
- get a trace of what pages a program needed when
- see how close your algorithm gets to this optimal pattern
We can't predict the future, so what do we do instead?
- least recently used
- guess: the page that was used the longest time ago
is the one we are least likely to need now
To implement LRU, we'd ideally need a timestamp for every page
- which told us when it was last used
- but that would be too much bookkeeping, so need to approximate
We do have some bits to keep track of each page: the extra
bits in the page table entries (12 bits on 32-bit systems)
- we only need 2 to keep track of the page state
What kind of pages do we have?
- code, data pages
- code pages are normally read only, but can be executed
- data pages are read write, but normally cannot be executed
If we need to kick code or data out of RAM, what do we need to do first?
- make sure we have another copy!
With code, we almost always already have a copy - in the binary
in the filesystem
But with data, what's in memory is generally our only copy unless we write to swap
Writing to swap can happen in the background, but if we're waiting on a page to be read from swap, program execution will be slowed down
When a page in RAM is our only copy, it is said to be "dirty"\
- we "clean" it by writing a copy to disk
So a page replacement algorithm will look at two properties of every page
- when it was last accessed
- is it dirty
Classic page replacement algorithms are implemented as a "two handed clock"
- face of the clock is the circular array of pages
(loop around)
- fast hand goes through memory setting the accessed bit to 0
- access bit gets set to 1 every time the page is accessed
- slow hand tries to clear dirty bits, but only for
pages that haven't been recently accessed
The slow hand depends on there being swap
- and not all systems have swap!
Why not?
- because flash memory isn't really good for swap
So how do smartphones deal with not having swap?
- apps have to be ready to be terminated at any time
So how can memory be shared?
- mainly with mmap
- used mostly for dynamic libraries
Dynamic linking is less used nowadays because of containers & virtual machines
So what is the difference between containers & VMs?
With virtual machines
- many kernels, one for each virtual machine
- each kernel has its own page tables for its set of processes
- while CPUs are shared, RAM is generally partitioned
between VMs (not shared)
- to share, we need deduplication mechanisms
With containers
- only one kernel
- only one set of page tables
- lots of opportunities of share RAM
- esp with shared files
- but sharing depends on how files are being stored
But why don't containers do so much dynamic linking?
- because they are already stripped down