<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://homeostasis.scs.carleton.ca/wiki/index.php?action=history&amp;feed=atom&amp;title=Operating_Systems_2026F_Lecture_17</id>
	<title>Operating Systems 2026F Lecture 17 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://homeostasis.scs.carleton.ca/wiki/index.php?action=history&amp;feed=atom&amp;title=Operating_Systems_2026F_Lecture_17"/>
	<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=Operating_Systems_2026F_Lecture_17&amp;action=history"/>
	<updated>2026-11-23T15:48:47Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.42.1</generator>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=Operating_Systems_2026F_Lecture_17&amp;diff=25270&amp;oldid=prev</id>
		<title>Soma: Created page with &quot;==Video==  Video from the lectures given on November 19th and 20th, 2026 are now available: * [https://homeostasis.scs.carleton.ca/~soma/os-2026f/lectures/comp3000-2026f-lec17a-20261119.mp4 Lecture 17A] * [https://homeostasis.scs.carleton.ca/~soma/os-2026f/lectures/comp3000-2026f-lec17b-20261120.mp4 Lecture 17B]  ==Notes==  ===Lecture 17A===  &lt;pre&gt; Lecture 17A -----------  Scheduling  The problem: what should the kernel run next?  - after a system call  - after handling...&quot;</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=Operating_Systems_2026F_Lecture_17&amp;diff=25270&amp;oldid=prev"/>
		<updated>2026-11-20T21:07:47Z</updated>

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