<?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_14</id>
	<title>Operating Systems 2020W Lecture 14 - 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_14"/>
	<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=Operating_Systems_2020W_Lecture_14&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_14&amp;diff=22579&amp;oldid=prev</id>
		<title>Soma: Created page with &quot;==Video==  The video from the lecture given on March 4, 2020 [https://homeostasis.scs.carleton.ca/~soma/os-2020w/lectures/comp3000-2020w-lec14-20200304.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_14&amp;diff=22579&amp;oldid=prev"/>
		<updated>2020-03-20T02:35:34Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;==Video==  The video from the lecture given on March 4, 2020 [https://homeostasis.scs.carleton.ca/~soma/os-2020w/lectures/comp3000-2020w-lec14-20200304.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 4, 2020 [https://homeostasis.scs.carleton.ca/~soma/os-2020w/lectures/comp3000-2020w-lec14-20200304.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 14&lt;br /&gt;
----------&lt;br /&gt;
&lt;br /&gt;
Topics&lt;br /&gt;
* midterm&lt;br /&gt;
* tutorials (meta)&lt;br /&gt;
* concurrency&lt;br /&gt;
* producer/consumer&lt;br /&gt;
* pipes&lt;br /&gt;
* shared memory with mmap&lt;br /&gt;
* Tutorial 6&lt;br /&gt;
&lt;br /&gt;
* pattern matching versus model building&lt;br /&gt;
&lt;br /&gt;
Concurrency&lt;br /&gt;
 - doing multiple things &amp;quot;in parallel&amp;quot; that interact&lt;br /&gt;
    - note signal handlers add a kind of concurrency&lt;br /&gt;
 - we really can&amp;#039;t do it well, can only do it in limited circumstances&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
We see concurrency issues when there are multiple computations that have&lt;br /&gt;
to communicate &amp;amp; coordinate&lt;br /&gt;
 - multiple threads&lt;br /&gt;
 - multiple processes&lt;br /&gt;
 - processes running on different systems&lt;br /&gt;
 - single process that gets interrupted by signal handlers&lt;br /&gt;
&lt;br /&gt;
Key task with concurrency is managing interactions&lt;br /&gt;
&lt;br /&gt;
Two basic strategies&lt;br /&gt;
 - messages&lt;br /&gt;
 - shared data structures (memory)&lt;br /&gt;
&lt;br /&gt;
Networking is messaging-based concurrency&lt;br /&gt;
So is pipes&lt;br /&gt;
&lt;br /&gt;
We can use messages to simulate shared state&lt;br /&gt;
 - think google docs&lt;br /&gt;
&lt;br /&gt;
But where we can, shared state is more efficient,&lt;br /&gt;
but is much more dangerous&lt;br /&gt;
&lt;br /&gt;
Pipes are a weird kernel thing&lt;br /&gt;
 - we are pausing the process doing writes when the reader&lt;br /&gt;
   isn&amp;#039;t ready for them&lt;br /&gt;
&lt;br /&gt;
Pausing writes is easy&lt;br /&gt;
 - just make the write system call take a long time&lt;br /&gt;
 - writes block after all&lt;br /&gt;
&lt;br /&gt;
Note that the same is true for reads, they can block&lt;br /&gt;
&lt;br /&gt;
We coordinate by using a shared buffer&lt;br /&gt;
 - buffer is in kernel space&lt;br /&gt;
 - writes put data into the buffer&lt;br /&gt;
 - reads take data out&lt;br /&gt;
 - when buffer is full, writer is paused on write&lt;br /&gt;
 - when buffer is empty, reader is paused on read&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A pipe is an example of the producer-consumer problem&lt;br /&gt;
 - classic design pattern for concurrency&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
pipes show the line between message passing and shared state isn&amp;#039;t&lt;br /&gt;
always so clear&lt;br /&gt;
&lt;br /&gt;
So, are pipes the way to go for IPC on a single host?&lt;br /&gt;
 - well, there is significant overhead&lt;br /&gt;
 - the overhead is in the system calls (switching back and forth to the kernel)&lt;br /&gt;
   and copying data&lt;br /&gt;
 - (high performance communication makes use of &amp;quot;zero copy&amp;quot; mechanisms)&lt;br /&gt;
&lt;br /&gt;
So, we can just share memory right?&lt;br /&gt;
&lt;br /&gt;
Note that child processes get a copy of a parent&amp;#039;s memory&lt;br /&gt;
 - NOT shared&lt;br /&gt;
&lt;br /&gt;
You get shared memory with either&lt;br /&gt;
 - making a thread (all memory is shared)&lt;br /&gt;
 - shared memory ranges using mmap&lt;br /&gt;
   - do an anonymous mmap that is shared&lt;br /&gt;
   - will be shared from parent to child, won&amp;#039;t be duplicated&lt;br /&gt;
&lt;br /&gt;
So we have shared memory, are we done?  NO&lt;br /&gt;
 - shared memory is WEIRD on modern systems&lt;br /&gt;
 - changes to memory made by one process won&amp;#039;t necessarily be&lt;br /&gt;
   seen immediately by other processes&lt;br /&gt;
&lt;br /&gt;
When we share memory, we are fighting the memory hierarchy&lt;br /&gt;
 - caches get in the way!&lt;br /&gt;
&lt;br /&gt;
Memory hierarchy&lt;br /&gt;
 - registers&lt;br /&gt;
 - L1 cache&lt;br /&gt;
 - L2 cache&lt;br /&gt;
 - L3 cache                 volatile, fast&lt;br /&gt;
 - main memory (DRAM)&lt;br /&gt;
--------------------&lt;br /&gt;
 - SSD&lt;br /&gt;
 - hard drives              persistent, slow&lt;br /&gt;
 - tapes&lt;br /&gt;
&lt;br /&gt;
L1 caches are normally per core&lt;br /&gt;
&lt;br /&gt;
If CPU cores A and B both try modifying X&lt;br /&gt;
 A will have one version of X, B will have another version&lt;br /&gt;
 and they won&amp;#039;t necessarily be the same&lt;br /&gt;
&lt;br /&gt;
You can only share memory safely between processes by using&lt;br /&gt;
special hardware mechanisms which makes sure data is consistent&lt;br /&gt;
  - not all data, just selected data&lt;br /&gt;
&lt;br /&gt;
What do we need?  special CPU instructions that make sure&lt;br /&gt;
changes to memory are instantly shared across all CPUs&lt;br /&gt;
 - need hardware-provided atomic operations&lt;br /&gt;
&lt;br /&gt;
But we don&amp;#039;t want to use such mechanisms directly&lt;br /&gt;
need special language support (just like with system calls)&lt;br /&gt;
&lt;br /&gt;
semaphores are an abstraction of this sort of coordination mechanism,&lt;br /&gt;
using shared memory that is changed atomically&lt;br /&gt;
&lt;br /&gt;
atomic operation - indivisible operation&lt;br /&gt;
&lt;br /&gt;
Semaphores come from railroad signals&lt;br /&gt;
 - want to make sure shared track is never occupied by two trains&lt;br /&gt;
 - semaphore indicates when the track is &amp;quot;taken&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Basic algorithm for accessing shared state&lt;br /&gt;
 - try to grab semaphore&lt;br /&gt;
     (check if zero, if zero, make one)&lt;br /&gt;
 - if taken, wait until it becomes available, then take it&lt;br /&gt;
     (watch semaphore until it turns to zero, then make it one)&lt;br /&gt;
 - when finished, release semaphore&lt;br /&gt;
     (set semaphore to zero)&lt;br /&gt;
&lt;br /&gt;
 (this describes a binary semaphore, also known as a mutex)&lt;br /&gt;
 (can also have counting semaphores)&lt;br /&gt;
&lt;br /&gt;
You can&amp;#039;t implement a semaphore safely with a simple byte or integer variable,&lt;br /&gt;
using standard programming constructs&lt;br /&gt;
&lt;br /&gt;
Need to be able to do test and set (check value and change it) as ONE instruction&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Soma</name></author>
	</entry>
</feed>