<?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_18</id>
	<title>Operating Systems 2020W Lecture 18 - 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_18"/>
	<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=Operating_Systems_2020W_Lecture_18&amp;action=history"/>
	<updated>2026-06-02T23:09:23Z</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_18&amp;diff=22583&amp;oldid=prev</id>
		<title>Soma: Created page with &quot;==Video==  The video for the lecture given on March 18, 2020 [https://homeostasis.scs.carleton.ca/~soma/os-2020w/lectures/comp3000-2020w-lec18-20200318.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_18&amp;diff=22583&amp;oldid=prev"/>
		<updated>2020-03-20T02:43:41Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;==Video==  The video for the lecture given on March 18, 2020 [https://homeostasis.scs.carleton.ca/~soma/os-2020w/lectures/comp3000-2020w-lec18-20200318.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 for the lecture given on March 18, 2020 [https://homeostasis.scs.carleton.ca/~soma/os-2020w/lectures/comp3000-2020w-lec18-20200318.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 18&lt;br /&gt;
----------&lt;br /&gt;
&lt;br /&gt;
Topics&lt;br /&gt;
* race condition deadlock in 3000pc-rendezvous.c&lt;br /&gt;
* hints for /dev/describe, Assignment 3 Q5&lt;br /&gt;
  - how to allow writing&lt;br /&gt;
  - how to store the pid&lt;br /&gt;
  - how to get task info&lt;br /&gt;
* containers&lt;br /&gt;
* scheduling&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Yes, Assignment 3 is due on March 25th, NOT March 20th!&lt;br /&gt;
 (changed when the assignment was finalized)&lt;br /&gt;
&lt;br /&gt;
Assigment 4 will be out by Friday, due last day of class&lt;br /&gt;
 (you just have to submit multiple choice answers)&lt;br /&gt;
&lt;br /&gt;
Will discuss final in a bit&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
So the bug in 3000pc-rendezvous.c is because communication isn&amp;#039;t&lt;br /&gt;
reliable&lt;br /&gt;
 - the consumer can send a wakeup message to the producer, and&lt;br /&gt;
   the producer won&amp;#039;t get it because it wasn&amp;#039;t waiting for the message&lt;br /&gt;
   (and vice versa)&lt;br /&gt;
 - solution is to add a timeout so messages can be re-sent&lt;br /&gt;
    - this is very real world&lt;br /&gt;
 - will try to post a better solution soon!  But use the posted version of&lt;br /&gt;
   3000pc-rendezvous for your assignment&lt;br /&gt;
 - in the meantime, your modified version should ALSO deadlock&lt;br /&gt;
 - (notice the fifo version is much more reliable!)&lt;br /&gt;
 - removing fprintf&amp;#039;s in wait_for_consumer() and wait_for_producer() should&lt;br /&gt;
   make 3000pc-rendezvous.c much more reliable (but perhaps not deadlock free)&lt;br /&gt;
    - because the fprintf&amp;#039;s cause system calls, which are a long enough&lt;br /&gt;
      delay (and a context switch) to cause a delay in the wait,&lt;br /&gt;
      so the wakeup message sent via the condition variable is lost&lt;br /&gt;
 - in practice, never assume perfectly reliable communication&lt;br /&gt;
   with concurrency, always plan for lost messages with timeouts&lt;br /&gt;
&lt;br /&gt;
I should discuss deadlock in more detail in a later lecture!&lt;br /&gt;
 - remind me!&lt;br /&gt;
&lt;br /&gt;
Are timeouts the only way to deal with lost messages?&lt;br /&gt;
 - basically, yes&lt;br /&gt;
   - ok, you could just send multiple, redundant messages,&lt;br /&gt;
     but that has its own problems&lt;br /&gt;
 - this is how we do it normally, especially over networks&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Hints for /dev/describe&lt;br /&gt;
 - there is a routine for converting strings to integers, kstrtol(),&lt;br /&gt;
   need this to get the provided string into a PID&lt;br /&gt;
&lt;br /&gt;
 - copy the read function to make a write function&lt;br /&gt;
   - same function signature, just make the buf constant&lt;br /&gt;
 - add write function to the device file ops struct&lt;br /&gt;
 - make the device file writable (change 0444 to 0666)&lt;br /&gt;
    - note the leading 0 in C means the value is octal, not decimal&lt;br /&gt;
    - 0x in front means hexadecimal&lt;br /&gt;
 - to get the uid, gid, look at how getuid, getgid work&lt;br /&gt;
   - getuid, getgid look weird&lt;br /&gt;
   - this is because they have been generalized to work with&lt;br /&gt;
     namespaces that are used for containers&lt;br /&gt;
     (e.g., containers each have their&lt;br /&gt;
     own range of process IDs, uids, gids, etc)&lt;br /&gt;
   - will explain containers in a bit&lt;br /&gt;
 - use task_uid(), task_euid() macros to find out the uid, gid&lt;br /&gt;
   associated with a task&lt;br /&gt;
   - still have to &amp;quot;munge&amp;quot; (transform) as getuid does&lt;br /&gt;
   - kernel maintains internal pid&amp;#039;s that are different from userspace&lt;br /&gt;
     ones, needed to support containers&lt;br /&gt;
   - so before sending a pid to userspace, we have to transform it&lt;br /&gt;
   - on VMs you probably don&amp;#039;t need it since we aren&amp;#039;t using&lt;br /&gt;
     containers, so just try and see what works&lt;br /&gt;
   - e.g., try &amp;quot;from_kuid_munged(current_user_ns(), task_uid(task))&amp;quot;&lt;br /&gt;
     to get the uid&lt;br /&gt;
&lt;br /&gt;
 - to get a task from a PID, use pid_task(pid, PIDTYPE_TGID) &amp;lt;-- not quite&lt;br /&gt;
   - will confirm after lecture&lt;br /&gt;
&lt;br /&gt;
I don&amp;#039;t expect you to understand everything with Q5&lt;br /&gt;
 - I definitely don&amp;#039;t!&lt;br /&gt;
But this gives you some experience in looking through the Linux&lt;br /&gt;
kernel source and trying to figure out how things work&lt;br /&gt;
 - so many abstractions!&lt;br /&gt;
&lt;br /&gt;
First, process groups, then containers&lt;br /&gt;
&lt;br /&gt;
Process groups are based on a simple issue&lt;br /&gt;
 - when a user logs out, what processes should be terminated?&lt;br /&gt;
   - can&amp;#039;t kill all of the processes belonging to the user,&lt;br /&gt;
     maybe they are logged in multiple times or&lt;br /&gt;
     they are running background processes that could be running for&lt;br /&gt;
     days or longer&lt;br /&gt;
 - process groups allow the system to know what processes should&lt;br /&gt;
   be terminated - just kill everything in the process group&lt;br /&gt;
&lt;br /&gt;
Similarly, processes are groups of threads&lt;br /&gt;
 - so when you do things with a thread,&lt;br /&gt;
   you have to keep track of the other threads&lt;br /&gt;
&lt;br /&gt;
tgid in kernel: &amp;quot;thread group ID&amp;quot;&lt;br /&gt;
 - PID of a multithreaded process&lt;br /&gt;
 - each thread also has its own PID&lt;br /&gt;
 - single threaded processes, pid=tgid&lt;br /&gt;
&lt;br /&gt;
tasks/threads/processes normally greatly outnumber the number of cores&lt;br /&gt;
 - so they each get a &amp;quot;turn&amp;quot; on the CPU&lt;br /&gt;
 - this problem is the CPU scheduling problem, which we will discuss!&lt;br /&gt;
&lt;br /&gt;
Why does firefox have so many threads?&lt;br /&gt;
 - to make it faster!&lt;br /&gt;
 - only one thread, runs only on one core&lt;br /&gt;
 - for performance, want to run on multiple cores at the same time&lt;br /&gt;
&lt;br /&gt;
Note tha Chrome divides itself into multiple processes, as does firefox&lt;br /&gt;
 - look like threads to me?&lt;br /&gt;
 - but I know they are isolated for security purposes, to limit&lt;br /&gt;
   sharing of memory (so evil web pages don&amp;#039;t compromise the whole&lt;br /&gt;
   browser)&lt;br /&gt;
 - not sure how exactly they are kept separate...&lt;br /&gt;
&lt;br /&gt;
top lists all tgid&amp;#039;s by default, but can be changed with the -H option (or H)&lt;br /&gt;
&lt;br /&gt;
Idea of containers is I want to share the kernel between multiple userspaces&lt;br /&gt;
 - with virtual machines, you partition the hardware and run multiple&lt;br /&gt;
   kernels, each of which has its own set of processes (userspace)&lt;br /&gt;
 - but why run multiple kernels at the same time?  Can&amp;#039;t one do the job?&lt;br /&gt;
   - that&amp;#039;s why we have containers - multiple userlands (sets of processes,&lt;br /&gt;
     root filesystem, etc) all running on one kernel&lt;br /&gt;
 - much more efficient than virtual machines, but containers aren&amp;#039;t&lt;br /&gt;
   as well separated (depends on the kernel separating things properly&lt;br /&gt;
&lt;br /&gt;
The Linux kernel doesn&amp;#039;t directly support containers&lt;br /&gt;
 - instead, it has multiple abstractions that can be combined to produce&lt;br /&gt;
   containers&lt;br /&gt;
&lt;br /&gt;
Most id&amp;#039;s can be put into namespaces&lt;br /&gt;
 - pid, uid, gid, etc&lt;br /&gt;
&lt;br /&gt;
So now any kernel routine that works with these id&amp;#039;s has to first&lt;br /&gt;
figure out which namespace (container) it belongs to&lt;br /&gt;
 - PID 1112, uid 1000 can mean different things depending on the container it&lt;br /&gt;
   is in, each has its own namespace&lt;br /&gt;
&lt;br /&gt;
Container/namespace support is why credentials are crazily abstracted in the&lt;br /&gt;
Linux kernel now (they used to be much simpler!)&lt;br /&gt;
&lt;br /&gt;
These containers are *exactly* what are used by docker, kubernetes, etc&lt;br /&gt;
&lt;br /&gt;
Why aren&amp;#039;t containers as well separated?&lt;br /&gt;
 - the API that is multiplexed in virtual machines is the hardware interface&lt;br /&gt;
    - devices, interrupts, CPU facilities&lt;br /&gt;
 - the API that is multiplexed with containers is the entirety of the system&lt;br /&gt;
   call interface&lt;br /&gt;
    - LOTS of system calls, with complex semantics&lt;br /&gt;
 - hardware interface is simpler to abstract, partition&lt;br /&gt;
&lt;br /&gt;
Intro to scheduling (will continue next lecture)&lt;br /&gt;
 - we only have so many cores on which to run code&lt;br /&gt;
 - must share them, how?&lt;br /&gt;
&lt;br /&gt;
One way we share: system calls&lt;br /&gt;
 - process is paused when it makes a system call&lt;br /&gt;
 - other processes or the kernel can use the core until the system call returns&lt;br /&gt;
   (i.e., if a process is blocked on a read, the kernel and other&lt;br /&gt;
   processes can use that core until the read finishes)&lt;br /&gt;
&lt;br /&gt;
But what if a process isn&amp;#039;t making many system calls?&lt;br /&gt;
 - calculating the digits of pi?&lt;br /&gt;
&lt;br /&gt;
Then, use a timer to allow a process to only use the CPU for some time&lt;br /&gt;
 - e.g., one millisecond&lt;br /&gt;
 - kernel sets a timer for one millisecond, interrupt happens after that time&lt;br /&gt;
 - kernel gets called by interrupt handler, decides what should next&lt;br /&gt;
   run on the core&lt;br /&gt;
&lt;br /&gt;
Final exam&lt;br /&gt;
 - open book, but will be similar to last term in format&lt;br /&gt;
   (being open book won&amp;#039;t really help you)&lt;br /&gt;
 - timed - you download at start, upload your answers to cuLearn by end&lt;br /&gt;
 - randomized and targeted zoom interviews&lt;br /&gt;
   - to make sure you actually understood what you wrote&lt;br /&gt;
 - if it becomes clear you couldn&amp;#039;t have written your answers,&lt;br /&gt;
   will report to dean for plagiarism&lt;br /&gt;
&lt;br /&gt;
Will explain more next class, see you Friday!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Soma</name></author>
	</entry>
</feed>