<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://homeostasis.scs.carleton.ca/wiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=PleaseEnterYourUsername</id>
	<title>Soma-notes - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://homeostasis.scs.carleton.ca/wiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=PleaseEnterYourUsername"/>
	<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php/Special:Contributions/PleaseEnterYourUsername"/>
	<updated>2026-05-12T21:37:33Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.42.1</generator>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=Operating_Systems_2017F_Lecture_10&amp;diff=21139</id>
		<title>Operating Systems 2017F Lecture 10</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=Operating_Systems_2017F_Lecture_10&amp;diff=21139"/>
		<updated>2017-10-20T16:03:33Z</updated>

		<summary type="html">&lt;p&gt;PleaseEnterYourUsername: /* Notes */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
Video from the lecture given on October 10, 2017 [http://homeostasis.scs.carleton.ca/~soma/os-2017f/lectures/comp3000-2017f-lec10-10Oct2017.mp4 is now available].  (The audio worked!)&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
===In-Class===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Lecture 10&lt;br /&gt;
----------&lt;br /&gt;
&lt;br /&gt;
Concurrency&lt;br /&gt;
 - more than one thing happening at the same time&lt;br /&gt;
&lt;br /&gt;
challenge with concurrency is keeping shared state consistent&lt;br /&gt;
 * files that are written to by multiple processes&lt;br /&gt;
 * two or more processes communicating&lt;br /&gt;
 * multiple threads within a process&lt;br /&gt;
&lt;br /&gt;
concurrency leads to race conditions&lt;br /&gt;
 - order of interleaved actions affects output&lt;br /&gt;
 - ex. did process A or B modify the file first?&lt;br /&gt;
&lt;br /&gt;
We want deterministic behavior from concurrent computations&lt;br /&gt;
&lt;br /&gt;
To get consistent computation, we need to coordinate&lt;br /&gt;
 - but you can&#039;t interact via regular variables&lt;br /&gt;
&lt;br /&gt;
Regular variables are subject to the memory hierarchy&lt;br /&gt;
 - variables are stored in registers, L1 cache, L2 cache, L3 cache, main memory&lt;br /&gt;
 - can be deeper in some contexts&lt;br /&gt;
 - so what if coordination variable is in L1 cache for one thread and in main memory for another?&lt;br /&gt;
 - hardware maintains consistency...eventually&lt;br /&gt;
&lt;br /&gt;
mechanisms for coordinating concurrent computations must bypass the memory hierachy&lt;br /&gt;
&lt;br /&gt;
Instead use&lt;br /&gt;
 - filesystem locking primitives (kernel)&lt;br /&gt;
 - special machine language instructions&lt;br /&gt;
&lt;br /&gt;
Operating systems - kernels in particular - are highly concurrent&lt;br /&gt;
 - makes them painful to code&lt;br /&gt;
 - have to always worry about having exclusive (or safe) access to data, resources&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Abstractions for managing concurrency&lt;br /&gt;
 - atomic variables guarantee strict ordering of operations&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
&lt;br /&gt;
Variable X:  ***** &amp;lt;- range of memory locations&lt;br /&gt;
&lt;br /&gt;
To modify X, you...&lt;br /&gt;
  - load X into a register&lt;br /&gt;
  - modify the register&lt;br /&gt;
  - save the register to X&lt;br /&gt;
&lt;br /&gt;
What if two cores try to modify X at the same time?&lt;br /&gt;
  - both could have X in a register at the same time&lt;br /&gt;
&lt;br /&gt;
X is 5&lt;br /&gt;
core A increments by 1&lt;br /&gt;
core B decrements by 1&lt;br /&gt;
&lt;br /&gt;
What&#039;s X after both have run?&lt;br /&gt;
&lt;br /&gt;
If B changes X but doesn&#039;t see A&#039;s change...you get 4&lt;br /&gt;
If A changes X but doesn&#039;t see B&#039;s change...you get 6&lt;br /&gt;
&lt;br /&gt;
For X to be atomic, ^^^ shouldn&#039;t be possible&lt;br /&gt;
 - has to make sure only one core accesses X at a time&lt;br /&gt;
&lt;br /&gt;
Atomic variables are typically used to build semaphores...&lt;br /&gt;
&lt;br /&gt;
But what we really want is mutual exclusion&lt;br /&gt;
&lt;br /&gt;
 - only one thread/process/CPU can access the resource at a time&lt;br /&gt;
&lt;br /&gt;
Want mutual exclusion for larger resources, e.g. data structures, devices&lt;br /&gt;
&lt;br /&gt;
Higher-level languages directly support mutual exclusion, e.g.&lt;br /&gt;
Java synchronized methods&lt;br /&gt;
&lt;br /&gt;
Semaphores are an analogy to a railroad control mechanism&lt;br /&gt;
 - only one train on the track!&lt;br /&gt;
&lt;br /&gt;
When a train enters the shared track&lt;br /&gt;
 - grab the semaphore (increment atomic variable)&lt;br /&gt;
 - if not available, wait (check first to see if it is 0)&lt;br /&gt;
&lt;br /&gt;
When a train leaves the shared track&lt;br /&gt;
 - release the semaphore (decrement atomic variable)&lt;br /&gt;
&lt;br /&gt;
Lesson: don&#039;t implement semaphores yourself, please!&lt;br /&gt;
&lt;br /&gt;
In the Linux kernel&lt;br /&gt;
 - locks using semaphores of various types are associated with all kinds&lt;br /&gt;
   of data structures&lt;br /&gt;
   e.g. inodes&lt;br /&gt;
&lt;br /&gt;
When you can&#039;t get the lock, what do you do?&lt;br /&gt;
 - sleep (tell the scheduler to wake you up when the lock is available)&lt;br /&gt;
 - deal with not getting the resource&lt;br /&gt;
 - spin (do a null loop)&lt;br /&gt;
&lt;br /&gt;
If the wait time is short, spinning is the fastest thing you can do&lt;br /&gt;
&lt;br /&gt;
Where possible, everything should be exclusive, minimize sharing&lt;br /&gt;
&lt;br /&gt;
But in userspace...&lt;br /&gt;
 - shared memory...NOOOOOOO&lt;br /&gt;
    - unless you use atomic variable, or something higher level&lt;br /&gt;
      like a semaphore&lt;br /&gt;
 - messages&lt;br /&gt;
    - series of messages can be used to synchronize state&lt;br /&gt;
&lt;br /&gt;
3-way handshakes, e.g. opening a TCP connection&lt;br /&gt;
 - SYN (synchronize) client-&amp;gt;server&lt;br /&gt;
 - SYN-ACK (acknowledge SYN) server-&amp;gt;client&lt;br /&gt;
 - ACK (acknowledge) client-&amp;gt;server&lt;br /&gt;
&lt;br /&gt;
Can send messages between processes&lt;br /&gt;
 - signals!&lt;br /&gt;
 - other ways too&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
concurrency pattern: producer/consumer&lt;br /&gt;
 * two processes P and C&lt;br /&gt;
 * P &amp;quot;makes&amp;quot; things, C &amp;quot;consumes&amp;quot; them&lt;br /&gt;
 * want to coordinate P and C&lt;br /&gt;
   - P shouldn&#039;t produce too much (C gets too far behind)&lt;br /&gt;
   - C shouldn&#039;t waste time waiting for P&lt;br /&gt;
&lt;br /&gt;
 * shared circular buffer for storing output of P and input of C&lt;br /&gt;
 * when buffer is empty, C should sleep&lt;br /&gt;
 * when buffer is full, P should sleep&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
For those who have used Mutexes before, a mutex is essentially a binary semaphore (hence the name, mutual exclusion).&lt;br /&gt;
&lt;br /&gt;
A mutex allows only a single thread to access a resource, whereas a semaphore allows, and can keep count of how many threads are accessing a certain resource at once.&lt;br /&gt;
&lt;br /&gt;
Mutexes are more common in practice, for some examples of when to use a semaphore, please see: &lt;br /&gt;
    http://www.freertos.org/Real-time-embedded-RTOS-Counting-Semaphores.html&lt;br /&gt;
&lt;br /&gt;
When using a semaphore, you wait on it using sem_wait(), an analogy is waiting for a key to go to the washroom.&lt;br /&gt;
When youre finished with the resource, you return the key using sem_post() for someone else to use.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>PleaseEnterYourUsername</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=Talk:Operating_Systems_2017F:_Tutorial_4&amp;diff=21138</id>
		<title>Talk:Operating Systems 2017F: Tutorial 4</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=Talk:Operating_Systems_2017F:_Tutorial_4&amp;diff=21138"/>
		<updated>2017-10-20T15:50:00Z</updated>

		<summary type="html">&lt;p&gt;PleaseEnterYourUsername: Is there a bug on line 30 of 3000random.c?&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Is the modulus operator intentional or a typo? The way it is right now would cause a fill_rand_buffer() to be triggered after every use system_rand().&lt;/div&gt;</summary>
		<author><name>PleaseEnterYourUsername</name></author>
	</entry>
</feed>