Operating Systems 2026F Lecture 17
Video
Video from the lectures given on November 19th and 20th, 2026 are now available:
Notes
Lecture 17A
Lecture 17A
-----------
Scheduling
The problem: what should the kernel run next?
- after a system call
- after handling an interrupt
(request from hardware)
Running processes are like a todo list for the kernel
- who deserves attention next
- which will cause problems if it doesn't get attention (resources) next?
So it is just managing a queue
- but where we are only giving limited resources at any time
to a process or thread
Why limited resources? because preemption
- everyone only gets a slice (like 1/100 of a second or less)
- most programs will need more than one time slice to finish their work
With standard queues, there are a few strategies
- first come, first serve (FCFS)
- normally each "served" runs to completion
- not useful for interactive systems, but
(may be) good for batch systems
- order by length of job (process)
- shortest job first
- longest job first
But once you add preemption, everyone can take turns
- round robin!
Round robin is fair, but not necessarily what we want
- because not all processes are equally important
- interactive tasks <-- someone is paying attention
- "important" for whatever other reason
How to add priorities to round robin?
- multiple queues!
- do round robin between queues in some way
But allocating resources is a hard problem, no fixed perfect solution
- depends on the nature of the workload
One wrinkle that is very important nowadays: power usage
- how do you schedule taking into account power usage?
- especially when some cores take more power than others?
Smartphones typically deal with scheduling in a bit of a brutal way
- background tasks are killed quickly
Why do we care about background tasks on a phone?
- notifications (push)
- updates (pull)
To minimize background traffic, Google and Apple maintain a dedicated notification service
- you have to go through it to get messages to a device when an app isn't running
Priorities and process states
Processes can be in various states:
R = running (can have as many processes in R as you have cores)
Virtual memory
--------------
Any computer has a fixed amount of RAM
- caches are managed automatically by hardware (generally)
- but main memory is managed by the operating system
RAM is fast for random access and is volatile
- loses its contents when power is lost
For PCs in the 1980's (and larger computers in the 1960's), one program would run at a time, and it had complete control over the computer
- so naturally it got all of the RAM and CPU time
To share the CPU, we just need interrupts
- some way to kick the currently running program off the CPU
But how can we share RAM?
- it is a dynamic resource management problem
What mechanisms can we use to make sharing easier?
- could allocate ranges of memory to different programs
(this is segmented memory)
Managing memory with variable-sized chunks, though, is very painful
- because you get fragmentation
.................... <-- 20 chunks
AAAAABBBBBCCCCCDDDDD <- 4 programs, each get 5
AAAAA.....CCCCC..... <- B and D terminate
Now let's say A wants 6 more chunks of memory
We have 10 chunks available, but there's no room for six all together!
And you want memory all together for data structures, generally
So how to avoid fragmentation? Add a level of indirection!
Programs don't use physical addresses (addresses that refer to real RAM)
- instead, they use virtual addresses (which are made up)
At runtime, every virtual address must be translated to a physical address before being used
So CPUs support a data structure known as a page table
- maps virtual to physical addresses
- referenced in principle on every memory lookup, but
results are cached (in the TLB)
What is being cached in the TLB is page table entries (PTEs)
What's a page table entry?
(why 32 bits? because the math is nicer!)
32bit virtual address -> 32 bit physical address
But not efficient to map on a byte to byte level. So let's map chunks. How big? let's say 4K
Mapping 4K -> 4K chunks means
- part of the address refers to the chunk
- part refers to the portion of the chunk
The chunk is a page.
- number of the chunk: page number
- portion of the chunk: page offset
Virtual address
.......... .......... ............ <-- 32 bits
10 bits 10 bits 12 bits
Physical address
.................... ............ <-- 32 bits
page # page offset
So we divide all of physical RAM into 4K pages
And we do the same for process virtual memory
32 bit addresses => max 4G RAM
The offset portion of the virtual address gets copied to the physical address
- last 12 bits are easy!
But how do I map 20 bits to 20 bits?
An array would be very big
But what about a fat tree?
4096 / 4 = 1024
So I have 1024 PTEs I can fit into a single page
- yes I have 12 extra bits for each but we need those
for metadata
How many bits do I need to represent 1024 possibilities?
=> 10 bits!
So your page table is a two level tree, with a 1024 fan out
- very wide tree!
And those each point to up to 1024 2nd-level nodes
And those point to actual pages with data
Lecture 17B
Lecture 17B ----------- Scheduling - what process to run next? Scheduling happens when we exit the kernel We can enter the kernel in a few ways, but we only leave through the scheduler Entry from - system calls - hardware interrupts (many kinds) - or, just running always (kernel threads) When we exit the kernel, have to decide what the next task is to run (process or thread) Which do we run? - could be the last process/thread that was running, but what if there wasn't one? - also, how would other tasks get a chance to run? Scheduling algorithms - MANY approaches - in the real world, algorithms are supplemented by heuristics, gets very complex Scheduling gets hard because we have to consider multiple dimensions when determining what to next run - fairness - responsiveness (to users) - power consumption power consumption gets really weird when you have heterogenous cores Scheduling on smartphones gets really annoying - and smartphones have made compromises to make scheduling easier In particular, background tasks are built to be disposable on mobile operating systems - the kernel will kill and restart them arbitrarily - so if you want a long-lived background process, you gotta do something different (generally, use an OS-provided service) - key example: notifications Why can't you just have a process run in the background on a smartphone to listen to network messages? - because that consumes more battery life - and the OS won't let it! First, consider job-oriented scheduling - where each process is a "job" that runs to completion - no preemption Approaches: - first come, first serve - shortest job first - longest job first - priority Preemptive scheduling uses these ideas, but in the context of multitasking first come first serve => round robin - think of everyone in a circle - everyone gets their turn How do I do priorities? - have multiple queues! But who sets the priorities? - static (by the user/admin) - dynamic (based on circumstances, history) Virtual memory Problem: how do we manage RAM that is shared between running programs? - OS is responsible for main memory (DRAM) - hardware handles caches (SRAM), e.g. L1, L2, L3 How to organize memory to make it easier to manage? - need to divide into chunks Should those chunks be fixed or variable sized? - variable is useful for having semantically-related memory together, this is what we get with segments (see ELF) - but leads to greater memory fragmentation .................... AAAAABBBBBCCCCCDDDDD AAAAA.....CCCCC..... What if a new process E wants 7 units of memory? - can we allocate it contiguously? NO Virtual memory is a solution to the contiguous memory problem - everyone can have as much contiguous memory as they want! The price: every memory access must be translated (from a virtual address to a physical address) How can we make it fast? by caching it! - the cache is called the TLB But what is the TLB caching? page table entries (PTEs) So what's the page table? First, what's a page? - fixed sized chunk of memory, typically 4K or 8K So all memory allocation is in terms of pages - what if we don't need a full page? rest of it gets wasted (internal fragmentation) When you have a page that is a power of two size, virtual->physical memory translation gets split into two parts * page numbers must be mapped from virtual to physical * page offsets can just be copied 32 bit virtual address => 32 bit physical address How many bits for an offset into a 4K page? 12, leaving 20 for the page number How many 32 bit values can we fit into a page? - 1024 We want 32 bit page table entries - 20 bits for the page number (top bits of a pointer) - 12 bits for metadata So then we just need to access two pages of page table entries to get to the real page So the 32 bit page table is a tree with three levels - page directory, page tables, and the actual pages - with the fan out of the nodes being 1024 So now how do we manage those pages? - what do we need to keep in memory? Trick: we can be lazy! Consider mmap - when a file is mapped into memory, we don't have to load it all at once: we can load on demand!