<?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_2021F_Lecture_21</id>
	<title>Operating Systems 2021F Lecture 21 - 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_2021F_Lecture_21"/>
	<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=Operating_Systems_2021F_Lecture_21&amp;action=history"/>
	<updated>2026-04-06T04:47:59Z</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_2021F_Lecture_21&amp;diff=23570&amp;oldid=prev</id>
		<title>Soma at 17:30, 2 December 2021</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=Operating_Systems_2021F_Lecture_21&amp;diff=23570&amp;oldid=prev"/>
		<updated>2021-12-02T17:30:05Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 17:30, 2 December 2021&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l2&quot;&gt;Line 2:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 2:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Video from the lecture given on December 2, 2021 is now available:&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Video from the lecture given on December 2, 2021 is now available:&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* [https://homeostasis.scs.carleton.ca/~soma/os-2021f/lectures/comp3000-2021f-&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;lec20&lt;/del&gt;-20211202.m4v video]&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* [https://homeostasis.scs.carleton.ca/~soma/os-2021f/lectures/comp3000-2021f-&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;lec21&lt;/ins&gt;-20211202.m4v video]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* [https://homeostasis.scs.carleton.ca/~soma/os-2021f/lectures/comp3000-2021f-&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;lec20&lt;/del&gt;-20211202.cc.vtt auto-generated captions]&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* [https://homeostasis.scs.carleton.ca/~soma/os-2021f/lectures/comp3000-2021f-&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;lec21&lt;/ins&gt;-20211202.cc.vtt auto-generated captions]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Video is also available through Brightspace (Resources-&amp;gt;Class zoom meetings-&amp;gt;Cloud Recordings tab)&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Video is also available through Brightspace (Resources-&amp;gt;Class zoom meetings-&amp;gt;Cloud Recordings tab)&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Soma</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=Operating_Systems_2021F_Lecture_21&amp;diff=23569&amp;oldid=prev</id>
		<title>Soma: Created page with &quot;==Video==  Video from the lecture given on December 2, 2021 is now available: * [https://homeostasis.scs.carleton.ca/~soma/os-2021f/lectures/comp3000-2021f-lec20-20211202.m4v...&quot;</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=Operating_Systems_2021F_Lecture_21&amp;diff=23569&amp;oldid=prev"/>
		<updated>2021-12-02T17:29:44Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;==Video==  Video from the lecture given on December 2, 2021 is now available: * [https://homeostasis.scs.carleton.ca/~soma/os-2021f/lectures/comp3000-2021f-lec20-20211202.m4v...&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 lecture given on December 2, 2021 is now available:&lt;br /&gt;
* [https://homeostasis.scs.carleton.ca/~soma/os-2021f/lectures/comp3000-2021f-lec20-20211202.m4v video]&lt;br /&gt;
* [https://homeostasis.scs.carleton.ca/~soma/os-2021f/lectures/comp3000-2021f-lec20-20211202.cc.vtt auto-generated captions]&lt;br /&gt;
Video is also available through Brightspace (Resources-&amp;gt;Class zoom meetings-&amp;gt;Cloud Recordings tab)&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Lecture 21&lt;br /&gt;
----------&lt;br /&gt;
Yes you can make your own filesystem&lt;br /&gt;
 - in fact, you can do this in userspace, see FUSE&lt;br /&gt;
   (this is how sshfs works)&lt;br /&gt;
&lt;br /&gt;
In the kernel, a file is a file struct&lt;br /&gt;
 - in userspace, it is a file descriptor (a small number)&lt;br /&gt;
 - note that a file struct represents an *open* file&lt;br /&gt;
   - and thus is referenced in task_struct&lt;br /&gt;
&lt;br /&gt;
When we look in /proc/&amp;lt;PID&amp;gt;, we are looking at task_struct&amp;#039;s&lt;br /&gt;
 - each file/directory corresponds to parts of the task_struct&lt;br /&gt;
&lt;br /&gt;
System calls in the kernel are defined with special macros&lt;br /&gt;
 - SYSCALL_DEFINEX  where X is a number for number of args&lt;br /&gt;
 - need this because system calls aren&amp;#039;t invoked like regular functions on the kernel side either, need to do machine language magic&lt;br /&gt;
   - after initial entry, kernel uses regular C functions like&lt;br /&gt;
     userspace&lt;br /&gt;
&lt;br /&gt;
As you can see, kernel code is C code, but there&amp;#039;s a lot and&lt;br /&gt;
it has lots of abstractions&lt;br /&gt;
 - I get lost following it sometimes&lt;br /&gt;
 - but if you&amp;#039;re patient you can see the patterns&lt;br /&gt;
&lt;br /&gt;
file descriptor is an index into an array of file structs in the kernel&lt;br /&gt;
&lt;br /&gt;
process IDs identify task_struct&amp;#039;s in the kernel&lt;br /&gt;
 - the process that made a given system call is always &amp;quot;current&amp;quot;&lt;br /&gt;
   in the kernel&lt;br /&gt;
&lt;br /&gt;
The current position in a file is inside a file struct&lt;br /&gt;
 - that&amp;#039;s the offset&lt;br /&gt;
&lt;br /&gt;
VFS = virtual file system&lt;br /&gt;
 - abstraction in the kernel for filesystems&lt;br /&gt;
 - allows for many implementations&lt;br /&gt;
&lt;br /&gt;
In kernel data structures, you&amp;#039;ll see mutexes and spinlocks&lt;br /&gt;
 - logically, they are the same thing&lt;br /&gt;
 - but a spinlock busy waits, while a mutex can go to sleep/wake up&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A busy wait is just a loop &amp;quot;spinning&amp;quot;&lt;br /&gt;
 - typically a while loop on a condition or equivalent&lt;br /&gt;
   logically:&lt;br /&gt;
&lt;br /&gt;
   int lock = 0;  /* shared variable */&lt;br /&gt;
&lt;br /&gt;
   while (lock) {&lt;br /&gt;
   	       /* wait */&lt;br /&gt;
   }&lt;br /&gt;
   lock = 1;&lt;br /&gt;
   /* do something assuming the condition is true */&lt;br /&gt;
   lock = 0;&lt;br /&gt;
&lt;br /&gt;
   Note that in the loop we&amp;#039;re doing no useful work,&lt;br /&gt;
   just waiting&lt;br /&gt;
      - very inefficient for long period of times&lt;br /&gt;
      - but going to sleep and waking up later takes a while&lt;br /&gt;
&lt;br /&gt;
It is more complicated because the above code can&amp;#039;t ever work properly on modern computers&lt;br /&gt;
&lt;br /&gt;
The problem is the separation between the testing (the while loop) and the setting (the assignment)&lt;br /&gt;
 - another process or thread could set lock to 1 between the end&lt;br /&gt;
   of the while loop and the assignment,&lt;br /&gt;
     - so logically two processes/threads would have the lock,&lt;br /&gt;
       but we wouldn&amp;#039;t know it&lt;br /&gt;
&lt;br /&gt;
This is known as a race condition&lt;br /&gt;
  - state depends on relative speed of different concurrent executions&lt;br /&gt;
&lt;br /&gt;
The only way to do this safely on modern systems is using special machine language instructions&lt;br /&gt;
 - the exact ones vary from processor to processor&lt;br /&gt;
 - but all modern processors that support multiple cores have them&lt;br /&gt;
&lt;br /&gt;
(Yes, you can&amp;#039;t make a mutex using standard C!)&lt;br /&gt;
&lt;br /&gt;
These instructions either swap two values as one atomic operation,&lt;br /&gt;
or they do a test &amp;amp; set as one atomic operation&lt;br /&gt;
 - they are implemented by one core sending messages to other cores&lt;br /&gt;
   saying &amp;quot;hey I&amp;#039;m messing with this memory, nobody else touch it&lt;br /&gt;
   until I&amp;#039;m done&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
a &amp;quot;spinlock&amp;quot; is a mutex that busy waits&lt;br /&gt;
a &amp;quot;mutex&amp;quot; is a mutex that can sleep&lt;br /&gt;
&lt;br /&gt;
a mutex is a semaphore that can take on a value of 1 or 0&lt;br /&gt;
&lt;br /&gt;
a semaphore is just a counting variable that has exclusive access properties&lt;br /&gt;
 - will always have a coherent state when being accessed concurrently&lt;br /&gt;
&lt;br /&gt;
When you&amp;#039;re doing concurrent programming, you need to guarantee mutual exclusion&lt;br /&gt;
 - &amp;quot;mutex&amp;quot; is just short for mutual exclusion&lt;br /&gt;
&lt;br /&gt;
How do processes and kernel threads share the CPU?&lt;br /&gt;
&lt;br /&gt;
When a process runs, it has exclusive access to a core&lt;br /&gt;
 - but the core is in user mode, so limited access&lt;br /&gt;
&lt;br /&gt;
But it only gets the core for a limited time.  Its use will end when&lt;br /&gt;
 - it makes a system call&lt;br /&gt;
 - it is preempted (interrupted)&lt;br /&gt;
&lt;br /&gt;
The kernel sets timers (timer interrupts) to make sure it gets back the CPU&lt;br /&gt;
 - at least every .01 seconds, but can be more often&lt;br /&gt;
&lt;br /&gt;
What is an interrupt?&lt;br /&gt;
 - an event where a device sends the CPU a message that&lt;br /&gt;
   it must act on immediately&lt;br /&gt;
 - signals are modeled on hardware interrupts&lt;br /&gt;
    - but for processes/kernel messages, not devices&lt;br /&gt;
&lt;br /&gt;
When you type on a keyboard, an interrupt is send by the keyboard to the CPU&lt;br /&gt;
 - and so the CPU has to stop and acknowledge a key has been pressed before continuing its business&lt;br /&gt;
 - (in reality it is more complicated because keyboards have their&lt;br /&gt;
    own little CPU)&lt;br /&gt;
&lt;br /&gt;
So the kernel has to constantly switch to service interrupts from devices&lt;br /&gt;
 - especially network interfaces, that data has to be dealt with quickly or it will be lost&lt;br /&gt;
&lt;br /&gt;
Sometimes polling is used, depends&lt;br /&gt;
 - so then the kernel periodically asks the device for its state,&lt;br /&gt;
   rather than the device telling it it has data&lt;br /&gt;
&lt;br /&gt;
Nowadays we don&amp;#039;t use polling so much because it wastes resources&lt;br /&gt;
most of the time&lt;br /&gt;
 - polling would consume resources when you aren&amp;#039;t typing, for example&lt;br /&gt;
&lt;br /&gt;
When your machine freezes, the kernel is no longer servicing interrupts&lt;br /&gt;
 - so it is REALLY messed up&lt;br /&gt;
&lt;br /&gt;
Keyboard death is a bad thing&lt;br /&gt;
&lt;br /&gt;
One quirk of linux - magic sysrq key&lt;br /&gt;
 - only works on real hardware I think?  Haven&amp;#039;t tried with VMs&lt;br /&gt;
 - you can hit alt-sysrq-X and do all kinds of things&lt;br /&gt;
    - alt-sysrq-S will sync file systems&lt;br /&gt;
    - alt-sysrq-R will instantly reboot the system&lt;br /&gt;
       (sync first otherwise some data may not have been&lt;br /&gt;
        written to disk and will be lost)&lt;br /&gt;
&lt;br /&gt;
So if the magic sysrq key is working, part of the kernel is still alive&lt;br /&gt;
 - if it fails, the kernel truly is dead&lt;br /&gt;
&lt;br /&gt;
On a linux box, it is possible for the display to freeze up but the system is still running&lt;br /&gt;
 - the X server or Wayland server has died&lt;br /&gt;
 - can still ssh in, and can restart any misbehaving processes&lt;br /&gt;
&lt;br /&gt;
Note that when a task loses a core it is then in another state&lt;br /&gt;
 - &amp;quot;blocked&amp;quot; - it is waiting on I/O&lt;br /&gt;
 - &amp;quot;sleep&amp;quot; - it is waiting for its next turn on a core&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The scheduler in the kernel is responsible for deciding what task to run next&lt;br /&gt;
 - it is a doubly linked list I think, with some additions to allow for efficient scheduling&lt;br /&gt;
&lt;br /&gt;
Scheduling needs to be efficient&lt;br /&gt;
 - can&amp;#039;t spend too long deciding&lt;br /&gt;
 - but can precompute things to make it easier&lt;br /&gt;
&lt;br /&gt;
If you look at top, you&amp;#039;ll see a PR and NI value&lt;br /&gt;
 - NI is &amp;quot;niceness&amp;quot;, the priority the user can set,&lt;br /&gt;
   basically static priority&lt;br /&gt;
 - PR is the dynamic priority, which is constantly updated&lt;br /&gt;
   based on past behavior of the process&lt;br /&gt;
     - if it has been using a lot of CPU time, it&lt;br /&gt;
       will get a lower priority&lt;br /&gt;
     - if it was waiting on I/O, it will get a higher&lt;br /&gt;
       priority when that wait is over&lt;br /&gt;
&lt;br /&gt;
So the big difference with a mutex versus a spinlock is with a mutex you give up the CPU when waiting, while with a spinlock you don&amp;#039;t.&lt;br /&gt;
 - if it is a short wait, better to hold the CPU&lt;br /&gt;
 - but otherwise better to give it up so someone else can use it&lt;br /&gt;
&lt;br /&gt;
When you go to sleep (give up the core you had), you have no guarantee when you&amp;#039;ll get a core again&lt;br /&gt;
 - waits can be arbitrarily long on a busy system&lt;br /&gt;
&lt;br /&gt;
a core should only idle if there is nothing to do&lt;br /&gt;
 - no processes in a &amp;quot;ready to run&amp;quot; state&lt;br /&gt;
&lt;br /&gt;
In tutorial 8&lt;br /&gt;
 - with 3000pc-fifo, calls to read/write can cause the process to sleep (when the kernel buffer for the pipe is full/empty)&lt;br /&gt;
 - but with 3000pc-rendezvous, we manage the buffer manually in&lt;br /&gt;
   userspace using mutexes&lt;br /&gt;
&lt;br /&gt;
UNIX is preemptively multitasking&lt;br /&gt;
 - so a task can always be kicked off a core&lt;br /&gt;
&lt;br /&gt;
Some systems are cooperatively multitasking&lt;br /&gt;
 - so a task running cannot be interrupted, it has to give&lt;br /&gt;
   up use of a core&lt;br /&gt;
&lt;br /&gt;
Key advantage of doing it yourself in userspace&lt;br /&gt;
 - grabbing lock successfully doesn&amp;#039;t involve making a system call,&lt;br /&gt;
 - only need system calls when you sleep&lt;br /&gt;
&lt;br /&gt;
With 3000pc-fifo, putting a word in the queue always involves a system call&lt;br /&gt;
 - with rendevous, we may not need to make a system call&lt;br /&gt;
   (and hence may not need to give up the CPU)&lt;br /&gt;
 - but clearly the Linux kernel is very efficient at file I/O,&lt;br /&gt;
   including pipes, even with system call overhead&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Soma</name></author>
	</entry>
</feed>