<?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_2020W_Lecture_19</id>
	<title>Operating Systems 2020W Lecture 19 - 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_2020W_Lecture_19"/>
	<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=Operating_Systems_2020W_Lecture_19&amp;action=history"/>
	<updated>2026-06-02T22:26:50Z</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_2020W_Lecture_19&amp;diff=22611&amp;oldid=prev</id>
		<title>Soma: Created page with &quot;==Video==  The video from the lecture given on March 20, 2020 [https://homeostasis.scs.carleton.ca/~soma/os-2020w/lectures/comp3000-2020w-lec19-20200320.m4v is now available]....&quot;</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=Operating_Systems_2020W_Lecture_19&amp;diff=22611&amp;oldid=prev"/>
		<updated>2020-03-20T20:02:16Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;==Video==  The video from the lecture given on March 20, 2020 [https://homeostasis.scs.carleton.ca/~soma/os-2020w/lectures/comp3000-2020w-lec19-20200320.m4v is now available]....&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;
The video from the lecture given on March 20, 2020 [https://homeostasis.scs.carleton.ca/~soma/os-2020w/lectures/comp3000-2020w-lec19-20200320.m4v is now available].&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Lecture 19&lt;br /&gt;
----------&lt;br /&gt;
&lt;br /&gt;
Topics&lt;br /&gt;
* questions about exam format&lt;br /&gt;
* update on Assignment 4&lt;br /&gt;
* questions on Assignment 3&lt;br /&gt;
* Deadlock&lt;br /&gt;
* Tutorial 9&lt;br /&gt;
* Scheduling&lt;br /&gt;
&lt;br /&gt;
Final exam&lt;br /&gt;
 - open book/note/internet&lt;br /&gt;
 - 2 hours&lt;br /&gt;
 - will post PDF of questions, 2 hours later you should upload solutions&lt;br /&gt;
   to cuLearn&lt;br /&gt;
 - will be available for PMs for questions during exam&lt;br /&gt;
 - after exam is graded, will do targeted interviews&lt;br /&gt;
   (randomly and targeted) to ensure exams match with your knowledge&lt;br /&gt;
   5-10 minutes, if you did the work on your own nothing to worry about&lt;br /&gt;
   will contact via discord and email&lt;br /&gt;
 - collaboration is NOT ALLOWED during the final&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Critical sections&lt;br /&gt;
 - code accessing shared resource&lt;br /&gt;
 - if resource is accessed by other code while in a critical section&lt;br /&gt;
   resource can be corrupted&lt;br /&gt;
 - semaphores are used to protect critical sections&lt;br /&gt;
    - binary semaphores are so often used for this they are also known as&lt;br /&gt;
      mutexes (for enforcing mutual exclusion)&lt;br /&gt;
 - mutual exclusion - making sure only one thread/process is in the critical&lt;br /&gt;
   section at a time (accessing shared resource at the same time)&lt;br /&gt;
 - also exist higher-level ways to protect critical sections, e.g.&lt;br /&gt;
   Java synchronized blocks&lt;br /&gt;
&lt;br /&gt;
lock in entry in 3000pc-rendezvous.c is there to enforce mutual exclusion&lt;br /&gt;
when modifying each entry&lt;br /&gt;
 - only the producer or consumer can change the word, never both&lt;br /&gt;
   at the same time&lt;br /&gt;
 - in practice, because we are doing so little work,&lt;br /&gt;
   very hard to catch the producer and consumer editing the word at the same&lt;br /&gt;
   time (possible but unlikely)&lt;br /&gt;
  &lt;br /&gt;
Deadlock: two or more processes/threads waiting on each other such that&lt;br /&gt;
they will wait forever (dead =&amp;gt; no progress on computation)&lt;br /&gt;
&lt;br /&gt;
mutual exclusion =&amp;gt; prevent corruption&lt;br /&gt;
avoiding deadlock =&amp;gt; preventing computation from getting stuck&lt;br /&gt;
&lt;br /&gt;
Efforts to enforce mutual exclusion can result in deadlock if you aren&amp;#039;t&lt;br /&gt;
careful.&lt;br /&gt;
&lt;br /&gt;
Necessary conditions for deadlock&lt;br /&gt;
 - mutual exclusion&lt;br /&gt;
 - resource holding&lt;br /&gt;
 - no preemption&lt;br /&gt;
 - circular wait&lt;br /&gt;
&lt;br /&gt;
Note deadlock can happen even without semaphores or shared data, just needs concurrency&lt;br /&gt;
 - Example: web application where web server is waiting on client to send data, while web client is waiting on the server to send data&lt;br /&gt;
     - well, deadlock if neither ever times out&lt;br /&gt;
&lt;br /&gt;
Explaining deadlock with dining philosopher&amp;#039;s problem&lt;br /&gt;
 - how do you make sure nobody starves?&lt;br /&gt;
 - 5 mathematicians, 5 chopsticks&lt;br /&gt;
 - need two chopsticks to eat&lt;br /&gt;
 - not going to talk to the other mathematicians about food,&lt;br /&gt;
   just going to talk math&lt;br /&gt;
 - what algorithm should they use for grabbing chopsticks?&lt;br /&gt;
&lt;br /&gt;
Obvious solution:&lt;br /&gt;
 - grab one on the left if it is there, then grab one on the right&lt;br /&gt;
 - this is BAD, why?&lt;br /&gt;
 - if everyone grabs the left chopstick, NOBODY will eat,&lt;br /&gt;
   because nobody can grab the right chopstick&lt;br /&gt;
 - really want to grab 2 or grab 0, but how?&lt;br /&gt;
 - ok, do a timeout...but if fixed, can get oscillation&lt;br /&gt;
   - technically this is livelock I think, but clearly it is bad&lt;br /&gt;
 - may be simpler to just kill one mathematician&lt;br /&gt;
   - or get one more chopstick&lt;br /&gt;
   - then you have at least two chopsticks for somebody, always&lt;br /&gt;
 &lt;br /&gt;
Approaches to making sure you don&amp;#039;t have deadlocks&lt;br /&gt;
 - ignore (i.e., I&amp;#039;ll reboot when it happens)&lt;br /&gt;
 - prevention&lt;br /&gt;
    - making sure four conditions never hold, e.g, make sure&lt;br /&gt;
      no circular dependencies&lt;br /&gt;
        - e.g., always allocate resources in the same order&lt;br /&gt;
	- may stop deadlock but may lead to starvation (some&lt;br /&gt;
	  not progressing in computation)&lt;br /&gt;
    - or, being careful with resource allocation so it never results in&lt;br /&gt;
      deadlock&lt;br /&gt;
       (this last one is the focus of much deadlock research)&lt;br /&gt;
 - detection/recovery&lt;br /&gt;
    - timeouts, so when been waiting too long &amp;quot;restart&amp;quot; in some&lt;br /&gt;
      way to get back to a sane state&lt;br /&gt;
    &lt;br /&gt;
Watchdog timers are a general way to deal with deadlock&lt;br /&gt;
 - have a part of the system &amp;quot;press the red button&amp;quot; periodically&lt;br /&gt;
 - some part is guaranteed to be watching for the periodic button&lt;br /&gt;
   presses&lt;br /&gt;
 - if the button isn&amp;#039;t pressed, reboot&lt;br /&gt;
 - &amp;quot;dead man&amp;#039;s switch&amp;quot;&lt;br /&gt;
&lt;br /&gt;
kernel oops is the kernel&amp;#039;s way of dealing with memory access errors&lt;br /&gt;
 (what would cause a segfault in userspace)&lt;br /&gt;
&lt;br /&gt;
Think of a synchonized block in Java&lt;br /&gt;
 - grabs semaphore at start of block&lt;br /&gt;
 - releases at end&lt;br /&gt;
 - so, only one thread can be in block at the same time&lt;br /&gt;
 - key advantage: you don&amp;#039;t have to remember to grab and release&lt;br /&gt;
   semaphores, mistakes there is a huge source of errors&lt;br /&gt;
&lt;br /&gt;
Scheduling&lt;br /&gt;
 - problem: how do we share the CPU between processes and threads?&lt;br /&gt;
 - with memory, we can just split it up between processes&lt;br /&gt;
 - but with the CPU, they have to take turns.  How?&lt;br /&gt;
&lt;br /&gt;
The scheduling problem is&lt;br /&gt;
 - how to do turn taking between processes &amp;amp; threads&lt;br /&gt;
 - when to do turn taking&lt;br /&gt;
 &lt;br /&gt;
Remember the CPU runs in (at least) two modes&lt;br /&gt;
 - user mode: restricted access to resources&lt;br /&gt;
 - supervisor mode: full access to resources&lt;br /&gt;
&lt;br /&gt;
Processes run in user mode, the kernel runs in supervisor mode&lt;br /&gt;
&lt;br /&gt;
When we make a system call, it causes the CPU to switch into supervisor mode&lt;br /&gt;
and run the system call dispatcher in the kernel&lt;br /&gt;
&lt;br /&gt;
Scheduling happens when the the kernel finishes with handling a system call&lt;br /&gt;
or with handling an interrupt&lt;br /&gt;
 - it happens when the kernel wants to return to userspace&lt;br /&gt;
 - happens on the supervisor-&amp;gt;user mode switch&lt;br /&gt;
 - so, what process or thread should run on this switch?&lt;br /&gt;
   - it normally isn&amp;#039;t what got us into the kernel in the first place&lt;br /&gt;
   - so, returning from a system call is very different from returning from&lt;br /&gt;
     a function&lt;br /&gt;
&lt;br /&gt;
So, one big determinant of what to run is a process&amp;#039;s priority&lt;br /&gt;
 - static priority, i.e. &amp;quot;niceness value&amp;quot;&lt;br /&gt;
 - dynamic priority&lt;br /&gt;
&lt;br /&gt;
Static priorities never change, dynamic ones do all the time&lt;br /&gt;
 - adjusted based on recent history&lt;br /&gt;
 - if a process has run recently, its dynamic priority gets &amp;quot;lowered&amp;quot;&lt;br /&gt;
   (which means a bigger value) so it is less likely to run next&lt;br /&gt;
 - if a process hasn&amp;#039;t run recently, its dynamic priority gets &amp;quot;increased&amp;quot;&lt;br /&gt;
   (lowered value)&lt;br /&gt;
&lt;br /&gt;
We have to constantly adjust dynamic priorities because everyone deserves&lt;br /&gt;
some resources&lt;br /&gt;
 - no process should be starved of CPU time&lt;br /&gt;
 - should be &amp;quot;fair&amp;quot;&lt;br /&gt;
&lt;br /&gt;
If you&amp;#039;re giving out pizza to a school&lt;br /&gt;
 - static priority: how many slices everyone bought&lt;br /&gt;
 - dynamic priority: have they gotten their pizza yet?&lt;br /&gt;
   - even if someone bought 3 slices, and they ask for more...&lt;br /&gt;
     they may have already have had three slices and&lt;br /&gt;
     another kid only ordered one slice but hasn&amp;#039;t gotten any&lt;br /&gt;
&lt;br /&gt;
We can change static priorities from userspace&lt;br /&gt;
dynamic priorities are purely controlled by the kernel&lt;br /&gt;
&lt;br /&gt;
What is the algorithm for going from priorities to actual&lt;br /&gt;
scheduling decisions?&lt;br /&gt;
 - COMPLICATED, and keeps changing&lt;br /&gt;
 - turns out scheduling in practice is a very delicate art&lt;br /&gt;
 - challenge: many kinds of workloads&lt;br /&gt;
&lt;br /&gt;
Contrast playing a game on your own machine to a server handling&lt;br /&gt;
thousands of requests to a web server with a background indexer running&lt;br /&gt;
 - what should be give priority isn&amp;#039;t always clear from the kernel&amp;#039;s perspective&lt;br /&gt;
 - sometimes &amp;quot;fair&amp;quot; isn&amp;#039;t what the user really wants&lt;br /&gt;
    - better to run the game than the background AV program!&lt;br /&gt;
 - gets really complicated when you take into account I/O usage as well as&lt;br /&gt;
   CPU time&lt;br /&gt;
&lt;br /&gt;
Scheduling is kernel version specific...highly variable&lt;br /&gt;
&lt;br /&gt;
Some processes are CPU bound (i.e., calculating digits of pi) while&lt;br /&gt;
others are I/O bound (scanning files for malware).&lt;br /&gt;
&lt;br /&gt;
Some are interactive (a game) versus non-interactive (AV scanner)&lt;br /&gt;
&lt;br /&gt;
I/O bound processes should normally get CPU time whenever they want it&lt;br /&gt;
 - most of the time, they are waiting on outside activity&lt;br /&gt;
&lt;br /&gt;
But interactive programs should be prioritized&lt;br /&gt;
 - so users get responsive systems&lt;br /&gt;
&lt;br /&gt;
But we also want high throughput&lt;br /&gt;
 - make programs complete their work faster&lt;br /&gt;
&lt;br /&gt;
There is a cost to switching which program is running, so can&amp;#039;t just&lt;br /&gt;
switch things around constantly.&lt;br /&gt;
&lt;br /&gt;
No clear winner because metrics change, different users have different goals&lt;br /&gt;
&lt;br /&gt;
Nowadays we care about power which makes scheduling even more complicated!&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Soma</name></author>
	</entry>
</feed>