<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Baron的博客</title>
  
  <link href="/atom.xml" rel="self"/>
  
  <link href="http://www.barondu.com/"/>
  <updated>2019-07-02T02:36:49.000Z</updated>
  <id>http://www.barondu.com/</id>
  
  <author>
    <name>Baron</name>
    
  </author>
  
  <generator uri="http://hexo.io/">Hexo</generator>
  
  <entry>
    <title>G52CPP复习</title>
    <link href="http://www.barondu.com/2018/11/26/G52CPP/"/>
    <id>http://www.barondu.com/2018/11/26/G52CPP/</id>
    <published>2018-11-25T19:27:27.000Z</published>
    <updated>2019-07-02T02:36:49.000Z</updated>
    
    <content type="html"><![CDATA[<h2 id="G52CPP"><a href="#G52CPP" class="headerlink" title="G52CPP"></a>G52CPP</h2><hr>
<h3 id="Lecture-1-amp-2"><a href="#Lecture-1-amp-2" class="headerlink" title="Lecture 1 &amp; 2"></a>Lecture 1 &amp; 2</h3><h5 id="Header"><a href="#Header" class="headerlink" title="Header"></a>Header</h5><figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span> 	<span class="comment">// C or C++</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cstdio&gt;</span> 	<span class="comment">// C++</span></span></div></pre></td></tr></table></figure>
<p>reference 省去 copy的过程<br>What is usually in a header file?</p>
<ol>
<li>Function declarations</li>
<li>Macro definitions (#define)</li>
<li>Constant definitions</li>
<li>Possibly other things as well<blockquote>
<ol>
<li>Ensure that the header file #includes everything that it needs itself</li>
<li>Ensure that it doesn’t matter if the header file is #included multiple times</li>
<li>Ensure that header files can be included in any order</li>
</ol>
<a id="more"></a>
</blockquote>
</li>
</ol>
<ul>
<li><code>#include</code><br>Replaces this statement by the text of the specified <strong>file</strong></li>
<li>Java’s <code>import</code></li>
<li><h5 id="Sizes-of-types"><a href="#Sizes-of-types" class="headerlink" title="Sizes of types"></a>Sizes of types</h5></li>
<li>A minimum size (bits): char 8(1 byte), short 16(2 byte), long 32(4 byte)</li>
<li>Relativesizes: char &lt;= short &lt;= int &lt;= long</li>
</ul>
<h5 id="bool-and-inttype"><a href="#bool-and-inttype" class="headerlink" title="bool and  inttype"></a><strong>bool</strong> and  <strong>int</strong>type</h5><ul>
<li>bool : <strong>true</strong> / <strong>false</strong> (C++ only, not C)</li>
<li>IMPORTANT: <code>bool</code> and <code>int</code> can be converted <strong>implicitly</strong> / <strong>automatically</strong> to each other<ul>
<li><strong>true</strong> defined to be <strong>1</strong> when converted to int </li>
<li><strong>false</strong> defined to be <strong>0</strong> when converted to int </li>
<li><strong>0</strong> is defined to be <strong>false</strong>, <strong>non-zero</strong> as true</li>
</ul>
</li>
</ul>
<blockquote>
<p>In both C and C++ any integer types can be used in conditions (i.e. char, short, long, int)<br><figure class="highlight llvm"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">int <span class="keyword">x</span> = <span class="number">6</span><span class="comment">;</span></div><div class="line">while ( <span class="keyword">x</span> )</div><div class="line">&#123;</div><div class="line">    printf( <span class="string">"X is %d\n"</span>, <span class="keyword">x</span> )<span class="comment">;</span></div><div class="line">    <span class="keyword">x</span> -= <span class="number">2</span><span class="comment">; </span></div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
</blockquote>
<figure class="highlight clean"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div></pre></td><td class="code"><pre><div class="line">##### wchar_t type</div><div class="line">wchar_t : wide character (C++ only, not C)</div><div class="line">- <span class="number">16</span> bits</div><div class="line">##### signed/unsigned value</div><div class="line">- Default is **signed**</div><div class="line">##### void</div><div class="line">- you can create a type void</div><div class="line">##### auto</div><div class="line">- new to C++<span class="number">11</span></div><div class="line">- can only used for **initialised variables**</div><div class="line">	- complier will **work out the type** at **compile time** <span class="keyword">from</span> **the initialisation value**</div><div class="line"></div><div class="line">##### #define</div><div class="line">&gt; An semi-intelligent ***‘find and replace**’* facility</div><div class="line"></div><div class="line">- Often considered `bad` <span class="keyword">in</span> C++ <span class="keyword">code</span> (useful <span class="keyword">in</span> C)</div><div class="line">	- **const** is used more often, especially for members</div><div class="line">	- **Template functions** are better than **macros**</div><div class="line">- **Remember**: Done by the pre-processor!</div><div class="line">- Constant `#define` usually written <span class="keyword">in</span> **CAPITALS**</div><div class="line"></div><div class="line">##### Conditional compilation</div><div class="line">You can remove parts <span class="keyword">of</span> the source <span class="keyword">code</span> <span class="keyword">if</span> desired</div><div class="line">- Use for **cross-platform** <span class="keyword">code</span></div></pre></td></tr></table></figure>
<p>#ifdef <strong>WINDOWS</strong><br>… windows code here …</p>
<p>#elif <strong>SYS5UNIX</strong><br>… System 5 code here …</p>
<p>#endif<br><figure class="highlight armasm"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">- Avoiding <span class="keyword">multiple </span>inclusion</div></pre></td></tr></table></figure></p>
<p>#ifndef UNIQUE_DEFINE_NAME_FOR_FILE</p>
<p>#define UNIQUE_DEFINE_NAME_FOR_FILE<br>… include the rest of the file here …</p>
<p>#endif<br><figure class="highlight markdown"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">别忘了endif</div><div class="line"></div><div class="line"><span class="section">### Lecture 3 &amp; 4</span></div><div class="line"><span class="section">##### Pointer</span></div><div class="line">![<span class="string">Alt text</span>](<span class="link">./1496502642897.png</span>)</div><div class="line"><span class="bullet">- </span><span class="code">`&amp;`</span> Address-of</div><div class="line"><span class="bullet">- </span><span class="code">`*`</span> De-referencing</div><div class="line">In C and C++, variables are <span class="strong">**NOT initialised**</span> unless you give them an <span class="strong">**initial value**</span></div><div class="line"><span class="bullet">- </span>You can use NULL</div><div class="line"><span class="bullet">- </span><span class="strong">**Dereferencing an unitialised pointer**</span> has undefined results <span class="emphasis">*Could crash your program (likely)*</span></div><div class="line"><span class="bullet">- </span>assigning one pointer to another means:</div><div class="line"><span class="code">	- It points at the same object</span></div><div class="line"><span class="code">	- It has the same address stored in it </span></div><div class="line"></div><div class="line"><span class="section">##### Casting</span></div></pre></td></tr></table></figure></p>
<p>char c1 = ‘h’;<br>char<em> pc2 = &c1;<br>int</em> pi4 = (int*)pc2;<br><figure class="highlight coffeescript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">- **Dangerous !**</div><div class="line"><span class="built_in">print</span> the address use `<span class="javascript">%p</span>` <span class="keyword">and</span> `<span class="javascript">(<span class="keyword">void</span>*)</span>`</div></pre></td></tr></table></figure></p>
<p>char c1 = ‘h’;<br>char<em> pc2 = &c1;<br>printf(“%p “,(void</em>)pc2);<br>printf(“%p\n”,(void*)&amp;c1);<br><figure class="highlight markdown"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div></pre></td><td class="code"><pre><div class="line"></div><div class="line"><span class="section">##### Arrays, C-strings and pointers</span></div><div class="line">Array</div><div class="line"><span class="bullet">- </span><span class="code">`char array[6];`</span></div><div class="line"><span class="bullet">- </span><span class="code">`char array[2] = &#123;'a','b'&#125;`</span></div><div class="line"><span class="bullet">- </span><span class="code">`char array[] = &#123;'a','b'&#125;`</span></div><div class="line"><span class="bullet">- </span>The addresses of elements <span class="strong">**within**</span> an array <span class="strong">**are**</span> consecutive</div><div class="line"><span class="bullet">- </span>The relative locations of <span class="strong">**different array**</span>s, or <span class="strong">**variables are NOT**</span> fixed</div><div class="line"></div><div class="line">C-string / char*</div><div class="line"><span class="bullet">- </span>C-strings consist of an array of characters, terminated by a character value of zero</div><div class="line"><span class="bullet">- </span>‘\0’, or 0 </div><div class="line"><span class="bullet">-  </span>NOT ‘0’!!! </div><div class="line"></div><div class="line"><span class="quote">&gt; char* str = "abcd";</span></div><div class="line"><span class="quote">&gt; str 是 ‘h’ 也就是第一个、字符的地址</span></div><div class="line"><span class="quote">&gt; char array[4] = &#123;'a','b','c','d','/0'&#125;;</span></div><div class="line"><span class="quote">&gt; char array[4] = "abcd";</span></div><div class="line"><span class="quote">&gt; array 就是 'h', 就是第一个字符</span></div><div class="line"></div><div class="line"><span class="bullet">- </span>char* 是只读的</div><div class="line">![<span class="string">12689f76eab6d982f416cc4f6e15e466.png</span>](<span class="link">evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=12689f76eab6d982f416cc4f6e15e466</span>)</div><div class="line"></div><div class="line">这里两个print出来的都是整个string</div><div class="line"></div><div class="line"><span class="section">### Lecture 5</span></div><div class="line">![<span class="string">6a4ee268a72a8b9dc7c7b83f68cba2e4.png</span>](<span class="link">evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=6a4ee268a72a8b9dc7c7b83f68cba2e4</span>)</div><div class="line"></div><div class="line"><span class="section">##### stack</span></div><div class="line"><span class="bullet">- </span>last in fist out (LIFO)</div><div class="line"><span class="bullet">- </span>Function calls (stack frames) are stored on a stack in memory</div><div class="line"><span class="bullet">- </span>stack frame</div><div class="line">![<span class="string">52a56050e9f28bc036a1906cfd328d0d.png</span>](<span class="link">evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=52a56050e9f28bc036a1906cfd328d0d</span>)</div><div class="line"></div><div class="line"><span class="section">##### Lifetime of local variables</span></div><div class="line"><span class="bullet">- </span>Your local variables only exist for as long as the block in which they are defined</div><div class="line"><span class="bullet">- </span>Do not access them after that</div><div class="line"><span class="bullet">- </span>Do not assume that they keep their value after the function ends</div><div class="line"><span class="bullet">- </span>Local variables get destroyed for you (auto-destroyed)</div><div class="line"><span class="bullet">- </span>But if you use malloc() you need to call free()</div><div class="line"></div><div class="line"><span class="section">##### Global variables</span></div><div class="line"><span class="bullet">- </span>Variable declared outside of all functions</div><div class="line"><span class="bullet">- </span>Global variables last for the duration of the program</div><div class="line"><span class="bullet">- </span>All functions in the file can access globals</div><div class="line"><span class="bullet">- </span>Not available in Java!</div><div class="line"><span class="section">##### Static local variables</span></div><div class="line">Local variables can be <span class="strong">**static**</span></div><div class="line"><span class="bullet">- </span>Means not moving/unchanging</div><div class="line"><span class="bullet">- </span>NOT the same as static member variables!</div><div class="line"><span class="bullet">- </span>NOT the same as <span class="strong">**const**</span></div><div class="line"></div><div class="line">Static local variables remember their value between function calls</div><div class="line"><span class="bullet">- </span>Like global variables</div><div class="line"></div><div class="line">But, you can only access them (by name) inside the one function they are defined in</div><div class="line"><span class="bullet">- </span>Unless you keep a pointer to them</div><div class="line"></div><div class="line">Static variable remembers its value Initialisation only occurs in the first function call</div><div class="line"><span class="section">##### Global vs local variables</span></div><div class="line">![<span class="string">552b5511aef4638112f5f5a7fb995764.png</span>](<span class="link">evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=552b5511aef4638112f5f5a7fb995764</span>)</div><div class="line"></div><div class="line">Do not refer to data on the stack outside the function</div><div class="line"><span class="bullet">- </span>This means local variables or actual parameters</div><div class="line"></div><div class="line">You CAN refer to them, but SHOULD NOT</div><div class="line">&gt;Things to avoid:</div><div class="line"><span class="quote">&gt; - Returning a pointer to a local variable or parameter </span></div><div class="line"><span class="quote">&gt; - Storing the address of a local variable or parameter</span></div><div class="line"><span class="section">##### Using multiple files</span></div><div class="line">![<span class="string">05f1607f86a43382e00ef97a21735133.png</span>](<span class="link">evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=05f1607f86a43382e00ef97a21735133</span>)</div><div class="line"><span class="bullet">- </span><span class="code">`extern`</span></div><div class="line">![<span class="string">d926279ef50ca572eaf0f5e80ee74d23.png</span>](<span class="link">evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=d926279ef50ca572eaf0f5e80ee74d23</span>)</div><div class="line"><span class="section">##### Encapsulation</span></div><div class="line">![<span class="string">a0e03a14297eb01f82df1eb75191bae6.png</span>](<span class="link">evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=a0e03a14297eb01f82df1eb75191bae6</span>)</div><div class="line"></div><div class="line"></div><div class="line"><span class="section">### Lecture 6</span></div><div class="line"><span class="section">##### struct</span></div><div class="line"><span class="code">``` </span></div><div class="line">struct Time</div><div class="line">&#123;</div><div class="line">	int hour;</div><div class="line">	int minuate;</div><div class="line">	int second;</div><div class="line">&#125;;</div><div class="line">struct Date &#123; int d, m, y; &#125;;</div></pre></td></tr></table></figure></p>
<figure class="highlight capnproto"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Date</span> dob = </span>&#123; <span class="number">1</span>, <span class="number">4</span>, <span class="number">1990</span> &#125;;</div></pre></td></tr></table></figure>
<p><strong>注意分号！</strong><br>Use <code>.</code> to access struct members</p>
<h5 id="The-heap-and-malloc"><a href="#The-heap-and-malloc" class="headerlink" title="The heap and malloc()"></a>The heap and malloc()</h5><p> <code>malloc()</code> returns a <code>void*</code><br><img src="evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=a3387c215ea60dc0e8734bae841021da" alt="a3387c215ea60dc0e8734bae841021da.png"></p>
<figure class="highlight lisp"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">int* pInt = (<span class="name">int*</span>)malloc( <span class="name">sizeof</span>(<span class="name">int</span>) )</div></pre></td></tr></table></figure>
<h5 id="Positioning-of-struct-elements"><a href="#Positioning-of-struct-elements" class="headerlink" title="Positioning of struct elements"></a>Positioning of struct elements</h5><p><img src="evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=908dc8380cabfd31644390642311987d" alt="908dc8380cabfd31644390642311987d.png"></p>
<h5 id="pragma"><a href="#pragma" class="headerlink" title="#pragma"></a>#pragma</h5><ul>
<li><code>struct</code> may get empty space in them <ul>
<li>To align members for maximum speed</li>
</ul>
</li>
</ul>
<p>-<code>#pragma</code> means a compiler/operating system specific pre-processor directive</p>
<pre><code>- `#pragma pack(1)` it will save space but will speed down
</code></pre><h5 id="union"><a href="#union" class="headerlink" title="union"></a>union</h5><ul>
<li>Elements of unions are in the SAME place</li>
<li>Elements of unions may be different sizes<ul>
<li><strong>A union is as big as the biggest thing in it</strong> </li>
</ul>
</li>
<li>Unions are a way of providing different ways of looking at the same memory<br><img src="evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=d869ddb12cd209ee24f1d586d7a29a09" alt="d869ddb12cd209ee24f1d586d7a29a09.png"></li>
</ul>
<h2 id="Lecture-7"><a href="#Lecture-7" class="headerlink" title="Lecture 7"></a>Lecture 7</h2><h5 id="classes-vs-structs-in-C"><a href="#classes-vs-structs-in-C" class="headerlink" title="classes vs structs in C++"></a>classes vs structs in C++</h5><ul>
<li>Everything you do with a <code>class</code> in C++ could also be done with a <code>struct</code></li>
<li><p>The difference is <strong>(ONLY!!!)</strong> in encapsulation</p>
<blockquote>
<p>struct defaults to public, class to private</p>
</blockquote>
</li>
<li><p>struct: Data only and no member functions</p>
</li>
<li><strong>Data</strong> should (usually) be private</li>
<li><strong>Methods</strong> (functions) should be: <ul>
<li><code>private</code> for internal use only</li>
<li><code>public</code> for the external class interface</li>
</ul>
</li>
<li>The values of the data members comprise the state of the object</li>
<li>Interface methods can be:<ul>
<li><strong>Mutators</strong> – change the ‘state’ of the object</li>
<li><strong>Accessors</strong> – only query values, no changes</li>
</ul>
</li>
<li>Use <code>this-&gt;</code>not<code>this.</code></li>
</ul>
<h5 id="Constructors-and-Destructors"><a href="#Constructors-and-Destructors" class="headerlink" title="Constructors and Destructors"></a>Constructors and Destructors</h5><p><strong>Constructor</strong> (as in Java)</p>
<ul>
<li>Called when an object is created</li>
<li>Has function name same as class name</li>
<li><strong>no return type</strong> (none/empty, NOT void!)</li>
<li>Adding a constructor makes it impossible to provide a C-style initialiser. e.g. <code>= {0,1,2};</code></li>
</ul>
<p><strong>Destructor</strong> (similar to Java finalize)</p>
<ul>
<li>Called when an object is destroyed</li>
<li>A function with name ~ then class name • E.g.: <code>~DemoClass()</code></li>
<li>And no return type</li>
</ul>
<h5 id="Default-parameters"><a href="#Default-parameters" class="headerlink" title="Default parameters"></a>Default parameters</h5><ul>
<li>Default values appear only in the function <strong>declaration</strong>, not any <strong>separate</strong> definition</li>
<li>Use `= <value>‘’<figure class="highlight stylus"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="title">DemoClass</span><span class="params">( char* dummy, int iValue = -<span class="number">1</span>)</span></span></div><div class="line">&#123; <span class="comment">/*Nothing*/</span> &#125;</div></pre></td></tr></table></figure>
</value></li>
</ul>
<h5 id="Default-Constructor"><a href="#Default-Constructor" class="headerlink" title="Default Constructor"></a>Default Constructor</h5><p>The <strong>‘Default Constructor’</strong> is a constructor which <strong>can be called</strong> with no parameters</p>
<ul>
<li>e.g. one which has no parameters</li>
<li>or has default values for all parameters</li>
<li>A class can only have one default constructor<ul>
<li>More would introduce ambiguity</li>
</ul>
</li>
</ul>
<p>When you create arrays of objects, the default constructor is used (because no parameters are provided):<code>DemoClass myDemoArray[4];</code></p>
<p><strong>IMPORTANT</strong>: Do <strong>NOT</strong> add empty brackets <code>()</code> <strong>when constructing on the stack</strong> if there are no parameters!</p>
<ul>
<li>Compiler thinks you are <strong>declaring a function</strong></li>
<li>e.g. <code>DemoClass myDemoClass1();</code> // <strong>WRONG!!!</strong></li>
</ul>
<h5 id="Initialisation-vs-Assignment"><a href="#Initialisation-vs-Assignment" class="headerlink" title="Initialisation vs Assignment"></a>Initialisation vs Assignment</h5><p><img src="evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=ec9c972cd119567ada7356a102fddb93" alt="ec9c972cd119567ada7356a102fddb93.png"><br>Compare the following:<br>1)<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">int</span> i = <span class="number">4</span>; <span class="comment">// Initialisation</span></div></pre></td></tr></table></figure></p>
<p>2)<br><figure class="highlight abnf"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">int j<span class="comment">; // Uninitialised</span></div><div class="line"><span class="attribute">j</span> = <span class="number">4</span><span class="comment">; // Assignment</span></div></pre></td></tr></table></figure></p>
<p> Member data is <strong>NOT always</strong> initialised</p>
<ul>
<li><strong>Basic types and pointers</strong> (e.g. int, short or char*) are NOT initialised</li>
<li><strong>Default constructor</strong> is <strong>called for members of type class/struct</strong> unless you say otherwise<ul>
<li>Using initialisation list</li>
</ul>
</li>
</ul>
<h5 id="Inline-functions"><a href="#Inline-functions" class="headerlink" title="Inline functions"></a>Inline functions</h5><p>Use the keyword <code>inline</code>, e.g.:<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">max</span><span class="params">( <span class="keyword">int</span> a, <span class="keyword">int</span> b )</span> </span>&#123; <span class="keyword">return</span> a&gt;b ? a : b; &#125;</div></pre></td></tr></table></figure></p>
<p>Similar to a ‘safe’ macro expansion</p>
<ul>
<li><strong>Safely</strong> replaces the function call with the code<ul>
<li>Unlike a macro (<code>#define</code>)</li>
<li>Avoids the overhead of creating a stack frame</li>
<li>Code gets included in EVERY file/function which calls it</li>
</ul>
</li>
<li>VERY useful for smal<strong>l, fast</strong> functions</li>
</ul>
<p><strong>Defining</strong> functions <strong>within</strong> the class declaration <strong>implicitly</strong> makes them inline</p>
<p>Function set default value for parameters in <strong>declaration</strong>.</p>
<h3 id="Lecture8"><a href="#Lecture8" class="headerlink" title="Lecture8"></a>Lecture8</h3><h5 id="Reference"><a href="#Reference" class="headerlink" title="Reference"></a>Reference</h5><p>A way to give a new name to an item<br><figure class="highlight matlab"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">int <span class="built_in">i</span> = <span class="number">1</span>;</div><div class="line">int&amp; <span class="built_in">j</span> = <span class="built_in">i</span>;</div></pre></td></tr></table></figure></p>
<p>References always have to refer to something </p>
<ul>
<li>Must give them a thing to refer to on initialisation </li>
<li>No such thing as a NULL reference<blockquote>
<p>do NOT return a reference to a local variable</p>
</blockquote>
</li>
</ul>
<h5 id="The-need-for-references"><a href="#The-need-for-references" class="headerlink" title="The need for references"></a>The need for references</h5><ul>
<li>Useful if we need to keep the same syntax</li>
<li>Useful as return values, to chain functions together</li>
<li>References are <strong>necessary</strong> for operator overloading</li>
</ul>
<h5 id="Passing-parameters"><a href="#Passing-parameters" class="headerlink" title="Passing parameters"></a>Passing parameters</h5><ul>
<li>When a function is called, the values of the parameters are <strong>copied</strong> into the stack frame for the new function</li>
</ul>
<h5 id="New-and-Delete"><a href="#New-and-Delete" class="headerlink" title="New and Delete"></a>New and Delete</h5><p><img src="evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=ef32056ce228ec5b633460e63656994c" alt="ef32056ce228ec5b633460e63656994c.png"><br><figure class="highlight vbnet"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">MyClass</span>* pOb = <span class="keyword">new</span> <span class="keyword">MyClass</span>;</div><div class="line">delete pOb;</div><div class="line"><span class="keyword">MyClass</span>* pObArray = <span class="keyword">new</span> <span class="keyword">MyClass</span>[<span class="number">4</span>];</div><div class="line">delete [] pObArray;</div></pre></td></tr></table></figure></p>
<p>delete destroys an object </p>
<ul>
<li>It cares about the object type</li>
<li>Calls the <strong>destructor</strong> of the class it thinks the thing is (using pointer type) <strong>and the</strong>n frees the memory<blockquote>
<ul>
<li>You MUST <code>delete</code> anything which you create<br>using new</li>
<li>You MUST <code>delete [] ...</code> any arrays which you create using <code>new ... []</code></li>
<li>You MUST <code>free</code> any memory which you<code>malloc</code>/<code>alloc</code>/<code>calloc</code>/<code>realloc</code></li>
</ul>
</blockquote>
</li>
</ul>
<p><img src="evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=24e0bd55dd6171f5d3036f058584c8bf" alt="24e0bd55dd6171f5d3036f058584c8bf.png"></p>
<blockquote>
<p>If you want to <strong>create</strong> objects <strong>in dynamic memory</strong> then you <strong>must</strong> go through <code>new</code><br>You can use <code>new</code> on basic types (e.g.int)<br>Array <code>new []</code> uses the default constructor for objects, and does not initialise basic types</p>
</blockquote>
<p><img src="evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=cb30edd5ec403dbf58ac40a90c20178e" alt="cb30edd5ec403dbf58ac40a90c20178e.png"></p>
<h5 id="Lecture-9"><a href="#Lecture-9" class="headerlink" title="Lecture 9"></a>Lecture 9</h5><h5 id="this-and-static"><a href="#this-and-static" class="headerlink" title="this and static"></a>this and static</h5><p>Static member functions do not have a <code>this</code> pointer<br><img src="evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=67c0ba8529a038b39d516e37f601c6e1" alt="67c0ba8529a038b39d516e37f601c6e1.png"></p>
<h5 id="const"><a href="#const" class="headerlink" title="const"></a>const</h5><p> Pointers to constant data<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> <span class="keyword">char</span> * p=“Hello”; </div><div class="line"><span class="keyword">char</span> <span class="keyword">const</span> * p=“Hello”;</div></pre></td></tr></table></figure></p>
<p> Constant pointers</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">char</span>* <span class="keyword">const</span> p = “Hello”;</div></pre></td></tr></table></figure>
<p><img src="evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=ff801fa4ef0ecfb70a1db868e7ea273f" alt="ff801fa4ef0ecfb70a1db868e7ea273f.png"></p>
<h5 id="String-literal"><a href="#String-literal" class="headerlink" title="String literal"></a>String literal</h5><p>String literals should not be changed<br><figure class="highlight processing"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> <span class="built_in">char</span>* <span class="built_in">str</span> = “Hello”;</div></pre></td></tr></table></figure></p>
<h5 id="const-references"><a href="#const-references" class="headerlink" title="const references"></a>const references</h5><ul>
<li><code>const</code> references make the thing referred to const</li>
<li><code>const</code> references are useful for parameters<ul>
<li>Passing by value (not reference) means the original variable (safer)<br>cannot be accidentally modified </li>
<li>Passing a reference means that no copy is made (maybe quicker)</li>
</ul>
</li>
<li>Using a const reference means no copy needs to be made, but the original can still not be changed, <strong>like a copy but faster</strong><br><img src="evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=80f3cb928a8018b98d3d8c668ff8f90e" alt="80f3cb928a8018b98d3d8c668ff8f90e.png"></li>
</ul>
<h5 id="mutable"><a href="#mutable" class="headerlink" title="mutable"></a>mutable</h5><p><img src="evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=0d8cd472d9a6254ba005ecf8e110fa2b" alt="0d8cd472d9a6254ba005ecf8e110fa2b.png"></p>
<p>提前 奶一口 const 就能改了</p>
<h5 id="friend"><a href="#friend" class="headerlink" title="friend"></a>friend</h5><p><img src="evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=f50408dc9ca5f56cbfb1597f1888c204" alt="f50408dc9ca5f56cbfb1597f1888c204.png"></p>
<p><img src="evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=20a5378bac3d99f5ce1e54ac8339dc3c" alt="20a5378bac3d99f5ce1e54ac8339dc3c.png"></p>
<p>friend 写在 被用的那个class里，就可以访问private的了</p>
<h3 id="Lecture-10"><a href="#Lecture-10" class="headerlink" title="Lecture 10"></a>Lecture 10</h3><h5 id="Namespace"><a href="#Namespace" class="headerlink" title="Namespace"></a>Namespace</h5><p>Namespaces are used to avoid name conflicts </p>
<ul>
<li>Only the name is affected<br><code>namespace &lt;NamespaceName&gt;{}</code></li>
</ul>
<figure class="highlight elixir"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">&lt;namespace&gt;<span class="symbol">:</span><span class="symbol">:&lt;class&gt;</span><span class="symbol">:</span><span class="symbol">:&lt;function&gt;</span></div><div class="line"><span class="symbol">MyNameSpace:</span><span class="symbol">:MyClass</span><span class="symbol">:</span><span class="symbol">:foo</span>();</div><div class="line">&lt;namespace&gt;<span class="symbol">:</span><span class="symbol">:&lt;globalfunction&gt;</span> </div><div class="line"><span class="symbol">MyNameSpace:</span><span class="symbol">:bar</span>();</div><div class="line"></div><div class="line">using namespace &lt;namespace&gt;</div></pre></td></tr></table></figure>
<h5 id="scoping"><a href="#scoping" class="headerlink" title="scoping"></a>scoping</h5><p><code>::</code>Left of scoping operator is</p>
<ul>
<li><strong>blank</strong> (to access a global variable/function)</li>
<li><strong>class name</strong> (to access member of that class)</li>
<li><strong>namespace name</strong> (to use that namespace)</li>
</ul>
<h5 id="streams-for-input-output"><a href="#streams-for-input-output" class="headerlink" title="streams for input/output"></a>streams for input/output</h5><p><img src="evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=7e47ed3f2786a6f2b291711287938712" alt="7e47ed3f2786a6f2b291711287938712.png"></p>
<h5 id="File-access-using-streams"><a href="#File-access-using-streams" class="headerlink" title="File access using streams"></a>File access using streams</h5><ul>
<li><code>ifstream</code> object - open the file for input</li>
<li><code>ofstream</code> object - open the file for output</li>
<li><code>fstream</code> object – specify what to open file for</li>
</ul>
<h5 id="stringstream"><a href="#stringstream" class="headerlink" title="stringstream"></a>stringstream</h5><p><img src="evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=083ab4900f76d981996bd34eba875257" alt="083ab4900f76d981996bd34eba875257.png"></p>
<h3 id="Lecture-11"><a href="#Lecture-11" class="headerlink" title="Lecture 11"></a>Lecture 11</h3><p><img src="evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=44e5ec986fe4f5ade5424f8ebaf8dc8b" alt="44e5ec986fe4f5ade5424f8ebaf8dc8b.png"></p>
<p><img src="evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=4c71ba063f5c40c8849d84cc4a574e6b" alt="4c71ba063f5c40c8849d84cc4a574e6b.png"></p>
<p>没有virtual的时候<code>pSubAsBase-&gt;foo()</code>是调用父类的<br>如果父类方法 virtual之后，再这么访问，要看<code>pSubAsBase</code>到底是什么class</p>
<p>vitual 之后想用父类的function 就要 用到 <code>::</code><br><img src="evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=cb89b4a32cd281517449b5e582872cdc" alt="cb89b4a32cd281517449b5e582872cdc.png"></p>
<p>1 2 3<br>4 5 6</p>
<h5 id="Inheritance-and-constructors"><a href="#Inheritance-and-constructors" class="headerlink" title="Inheritance and constructors"></a>Inheritance and constructors</h5><p>create in stack<br><img src="evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=1f77f23c106a84a64a598ba9ee7a9435" alt="1f77f23c106a84a64a598ba9ee7a9435.png"></p>
<p>create in heap<br><img src="evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=b30b898d6bf1d16929d372beee785e65" alt="b30b898d6bf1d16929d372beee785e65.png"></p>
<ul>
<li>Do not call virtual functions from the constructor or destructor</li>
</ul>
<p><img src="evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=2dddaf0fd087702683124fbe82e56537" alt="2dddaf0fd087702683124fbe82e56537.png"></p>
<p>想要也删掉sub的，把父类的destructor加virtual<br><img src="evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=8ef4ca3a01b939007a0f2de0cfef5fa2" alt="8ef4ca3a01b939007a0f2de0cfef5fa2.png"></p>
<h3 id="Lecture-13"><a href="#Lecture-13" class="headerlink" title="Lecture 13"></a>Lecture 13</h3><h5 id="Function-pointers"><a href="#Function-pointers" class="headerlink" title="Function pointers"></a>Function pointers</h5><p> Function pointers<br> <img src="evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=239761c804da4fbc17d87e8985b2148f" alt="239761c804da4fbc17d87e8985b2148f.png"></p>
<p><img src="evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=1c52f0decc3f8b926dd9ae46068266de" alt="1c52f0decc3f8b926dd9ae46068266de.png"></p>
<p> 括号里是paraeter</p>
<ul>
<li>callback function<br><img src="evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=088270f898f3aff1ce3a8cf83c9fae60" alt="088270f898f3aff1ce3a8cf83c9fae60.png"></li>
</ul>
<h5 id="Virtual-and-non-virtual-functions"><a href="#Virtual-and-non-virtual-functions" class="headerlink" title="Virtual and non-virtual functions"></a>Virtual and non-virtual functions</h5><p><img src="evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=3975d70c9da8d9aed62fdb091e6d86d5" alt="3975d70c9da8d9aed62fdb091e6d86d5.png"></p>
<h3 id="Lecture-14"><a href="#Lecture-14" class="headerlink" title="Lecture 14"></a>Lecture 14</h3><p>4 functions created by default <strong>if needed</strong></p>
<ul>
<li>You can make them unavailable (e.g. private)</li>
</ul>
<blockquote>
<ol>
<li>A defaul tconstructor(no parameters needed) </li>
<li>A copy constructor (copy one object to another)</li>
<li>An assignment operator (=operator)</li>
<li>A destructor</li>
</ol>
</blockquote>
<h5 id="A-default-constructor"><a href="#A-default-constructor" class="headerlink" title="A default constructor"></a>A default constructor</h5><ul>
<li>Automatically created <strong>if and only if</strong> you do NOT create any other constructors</li>
</ul>
<h5 id="The-Copy-Constructor"><a href="#The-Copy-Constructor" class="headerlink" title="The Copy Constructor"></a>The Copy Constructor</h5><ul>
<li><p>The <strong>copy constructor</strong> is used to <strong>initialise</strong> one object from another <strong>of the same type</strong></p>
<figure class="highlight nimrod"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="type">MyClass</span>( <span class="keyword">const</span> <span class="type">MyClass</span>&amp; rhs )</div><div class="line"><span class="meta">&#123;...&#125;</span></div></pre></td></tr></table></figure>
</li>
<li><p>Takes a <strong>constant reference</strong> to the object to copy from</p>
</li>
<li><strong>Has to be a reference!</strong><br><img src="evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=ff34c6d257eb117256d4547061d4d722" alt="ff34c6d257eb117256d4547061d4d722.png"></li>
</ul>
<h5 id="Assignment-operator"><a href="#Assignment-operator" class="headerlink" title="Assignment operator"></a>Assignment operator</h5><ul>
<li>Used when value of one object is assigned to another</li>
<li><code>ob1=ob2=ob3=ob4;</code></li>
<li><img src="evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=ddd6c53e9de96359d88867c23e79e495" alt="ddd6c53e9de96359d88867c23e79e495.png"></li>
</ul>
<h5 id="Destructor"><a href="#Destructor" class="headerlink" title="Destructor"></a>Destructor</h5><ul>
<li><strong>Default destructor does nothing</strong></li>
</ul>
<h5 id="Conversion-constructor"><a href="#Conversion-constructor" class="headerlink" title="Conversion constructor"></a>Conversion constructor</h5><ul>
<li>A conversion constructor is <strong>a constructor with one parameter.</strong><figure class="highlight lasso"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">MyClass( char c )</div><div class="line">&#123; <span class="params">...</span> <span class="keyword">do</span> something <span class="keyword">with</span> c <span class="params">...</span> &#125;</div><div class="line">MyClass ob = ‘h’;</div></pre></td></tr></table></figure>
</li>
</ul>
<p>int 就可以这么用<br><img src="evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=09b26709fb14dc3f167476cae7b9da90" alt="09b26709fb14dc3f167476cae7b9da90.png"></p>
<h5 id="conversion-operator"><a href="#conversion-operator" class="headerlink" title="conversion operator"></a>conversion operator</h5><p><img src="evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=410f5e9fca1f84660a1aedb7ff0ab2a3" alt="410f5e9fca1f84660a1aedb7ff0ab2a3.png"></p>
<p><code>explict</code><br>Providing a <strong>one-parameter</strong> constructor provides a conversion constructor<br><img src="evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=d8eddd2eb7ccb5d66c95a990e6f558f6" alt="d8eddd2eb7ccb5d66c95a990e6f558f6.png"></p>
<h3 id="Lecture-15"><a href="#Lecture-15" class="headerlink" title="Lecture 15"></a>Lecture 15</h3><h5 id="Casting"><a href="#Casting" class="headerlink" title="Casting"></a>Casting</h5><ul>
<li>static cast</li>
<li>dynamic cast</li>
<li>const cast</li>
<li>reinterpret cast</li>
</ul>
<p>casting-ness是强转比如<code>int&amp; xr = (int&amp;)(x);</code>，尽量避免<br><img src="evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=c07fbdb853c44fd167b8888c3ac56c1c" alt="c07fbdb853c44fd167b8888c3ac56c1c.png"></p>
<p>dynamic 失败的时候会抛出exception 所以，sub base class转的时候。用dynamic.<br>Pointer:<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">BouncingBall game;</div><div class="line">BaseEngine* pGame = &amp;game; <span class="comment">// No cast</span></div><div class="line">BouncingBall* pmGame =</div><div class="line">    <span class="keyword">dynamic_cast</span>&lt;BouncingBall*&gt;(pGame);</div><div class="line"><span class="keyword">if</span> ( pGame==<span class="literal">NULL</span> ) &#123; <span class="comment">/* Failed */</span> &#125;</div></pre></td></tr></table></figure></p>
<p>Reference:<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">BouncingBall game;</div><div class="line">BaseGameEngine&amp; rgame = game; <span class="comment">// No cast</span></div><div class="line"><span class="keyword">try</span></div><div class="line">&#123;</div><div class="line">BouncingBall&amp; rmgame =</div><div class="line">      <span class="keyword">dynamic_cast</span>&lt;BouncingBall&amp;&gt;(rgame);</div><div class="line">&#125;</div><div class="line"><span class="keyword">catch</span> ( <span class="built_in">std</span>::bad_cast b )</div><div class="line">&#123;</div><div class="line"><span class="comment">// Handle the exception</span></div><div class="line"><span class="comment">// Happens if rgame is NOT a BouncingBall</span></div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<h5 id="Operator-overloading"><a href="#Operator-overloading" class="headerlink" title="Operator overloading"></a>Operator overloading</h5><p>non-mumber function<br>别忘了<code>friend</code><br><figure class="highlight maxima"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">MyFloat operator-( const MyFloat&amp; <span class="built_in">lhs</span>, const MyFloat&amp; <span class="built_in">rhs</span> ) &#123;</div><div class="line">MyFloat temp(<span class="built_in">lhs</span>.strName + <span class="string">"-"</span> + <span class="built_in">rhs</span>.strName, <span class="built_in">lhs</span>.f - <span class="built_in">rhs</span>.f);</div><div class="line"><span class="built_in">return</span> temp;</div></pre></td></tr></table></figure></p>
<p>mumber function<br><img src="evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=98fb08771a499045421cb166a5326e61" alt="98fb08771a499045421cb166a5326e61.png"></p>
<h5 id="Operator-overloading-restrictions"><a href="#Operator-overloading-restrictions" class="headerlink" title="Operator overloading restrictions"></a>Operator overloading restrictions</h5><p><img src="evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=3fafe3eab0ac9eb02564851e864d379d" alt="3fafe3eab0ac9eb02564851e864d379d.png"></p>
<p><img src="evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=e62601d73c46a1511c4f35975a0364fc" alt="e62601d73c46a1511c4f35975a0364fc.png"></p>
<h3 id="Lecture-16"><a href="#Lecture-16" class="headerlink" title="Lecture 16"></a>Lecture 16</h3><h5 id="and"><a href="#and" class="headerlink" title="= and !="></a>= and !=</h5><p><img src="evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=ce7266d4a5941b51140e0cdfd64cae07" alt="ce7266d4a5941b51140e0cdfd64cae07.png"></p>
<h5 id="and-1"><a href="#and-1" class="headerlink" title="+ and +="></a>+ and +=</h5><p><img src="evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=6698812de9d8c1a0e15ef834e2416508" alt="6698812de9d8c1a0e15ef834e2416508.png"></p>
<h5 id="template"><a href="#template" class="headerlink" title="template"></a>template</h5><figure class="highlight r"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">template &lt; typename <span class="literal">T</span> &gt;</div><div class="line"><span class="literal">T</span> mymax( <span class="literal">T</span> a, <span class="literal">T</span> b )</div><div class="line">&#123; <span class="keyword">return</span> a &gt; b ? a : b; &#125;</div></pre></td></tr></table></figure>
<p>The compiler will a<strong>ctually generate the functions which are needed,</strong> according to the parameters</p>
<h3 id="Lecture-17"><a href="#Lecture-17" class="headerlink" title="Lecture 17"></a>Lecture 17</h3><h5 id="Functor"><a href="#Functor" class="headerlink" title="Functor"></a>Functor</h5><p>Functors are classes which overload the () operator<br><img src="evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=3653bec9375b9a8abf3e4d3544ccecf6" alt="3653bec9375b9a8abf3e4d3544ccecf6.png"><br><img src="evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=f67a50564f89ddc4a3fc908866bf4824" alt="f67a50564f89ddc4a3fc908866bf4824.png"></p>
<h3 id="Lecture-18"><a href="#Lecture-18" class="headerlink" title="Lecture 18"></a>Lecture 18</h3><h5 id="How-do-we-report-errors"><a href="#How-do-we-report-errors" class="headerlink" title="How do we report errors?"></a>How do we report errors?</h5><ul>
<li>Return an error value from function</li>
<li>Set a global error code</li>
<li>Throw an exception (to report error)</li>
<li><h5 id="catch"><a href="#catch" class="headerlink" title="catch"></a>catch</h5></li>
<li><code>catch</code> clauses are ch<strong>ecked in the order in which they are encountered</strong></li>
<li>Exceptions are thrown by value</li>
<li>Catch by reference or by value would work - Catch by reference avoids the copy</li>
</ul>
<blockquote>
<p><code>catch ( ... )</code> will match ANY exception<br><code>catch ( BaseClass&amp; b ) { }</code>will also catch sub-class objects<br><code>catch ( BaseClass* b ) { }</code>will also catch sub-class pointers</p>
</blockquote>
<h5 id="Lambda"><a href="#Lambda" class="headerlink" title="Lambda"></a>Lambda</h5><p>Lambdas are anonymous <strong>functors</strong> that get created on the fly</p>
<h5 id="Basic-lambda-function"><a href="#Basic-lambda-function" class="headerlink" title="Basic lambda function:"></a>Basic lambda function:</h5><figure class="highlight markdown"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[Capture] ( parameters ) -&gt; return_type &#123; function body &#125;</div><div class="line">[<span class="string">iAdd</span>](<span class="link"> int i </span>)-&gt;int &#123; return i + iAdd; &#125;</div></pre></td></tr></table></figure>
<p><img src="evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=34ec51d2072e535047267de18f4c70c8" alt="34ec51d2072e535047267de18f4c70c8.png"></p>
<p><img src="evernotecid://A4C2A14E-E2AB-40E7-9264-1D440DF81A61/appyinxiangcom/14488959/ENNote/p60?hash=376fe6aec0ddad80318bef110af99859" alt="376fe6aec0ddad80318bef110af99859.png"></p>
]]></content>
    
    <summary type="html">
    
      &lt;h2 id=&quot;G52CPP&quot;&gt;&lt;a href=&quot;#G52CPP&quot; class=&quot;headerlink&quot; title=&quot;G52CPP&quot;&gt;&lt;/a&gt;G52CPP&lt;/h2&gt;&lt;hr&gt;
&lt;h3 id=&quot;Lecture-1-amp-2&quot;&gt;&lt;a href=&quot;#Lecture-1-amp-2&quot; class=&quot;headerlink&quot; title=&quot;Lecture 1 &amp;amp; 2&quot;&gt;&lt;/a&gt;Lecture 1 &amp;amp; 2&lt;/h3&gt;&lt;h5 id=&quot;Header&quot;&gt;&lt;a href=&quot;#Header&quot; class=&quot;headerlink&quot; title=&quot;Header&quot;&gt;&lt;/a&gt;Header&lt;/h5&gt;&lt;figure class=&quot;highlight cpp&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre&gt;&lt;div class=&quot;line&quot;&gt;1&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;2&lt;/div&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;div class=&quot;line&quot;&gt;&lt;span class=&quot;meta&quot;&gt;#&lt;span class=&quot;meta-keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;meta-string&quot;&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt; 	&lt;span class=&quot;comment&quot;&gt;// C or C++&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;&lt;span class=&quot;meta&quot;&gt;#&lt;span class=&quot;meta-keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;meta-string&quot;&gt;&amp;lt;cstdio&amp;gt;&lt;/span&gt; 	&lt;span class=&quot;comment&quot;&gt;// C++&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/figure&gt;
&lt;p&gt;reference 省去 copy的过程&lt;br&gt;What is usually in a header file?&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Function declarations&lt;/li&gt;
&lt;li&gt;Macro definitions (#define)&lt;/li&gt;
&lt;li&gt;Constant definitions&lt;/li&gt;
&lt;li&gt;Possibly other things as well&lt;blockquote&gt;
&lt;ol&gt;
&lt;li&gt;Ensure that the header file #includes everything that it needs itself&lt;/li&gt;
&lt;li&gt;Ensure that it doesn’t matter if the header file is #included multiple times&lt;/li&gt;
&lt;li&gt;Ensure that header files can be included in any order&lt;/li&gt;
&lt;/ol&gt;
    
    </summary>
    
      <category term="笔记" scheme="http://www.barondu.com/categories/%E7%AC%94%E8%AE%B0/"/>
    
    
      <category term="CPP" scheme="http://www.barondu.com/tags/CPP/"/>
    
      <category term="C++" scheme="http://www.barondu.com/tags/C/"/>
    
  </entry>
  
  <entry>
    <title>PyCharm运行TensorBoard时的小BUG</title>
    <link href="http://www.barondu.com/2018/02/21/PyCharm%E8%BF%90%E8%A1%8CTensorBoard%E6%97%B6%E7%9A%84%E5%B0%8FBUG/"/>
    <id>http://www.barondu.com/2018/02/21/PyCharm运行TensorBoard时的小BUG/</id>
    <published>2018-02-21T08:03:41.000Z</published>
    <updated>2019-07-01T20:49:14.000Z</updated>
    
    <content type="html"><![CDATA[<h2 id="PyCharm运行TensorBoard时的小BUG"><a href="#PyCharm运行TensorBoard时的小BUG" class="headerlink" title="PyCharm运行TensorBoard时的小BUG"></a>PyCharm运行TensorBoard时的小BUG</h2><p>@(G53IDS)[PyCharm, TensorBoard, BUG]<br>今天在PyCharm的Terminal使用TensorBoard的时候出现了以下报错，但是mac自带的terminal却可以正常使用：</p>
<figure class="highlight vhdl"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line"># tensorboard <span class="comment">--logdir=logs</span></div><div class="line">Traceback (most recent call last):</div><div class="line">  <span class="keyword">File</span> <span class="string">"/Users/baron/.pyenv/versions/3.6.3/bin/tensorboard"</span>, <span class="literal">line</span> <span class="number">11</span>, <span class="keyword">in</span> &lt;module&gt;</div><div class="line">    sys.<span class="keyword">exit</span>(main())</div><div class="line">  <span class="keyword">File</span> <span class="string">"/Users/baron/.pyenv/versions/3.6.3/lib/python3.6/site-packages/tensorboard/main.py"</span>, <span class="literal">line</span> <span class="number">45</span>, <span class="keyword">in</span> main</div><div class="line">    <span class="keyword">default</span>.get_assets_zip_provider())</div><div class="line">  <span class="keyword">File</span> <span class="string">"/Users/baron/.pyenv/versions/3.6.3/lib/python3.6/site-packages/tensorboard/program.py"</span>, <span class="literal">line</span> <span class="number">147</span>, <span class="keyword">in</span> main</div><div class="line">    util.setup_logging()</div><div class="line">  <span class="keyword">File</span> <span class="string">"/Users/baron/.pyenv/versions/3.6.3/lib/python3.6/site-packages/tensorboard/util.py"</span>, <span class="literal">line</span> <span class="number">50</span>, <span class="keyword">in</span> setup_logging</div><div class="line">    locale.setlocale(locale.LC_ALL, '')</div><div class="line">  <span class="keyword">File</span> <span class="string">"/Users/baron/.pyenv/versions/3.6.3/lib/python3.6/locale.py"</span>, <span class="literal">line</span> <span class="number">598</span>, <span class="keyword">in</span> setlocale</div><div class="line">    <span class="keyword">return</span> _setlocale(category, locale)</div><div class="line">locale.<span class="literal">Error</span>: unsupported locale setting</div></pre></td></tr></table></figure>
<a id="more"></a>
<p>输入locale查看下是什么状况</p>
<figure class="highlight ini"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># locale</span></div><div class="line"><span class="attr">LANG</span>=</div><div class="line"><span class="attr">LC_COLLATE</span>=<span class="string">"C"</span></div><div class="line"><span class="attr">LC_CTYPE</span>=<span class="string">"C"</span></div><div class="line"><span class="attr">LC_MESSAGES</span>=<span class="string">"C"</span></div><div class="line"><span class="attr">LC_MONETARY</span>=<span class="string">"C"</span></div><div class="line"><span class="attr">LC_NUMERIC</span>=<span class="string">"C"</span></div><div class="line"><span class="attr">LC_TIME</span>=<span class="string">"C"</span></div><div class="line"><span class="attr">LC_ALL</span>=</div></pre></td></tr></table></figure>
<p>发现原来是语言设置除了问题，<code>LC_ALL</code>后面什么都没有，于是我们的解决办法就是</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">export</span> LC_ALL=<span class="string">"C"</span></div></pre></td></tr></table></figure>
<p>然后世界就和平了(｡･ω･｡)ﾉ</p>
]]></content>
    
    <summary type="html">
    
      &lt;h2 id=&quot;PyCharm运行TensorBoard时的小BUG&quot;&gt;&lt;a href=&quot;#PyCharm运行TensorBoard时的小BUG&quot; class=&quot;headerlink&quot; title=&quot;PyCharm运行TensorBoard时的小BUG&quot;&gt;&lt;/a&gt;PyCharm运行TensorBoard时的小BUG&lt;/h2&gt;&lt;p&gt;@(G53IDS)[PyCharm, TensorBoard, BUG]&lt;br&gt;今天在PyCharm的Terminal使用TensorBoard的时候出现了以下报错，但是mac自带的terminal却可以正常使用：&lt;/p&gt;
&lt;figure class=&quot;highlight vhdl&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre&gt;&lt;div class=&quot;line&quot;&gt;1&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;2&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;3&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;4&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;5&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;6&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;7&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;8&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;9&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;10&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;11&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;12&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;13&lt;/div&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;div class=&quot;line&quot;&gt;# tensorboard &lt;span class=&quot;comment&quot;&gt;--logdir=logs&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;Traceback (most recent call last):&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;  &lt;span class=&quot;keyword&quot;&gt;File&lt;/span&gt; &lt;span class=&quot;string&quot;&gt;&quot;/Users/baron/.pyenv/versions/3.6.3/bin/tensorboard&quot;&lt;/span&gt;, &lt;span class=&quot;literal&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;number&quot;&gt;11&lt;/span&gt;, &lt;span class=&quot;keyword&quot;&gt;in&lt;/span&gt; &amp;lt;module&amp;gt;&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;    sys.&lt;span class=&quot;keyword&quot;&gt;exit&lt;/span&gt;(main())&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;  &lt;span class=&quot;keyword&quot;&gt;File&lt;/span&gt; &lt;span class=&quot;string&quot;&gt;&quot;/Users/baron/.pyenv/versions/3.6.3/lib/python3.6/site-packages/tensorboard/main.py&quot;&lt;/span&gt;, &lt;span class=&quot;literal&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;number&quot;&gt;45&lt;/span&gt;, &lt;span class=&quot;keyword&quot;&gt;in&lt;/span&gt; main&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;    &lt;span class=&quot;keyword&quot;&gt;default&lt;/span&gt;.get_assets_zip_provider())&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;  &lt;span class=&quot;keyword&quot;&gt;File&lt;/span&gt; &lt;span class=&quot;string&quot;&gt;&quot;/Users/baron/.pyenv/versions/3.6.3/lib/python3.6/site-packages/tensorboard/program.py&quot;&lt;/span&gt;, &lt;span class=&quot;literal&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;number&quot;&gt;147&lt;/span&gt;, &lt;span class=&quot;keyword&quot;&gt;in&lt;/span&gt; main&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;    util.setup_logging()&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;  &lt;span class=&quot;keyword&quot;&gt;File&lt;/span&gt; &lt;span class=&quot;string&quot;&gt;&quot;/Users/baron/.pyenv/versions/3.6.3/lib/python3.6/site-packages/tensorboard/util.py&quot;&lt;/span&gt;, &lt;span class=&quot;literal&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;number&quot;&gt;50&lt;/span&gt;, &lt;span class=&quot;keyword&quot;&gt;in&lt;/span&gt; setup_logging&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;    locale.setlocale(locale.LC_ALL, &#39;&#39;)&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;  &lt;span class=&quot;keyword&quot;&gt;File&lt;/span&gt; &lt;span class=&quot;string&quot;&gt;&quot;/Users/baron/.pyenv/versions/3.6.3/lib/python3.6/locale.py&quot;&lt;/span&gt;, &lt;span class=&quot;literal&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;number&quot;&gt;598&lt;/span&gt;, &lt;span class=&quot;keyword&quot;&gt;in&lt;/span&gt; setlocale&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;    &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; _setlocale(category, locale)&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;locale.&lt;span class=&quot;literal&quot;&gt;Error&lt;/span&gt;: unsupported locale setting&lt;/div&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/figure&gt;
    
    </summary>
    
      <category term="随笔" scheme="http://www.barondu.com/categories/%E9%9A%8F%E7%AC%94/"/>
    
    
      <category term="Machine Learning" scheme="http://www.barondu.com/tags/Machine-Learning/"/>
    
      <category term="TensorFlow" scheme="http://www.barondu.com/tags/TensorFlow/"/>
    
      <category term="pyenv" scheme="http://www.barondu.com/tags/pyenv/"/>
    
      <category term="PyCharm" scheme="http://www.barondu.com/tags/PyCharm/"/>
    
  </entry>
  
  <entry>
    <title>Hexo以及yilia主题个性化设置</title>
    <link href="http://www.barondu.com/2017/11/26/Hexo%E4%BB%A5%E5%8F%8Ayilia%E4%B8%BB%E9%A2%98%E4%B8%AA%E6%80%A7%E5%8C%96%E8%AE%BE%E7%BD%AE/"/>
    <id>http://www.barondu.com/2017/11/26/Hexo以及yilia主题个性化设置/</id>
    <published>2017-11-25T19:11:27.000Z</published>
    <updated>2019-07-01T21:24:56.000Z</updated>
    
    <content type="html"><![CDATA[<h1 id="Hexo以及yilia主题个性化设置"><a href="#Hexo以及yilia主题个性化设置" class="headerlink" title="Hexo以及yilia主题个性化设置"></a>Hexo以及yilia主题个性化设置</h1><h3 id="修改浏览器Tab里的icon"><a href="#修改浏览器Tab里的icon" class="headerlink" title="修改浏览器Tab里的icon"></a>修改浏览器Tab里的icon</h3><p>修改后效果如下<br><img src="https://app.yinxiang.com/shard/s67/res/b7a1bfae-d09c-4a00-be66-a474d57e6378/20171123140602_rXDDHa_Screenshot.jpg" alt="aaaaa"></p>
<ul>
<li>很简单我们只需要在yilia主题里<code>_config.yml</code>文件里修改为<code>favicon: /img/favicon.ico</code></li>
<li>将自己想要的icon，转换成<code>.ico</code>格式修改命名放在<code>yilia/sourse/img</code>里，大功告成</li>
</ul>
<a id="more"></a>
<h3 id="添加评论模块"><a href="#添加评论模块" class="headerlink" title="添加评论模块"></a>添加评论模块</h3><p>评论系统真的是折腾死我了，国内第三方免费平台接连停止服务</p>
<h4 id="都有那些常用的评论系统"><a href="#都有那些常用的评论系统" class="headerlink" title="都有那些常用的评论系统"></a>都有那些常用的评论系统</h4><ul>
<li>最开始用的<strong>多说</strong>，省心免费虽然总是挂掉，今年7月份就停止服务了</li>
<li><strong>网易云跟帖</strong>也宣布即将停止服务。<br>Gitment是一款一款基于GitHub Issues 的评论系统，所以只能用GitHub帐号登录</li>
<li><strong>Disqus</strong>则已经被墙</li>
<li>目前可选择的评论系统还有<strong>友言</strong>，<strong>畅言</strong>，<strong>来必力</strong>等</li>
<li><strong>畅言</strong>最蛋疼的是需要你的域名已经在国内备案过（内心飘过一万只草泥马</li>
</ul>
<h4 id="如何添加来必力评论系统"><a href="#如何添加来必力评论系统" class="headerlink" title="如何添加来必力评论系统"></a>如何添加来必力评论系统</h4><p>我最终选择了<a href="https://livere.com/" target="_blank" rel="external">来必力</a>，虽然它总公司是韩国的，但是看在免费且支持的第三方登录的种类的面子上，就选它了。</p>
<ol>
<li>注册帐户，登录选择个人网站安装city免费版就好，复制它给你的安装代码</li>
</ol>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line"><span class="comment">&lt;!-- 来必力City版安装代码 --&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"lv-container"</span> <span class="attr">data-id</span>=<span class="string">"city"</span> <span class="attr">data-uid</span>=<span class="string">"切记这里改成你账户里自己的ID"</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">"text/javascript"</span>&gt;</span><span class="javascript"></span></div><div class="line">   (<span class="function"><span class="keyword">function</span>(<span class="params">d, s</span>) </span>&#123;</div><div class="line">       <span class="keyword">var</span> j, e = d.getElementsByTagName(s)[<span class="number">0</span>];</div><div class="line"></div><div class="line">       <span class="keyword">if</span> (<span class="keyword">typeof</span> LivereTower === <span class="string">'function'</span>) &#123; <span class="keyword">return</span>; &#125;</div><div class="line"></div><div class="line">       j = d.createElement(s);</div><div class="line">       j.src = <span class="string">'https://cdn-city.livere.com/js/embed.dist.js'</span>;</div><div class="line">       j.async = <span class="literal">true</span>;</div><div class="line"></div><div class="line">       e.parentNode.insertBefore(j, e);</div><div class="line">   &#125;)(<span class="built_in">document</span>, <span class="string">'script'</span>);</div><div class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">noscript</span>&gt;</span>为正常使用来必力评论功能请激活JavaScript<span class="tag">&lt;/<span class="name">noscript</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line"><span class="comment">&lt;!-- City版安装代码已完成 --&gt;</span></div></pre></td></tr></table></figure>
<ol>
<li>在<code>themes\yilia\layout\_partial\post</code> 目录下<br>新建一个livere.ejs文件，粘贴之前的安装代码然后save</li>
<li>在<code>themes\yilia\_config.yml</code>里<code>duoshuo: false</code>前添加<code>livere: true</code>注意顺序</li>
<li><code>themes\yilia\layout\_partial\article.ejs</code>中<br><code>&lt;% if (!index &amp;&amp; post.comments){ %&gt;</code>这行代码下面添加</li>
</ol>
<figure class="highlight gcode"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">&lt;<span class="meta">%</span> <span class="keyword">if</span> <span class="comment">(theme.livere)</span>&#123; <span class="meta">%</span>&gt;</div><div class="line">&lt;<span class="meta">%</span>- partial<span class="comment">('post/livere', &#123;</span></div><div class="line">    key: post.slug,</div><div class="line">    title: post.title,</div><div class="line">    url: config.url+url_for(post.path)</div><div class="line">  &#125;) <span class="meta">%</span>&gt;</div><div class="line">&lt;<span class="meta">%</span> &#125; <span class="meta">%</span>&gt;</div></pre></td></tr></table></figure>
<ol>
<li>Done！</li>
</ol>
]]></content>
    
    <summary type="html">
    
      &lt;h1 id=&quot;Hexo以及yilia主题个性化设置&quot;&gt;&lt;a href=&quot;#Hexo以及yilia主题个性化设置&quot; class=&quot;headerlink&quot; title=&quot;Hexo以及yilia主题个性化设置&quot;&gt;&lt;/a&gt;Hexo以及yilia主题个性化设置&lt;/h1&gt;&lt;h3 id=&quot;修改浏览器Tab里的icon&quot;&gt;&lt;a href=&quot;#修改浏览器Tab里的icon&quot; class=&quot;headerlink&quot; title=&quot;修改浏览器Tab里的icon&quot;&gt;&lt;/a&gt;修改浏览器Tab里的icon&lt;/h3&gt;&lt;p&gt;修改后效果如下&lt;br&gt;&lt;img src=&quot;https://app.yinxiang.com/shard/s67/res/b7a1bfae-d09c-4a00-be66-a474d57e6378/20171123140602_rXDDHa_Screenshot.jpg&quot; alt=&quot;aaaaa&quot;&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;很简单我们只需要在yilia主题里&lt;code&gt;_config.yml&lt;/code&gt;文件里修改为&lt;code&gt;favicon: /img/favicon.ico&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;将自己想要的icon，转换成&lt;code&gt;.ico&lt;/code&gt;格式修改命名放在&lt;code&gt;yilia/sourse/img&lt;/code&gt;里，大功告成&lt;/li&gt;
&lt;/ul&gt;
    
    </summary>
    
      <category term="随笔" scheme="http://www.barondu.com/categories/%E9%9A%8F%E7%AC%94/"/>
    
    
      <category term="Hexo" scheme="http://www.barondu.com/tags/Hexo/"/>
    
      <category term="yilia" scheme="http://www.barondu.com/tags/yilia/"/>
    
      <category term="blog" scheme="http://www.barondu.com/tags/blog/"/>
    
  </entry>
  
  <entry>
    <title>Installing TensorFlow</title>
    <link href="http://www.barondu.com/2017/11/17/TensorFlow/"/>
    <id>http://www.barondu.com/2017/11/17/TensorFlow/</id>
    <published>2017-11-17T11:43:51.000Z</published>
    <updated>2019-07-01T22:12:29.000Z</updated>
    
    <content type="html"><![CDATA[<h1 id="TensorFlow"><a href="#TensorFlow" class="headerlink" title="TensorFlow"></a>TensorFlow</h1><h2 id="配置环境"><a href="#配置环境" class="headerlink" title="配置环境"></a>配置环境</h2><p>我的环境是 macOS Sierra 10.12.6，其他mac版本应该也适用。</p>
<h3 id="pyenv"><a href="#pyenv" class="headerlink" title="pyenv"></a>pyenv</h3><p>安装<a href="https://github.com/pyenv/pyenv" target="_blank" rel="external">pyenv</a> 管理多版本 python，通过它安装 python。原因是不希望污染mac自带系统python。详细过程参考官方doc，这里描述一下我的安装过程。</p>
<iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width="330" height="86" src="//music.163.com/outchain/player?type=2&id=518894283&auto=1&height=66"></iframe>


<embed src="https://emumo.xiami.com/widget/291251140_1775818238/singlePlayer.swf" type="application/x-shockwave-flash" width="257" height="33" wmode="transparent">

<ol>
<li>安装：</li>
</ol>
<figure class="highlight elixir"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># 使用git安装</span></div><div class="line"><span class="variable">$ </span>git clone <span class="symbol">https:</span>/<span class="regexp">/github.com/pyenv</span><span class="regexp">/pyenv.git ~/</span>.pyenv</div><div class="line"><span class="comment"># 使用Homebrew on Mac OS X 安装</span></div><div class="line"><span class="variable">$ </span>brew update</div><div class="line"><span class="variable">$ </span>brew install pyenv</div></pre></td></tr></table></figure>
<p>没有<a href="https://brew.sh/" title="Homebrew" target="_blank" rel="external">Homebrew</a>请安装并更新<br><a id="more"></a></p>
<figure class="highlight julia"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># 配置pyenv的环境，</span></div><div class="line">$ echo '<span class="keyword">export</span> PYENV_ROOT=<span class="string">"<span class="variable">$HOME</span>/.pyenv"</span>' &gt;&gt; ~/.bash_profile</div><div class="line">$ echo '<span class="keyword">export</span> PATH=<span class="string">"<span class="variable">$PYENV_ROOT</span>/bin:<span class="variable">$PATH</span>"</span>' &gt;&gt; ~/.bash_profile</div><div class="line">$ echo -<span class="literal">e</span> '<span class="keyword">if</span> command -v pyenv <span class="number">1</span>&gt;/dev/null <span class="number">2</span>&gt;&amp;<span class="number">1</span>; then\n  eval <span class="string">"<span class="subst">$(pyenv init -)</span>"</span>\nfi' &gt;&gt; ~/.bash_profile</div><div class="line">$ echo -<span class="literal">e</span> '<span class="keyword">if</span> command -v pyenv <span class="number">1</span>&gt;/dev/null <span class="number">2</span>&gt;&amp;<span class="number">1</span>; then\n  eval <span class="string">"<span class="subst">$(pyenv init -)</span>"</span>\nfi' &gt;&gt; ~/.bash_profile</div><div class="line">$ exec <span class="string">"<span class="variable">$SHELL</span>"</span></div></pre></td></tr></table></figure>
<p>这里的 shell 配置文件（~/.bash_profile）依不同 Linux 而需作修改，如果使用 Zsh 则需要相应的配置 ~/.zshrc 详情见<a href="https://github.com/pyenv/pyenv" target="_blank" rel="external">官方doc</a></p>
<ol>
<li>用pyenv安装python</li>
</ol>
<figure class="highlight lsl"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ pyenv install <span class="number">3.6</span><span class="number">.3</span></div></pre></td></tr></table></figure>
<p>pyenv的常用指令</p>
<p><img src="https://app.yinxiang.com/shard/s67/res/c8088c9c-9d3e-43d5-856b-7696457021b0/20171118172703_TsVcPT_Screenshot.jpg" alt="6623c3697be87f3452b4c2e79403010b.jpeg">s</p>
<p>更多关于pyenv的使用大家可以参考这篇文章<a href="http://einverne.github.io/post/2017/04/pyenv.html" target="_blank" rel="external">使用 pyenv 管理 Python 版本 </a><br>查看当前python版本和路径<br><figure class="highlight livecodeserver"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">pyenv versions</div><div class="line">  <span class="keyword">system</span></div><div class="line">* <span class="number">3.6</span><span class="number">.3</span> (<span class="built_in">set</span> <span class="keyword">by</span> /Users/baron/.pyenv/<span class="built_in">version</span>)</div></pre></td></tr></table></figure></p>
<h3 id="TensorfFlow-安装"><a href="#TensorfFlow-安装" class="headerlink" title="TensorfFlow 安装"></a>TensorfFlow 安装</h3><ul>
<li>最简单的办法是从<a href="*https://www.tensorflow.org/install/">官网</a>下载TensorFlow直接安装</li>
<li>当然我们也可以追求性能用源码安装，这里是<a href="https://www.tensorflow.org/install/install_sources" target="_blank" rel="external">官方文档</a></li>
<li>这里介绍CPU版本的安装，GPU版本还需要CUDA和Xcode的环境，这里有关于<a href="https://www.tensorflow.org/install/install_mac#NVIDIARequirements" target="_blank" rel="external">GPU版本安装的解释</a></li>
</ul>
<figure class="highlight vala"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div></pre></td><td class="code"><pre><div class="line"><span class="meta"># 安装 bazel</span></div><div class="line">brew update &amp;&amp; brew install bazel</div><div class="line"></div><div class="line"><span class="meta"># 安装 python dependencies分别是six numpy wheel</span></div><div class="line"></div><div class="line"><span class="meta"># python 2.x pip用以下指令</span></div><div class="line">sudo -H pip install six numpy wheel </div><div class="line"></div><div class="line"><span class="meta"># python 3.x pip3 用以下指令</span></div><div class="line">sudo -H pip3 install six numpy wheel </div><div class="line"></div><div class="line"><span class="meta"># 下载源码</span></div><div class="line">git clone https:<span class="comment">//github.com/tensorflow/tensorflow</span></div><div class="line"></div><div class="line"><span class="meta"># 如果使用pyenv之后 建议clone在pyenv的python子文件夹下 </span></div><div class="line"><span class="meta"># cd 到路径，以下是我的tensflow的路径</span></div><div class="line">cd /Users/baron/.pyenv/versions/<span class="number">3.6</span><span class="number">.3</span>/lib/python3<span class="number">.6</span>/site-packages/tensorflow</div><div class="line"></div><div class="line"><span class="meta"># 配置编译文件，基本默认选项，建议仔细看下，官方doc有解释，路径一定要注意，tensflow在那个pyrthon下，写哪个python路径</span></div><div class="line"><span class="meta"># 因为需要下载一些文件，需要开翻墙</span></div><div class="line">./configure</div><div class="line"></div><div class="line"><span class="meta"># 编译没有 GPU 支持版本，很漫长，编译了将近一个小时</span></div><div class="line">bazel build --config=opt <span class="comment">//tensorflow/tools/pip_package:build_pip_package</span></div><div class="line"></div><div class="line"><span class="meta"># 打包到/tmp/tensorflow_pkg下</span></div><div class="line">bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg</div><div class="line"></div><div class="line"><span class="meta"># 查看包名</span></div><div class="line">ls -alh /tmp/tensorflow_pkg</div><div class="line"></div><div class="line"><span class="meta"># 用pip或pip3安装，后面的包名替换成泥自己编译后的</span></div><div class="line">pip3 install /tmp/tensorflow_pkg/tensorflow<span class="number">-1.0</span><span class="number">.0</span>-cp36-cp36m-macosx_10_11_x86_64.whl</div></pre></td></tr></table></figure>
<p>在安装报错的时候，尝试加上<code>sudo</code>，若还是不行使用<code>sudo -H</code>，我就因为这个问题耽误好久。<br>注意所有路径问题，编译时显示路径不对，请cd或者更改到你TensorFlow的实际路径</p>
<h3 id="直接安装他人编译好的版本"><a href="#直接安装他人编译好的版本" class="headerlink" title="直接安装他人编译好的版本"></a>直接安装他人编译好的版本</h3><p>可以在<a href="https://github.com/lakshayg/tensorflow-build" target="_blank" rel="external">这里</a>下载别人已经编译好的<code>.whl</code>文件来安装或者更新，注意自己的python版本和TensorFlow版本以及系统</p>
<h3 id="检测和更新"><a href="#检测和更新" class="headerlink" title="检测和更新"></a>检测和更新</h3><p>最后进入IDE，python shell或者terminal 用pyenv选择你安装了tensorflow的python版本</p>
<figure class="highlight ruby"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">workon ml</div><div class="line">python</div><div class="line"><span class="comment"># 进入 python repl 环境</span></div><div class="line"><span class="meta">&gt;&gt;</span>&gt; import tensorflow as tf</div><div class="line"><span class="meta">&gt;&gt;</span>&gt; hello = tf.constant(<span class="string">'Hello, TensorFlow!'</span>)</div><div class="line"><span class="meta">&gt;&gt;</span>&gt; sess = tf.Session()</div><div class="line"><span class="meta">&gt;&gt;</span>&gt; print(sess.run(hello))</div><div class="line">b<span class="string">'Hello, TensorFlow!'</span></div></pre></td></tr></table></figure>
<h4 id="更新"><a href="#更新" class="headerlink" title="更新"></a>更新</h4><p>对于有强迫症的小伙伴（譬如我<br>更新是必不可少的^_^</p>
<figure class="highlight tcl"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># 更新brew安装过的一切</span></div><div class="line">brew <span class="keyword">update</span></div><div class="line"><span class="comment"># 查看pip安装过的所有包</span></div><div class="line">pip3 <span class="keyword">list</span> --<span class="keyword">format</span>=columns</div><div class="line"><span class="comment"># 查看pip安装过的且需要更新的包</span></div><div class="line">pip3 <span class="keyword">list</span> --outdated --<span class="keyword">format</span>=columns</div><div class="line"><span class="comment"># 一个个输入包名更新就好了</span></div><div class="line">sudo -H pip3 install -U 包名</div></pre></td></tr></table></figure>
<h2 id="Reference"><a href="#Reference" class="headerlink" title="Reference"></a>Reference</h2><ol>
<li><a href="https://www.tensorflow.org/install/install_sources#prepare_environment_for_mac_os" target="_blank" rel="external">Installing TensorFlow from Sources</a></li>
<li><a href="https://gist.github.com/cosmtrek/91d921bc1c45f30de38b689965ab2236" target="_blank" rel="external">源码安装 TensorFlow for Mac OS X</a></li>
</ol>
]]></content>
    
    <summary type="html">
    
      &lt;h1 id=&quot;TensorFlow&quot;&gt;&lt;a href=&quot;#TensorFlow&quot; class=&quot;headerlink&quot; title=&quot;TensorFlow&quot;&gt;&lt;/a&gt;TensorFlow&lt;/h1&gt;&lt;h2 id=&quot;配置环境&quot;&gt;&lt;a href=&quot;#配置环境&quot; class=&quot;headerlink&quot; title=&quot;配置环境&quot;&gt;&lt;/a&gt;配置环境&lt;/h2&gt;&lt;p&gt;我的环境是 macOS Sierra 10.12.6，其他mac版本应该也适用。&lt;/p&gt;
&lt;h3 id=&quot;pyenv&quot;&gt;&lt;a href=&quot;#pyenv&quot; class=&quot;headerlink&quot; title=&quot;pyenv&quot;&gt;&lt;/a&gt;pyenv&lt;/h3&gt;&lt;p&gt;安装&lt;a href=&quot;https://github.com/pyenv/pyenv&quot;&gt;pyenv&lt;/a&gt; 管理多版本 python，通过它安装 python。原因是不希望污染mac自带系统python。详细过程参考官方doc，这里描述一下我的安装过程。&lt;/p&gt;
&lt;iframe frameborder=&quot;no&quot; border=&quot;0&quot; marginwidth=&quot;0&quot; marginheight=&quot;0&quot; width=330 height=86 src=&quot;//music.163.com/outchain/player?type=2&amp;id=518894283&amp;auto=1&amp;height=66&quot;&gt;&lt;/iframe&gt;


&lt;embed src=&quot;https://emumo.xiami.com/widget/291251140_1775818238/singlePlayer.swf&quot; type=&quot;application/x-shockwave-flash&quot; width=&quot;257&quot; height=&quot;33&quot; wmode=&quot;transparent&quot;&gt;&lt;/embed&gt;

&lt;ol&gt;
&lt;li&gt;安装：&lt;/li&gt;
&lt;/ol&gt;
&lt;figure class=&quot;highlight elixir&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre&gt;&lt;div class=&quot;line&quot;&gt;1&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;2&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;3&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;4&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;5&lt;/div&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;div class=&quot;line&quot;&gt;&lt;span class=&quot;comment&quot;&gt;# 使用git安装&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;&lt;span class=&quot;variable&quot;&gt;$ &lt;/span&gt;git clone &lt;span class=&quot;symbol&quot;&gt;https:&lt;/span&gt;/&lt;span class=&quot;regexp&quot;&gt;/github.com/pyenv&lt;/span&gt;&lt;span class=&quot;regexp&quot;&gt;/pyenv.git ~/&lt;/span&gt;.pyenv&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;&lt;span class=&quot;comment&quot;&gt;# 使用Homebrew on Mac OS X 安装&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;&lt;span class=&quot;variable&quot;&gt;$ &lt;/span&gt;brew update&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;&lt;span class=&quot;variable&quot;&gt;$ &lt;/span&gt;brew install pyenv&lt;/div&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/figure&gt;
&lt;p&gt;没有&lt;a href=&quot;https://brew.sh/&quot; title=&quot;Homebrew&quot;&gt;Homebrew&lt;/a&gt;请安装并更新&lt;br&gt;
    
    </summary>
    
      <category term="随笔" scheme="http://www.barondu.com/categories/%E9%9A%8F%E7%AC%94/"/>
    
    
      <category term="Machine Learning" scheme="http://www.barondu.com/tags/Machine-Learning/"/>
    
      <category term="TensorFlow" scheme="http://www.barondu.com/tags/TensorFlow/"/>
    
      <category term="pyenv" scheme="http://www.barondu.com/tags/pyenv/"/>
    
  </entry>
  
  <entry>
    <title>笔记 OSC_Deadlocks</title>
    <link href="http://www.barondu.com/2017/05/03/Deadlocks/"/>
    <id>http://www.barondu.com/2017/05/03/Deadlocks/</id>
    <published>2017-05-03T15:10:14.000Z</published>
    <updated>2019-07-02T02:55:54.000Z</updated>
    
    <content type="html"><![CDATA[<h1 id="Deadlocks"><a href="#Deadlocks" class="headerlink" title="Deadlocks"></a>Deadlocks</h1><hr>
<h2 id="Overview"><a href="#Overview" class="headerlink" title="Overview"></a>Overview</h2><ul>
<li>Deadlocks and <strong>conditions for deadlocks</strong> </li>
<li>Algorithms to <strong>detect[探知] deadlocks</strong><br>Approaches to <strong>recover</strong> from deadlocks<a id="more"></a>
<h3 id="Deadlocks-1"><a href="#Deadlocks-1" class="headerlink" title="Deadlocks"></a>Deadlocks</h3><h4 id="How-Occur-deadlocks-看看就好"><a href="#How-Occur-deadlocks-看看就好" class="headerlink" title="How Occur deadlocks?[看看就好]"></a>How Occur deadlocks?[看看就好]</h4></li>
<li>Some resources are <strong>mutually exclusive</strong> and can only be used by one <strong>process at a time</strong></li>
<li>On occasions[偶然], <strong>multiple processes</strong> will <strong>require access</strong> to <strong>multiple mutually exclusive resources</strong> (e.g. process A and B need resources X and Y)</li>
<li>Process A and B request the resources in <strong>opposite orders</strong> and end up in deadlock<ul>
<li>Deadlocks can occur on the <strong>same machine</strong> or between <strong>multiple machines</strong> (e.g. resources are requested over the network) and <strong>any number of resources</strong></li>
<li>Deadlocks are not just related to operating systems but also occur, e.g., <strong>in databases</strong></li>
</ul>
</li>
</ul>
<p>resource</p>
<ul>
<li>A resource (e.g. a device, a data record, file, semaphore) can be <strong>acquired, used, </strong>and <strong>released</strong><ul>
<li>A resource can be <strong>preemptable</strong>, i.e., it can be forcefully taken away from the process without permanent adverse effect</li>
<li>A resource can be <strong>non-preemptable,</strong> i.e., it cannot be taken away from a process without permanent adverse effect</li>
</ul>
</li>
<li>If a <strong>non-preemptable resource is requested</strong> but not available, <strong>the process is made to wait</strong></li>
<li><strong>Deadlocks only occur</strong> for <strong>non-preemtable resources</strong> because preemtable resources can be temporarily taken away to recover from the deadlock</li>
</ul>
<h3 id="Definition"><a href="#Definition" class="headerlink" title="Definition"></a>Definition</h3><blockquote>
<p>“A set of processes is deadlocked if <strong>each process</strong> in the set is waiting for <strong>an event</strong> that only the <strong>other process</strong> in the set can cause”<br> –  Tanenbaum</p>
</blockquote>
<ul>
<li>Each <strong>deadlocked process</strong> is <strong>waiting for</strong> a resource held by <strong>an other deadlocked process</strong> (which cannot run and hence cannot release the resources)</li>
<li>This can happen between <strong>any number of processes</strong> and for <strong>any number of resources</strong></li>
</ul>
<h3 id="Conditions"><a href="#Conditions" class="headerlink" title="Conditions"></a>Conditions</h3><p><strong>Four conditions</strong> must hold for deadlocks to occur (Coffman et al. (1971)):</p>
<ul>
<li><strong>Mutual exclusion</strong>: a resource can be assigned to at most one process at a time</li>
<li><strong>Hold and wait condition</strong>: a resource can be held whilst[同时] requesting new resources</li>
<li><strong>No preemption:</strong> resources cannot be forcefully taken away from a process </li>
<li><strong>Circular wait</strong>: there is a circular chain of two or more processes, waiting for a resource held by the other processes</li>
</ul>
<p><strong>No deadlocks</strong> can occur if one of the conditions is <strong>not satisfied</strong></p>
]]></content>
    
    <summary type="html">
    
      &lt;h1 id=&quot;Deadlocks&quot;&gt;&lt;a href=&quot;#Deadlocks&quot; class=&quot;headerlink&quot; title=&quot;Deadlocks&quot;&gt;&lt;/a&gt;Deadlocks&lt;/h1&gt;&lt;hr&gt;
&lt;h2 id=&quot;Overview&quot;&gt;&lt;a href=&quot;#Overview&quot; class=&quot;headerlink&quot; title=&quot;Overview&quot;&gt;&lt;/a&gt;Overview&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;Deadlocks and &lt;strong&gt;conditions for deadlocks&lt;/strong&gt; &lt;/li&gt;
&lt;li&gt;Algorithms to &lt;strong&gt;detect[探知] deadlocks&lt;/strong&gt;&lt;br&gt;Approaches to &lt;strong&gt;recover&lt;/strong&gt; from deadlocks
    
    </summary>
    
      <category term="笔记" scheme="http://www.barondu.com/categories/%E7%AC%94%E8%AE%B0/"/>
    
    
      <category term="OSC" scheme="http://www.barondu.com/tags/OSC/"/>
    
      <category term="Deadlocks" scheme="http://www.barondu.com/tags/Deadlocks/"/>
    
  </entry>
  
  <entry>
    <title>拓展 N和NP问题</title>
    <link href="http://www.barondu.com/2017/02/06/N%E5%92%8CNP%E9%97%AE%E9%A2%98/"/>
    <id>http://www.barondu.com/2017/02/06/N和NP问题/</id>
    <published>2017-02-06T03:43:27.000Z</published>
    <updated>2017-02-08T14:48:31.000Z</updated>
    
    <content type="html"><![CDATA[<h1 id="N和NP问题"><a href="#N和NP问题" class="headerlink" title="N和NP问题"></a>N和NP问题</h1><blockquote>
<p>“P” refers to the class of problems that can be solved in polynomial time<br>“NP” refers to problems that can be solved in non- deterministic polynomial time</p>
</blockquote>
<ul>
<li>P是能在多项式时间内<strong>解决</strong>的问题，</li>
<li>NP是能在多项式时间<strong>验证答案正确与否</strong>的问题。</li>
</ul>
<p>用大白话讲大概就是这样。P是否等于NP实质上就是在问，如果对于一个问题我能在多项式时间内验证其答案的正确性，那么我是否能在多项式时间内解决它？这个表述不太严谨，但通俗来讲就是如此。(2014, Wang)</p>
<h4 id="什么是-polynomial-time？"><a href="#什么是-polynomial-time？" class="headerlink" title="什么是 polynomial time？"></a>什么是 polynomial time？</h4><ul>
<li>多项式级的复杂度：O(1),O(log(n)),O(n^a)等，因为它的规模n出现在底数的位置；</li>
<li>非多项式级的复杂度：O(a^n)和O(n!)型复杂度，其复杂度计算机往往不能承受。</li>
</ul>
]]></content>
    
    <summary type="html">
    
      &lt;h1 id=&quot;N和NP问题&quot;&gt;&lt;a href=&quot;#N和NP问题&quot; class=&quot;headerlink&quot; title=&quot;N和NP问题&quot;&gt;&lt;/a&gt;N和NP问题&lt;/h1&gt;&lt;blockquote&gt;
&lt;p&gt;“P” refers to the class of problems that 
    
    </summary>
    
      <category term="笔记" scheme="http://www.barondu.com/categories/%E7%AC%94%E8%AE%B0/"/>
    
    
      <category term="G52LAC" scheme="http://www.barondu.com/tags/G52LAC/"/>
    
  </entry>
  
  <entry>
    <title>笔记 OSC_Concurrency2</title>
    <link href="http://www.barondu.com/2017/01/09/Concurrency2/"/>
    <id>http://www.barondu.com/2017/01/09/Concurrency2/</id>
    <published>2017-01-09T09:33:27.000Z</published>
    <updated>2019-07-01T19:27:09.000Z</updated>
    
    <content type="html"><![CDATA[<h1 id="Concurrency-2"><a href="#Concurrency-2" class="headerlink" title="Concurrency 2"></a>Concurrency 2</h1><hr>
<h2 id="Overview"><a href="#Overview" class="headerlink" title="Overview"></a>Overview</h2><ul>
<li><strong>Software approaches</strong>: Peterson’s solution Hardware approaches:</li>
<li><strong>Disabling interrupts</strong>:<ul>
<li><code>test_and_set()</code></li>
<li><code>compare_and_swap()</code></li>
</ul>
</li>
<li>Higher level approaches include <strong>mutexes</strong> and <strong>semaphores</strong><h3 id="Peterson’s-Solution"><a href="#Peterson’s-Solution" class="headerlink" title="Peterson’s Solution"></a>Peterson’s Solution</h3></li>
<li>Peterson’s solution is a <strong>software based solution</strong> which worked well for <strong>older machines</strong></li>
<li>Two <strong>shared variables</strong> are used:<ul>
<li>turn: indicates <strong>which process is next</strong> to enter its critical section </li>
<li>boolean flag[2]: indicates that a <strong>process is ready</strong> to enter its critical section</li>
</ul>
</li>
<li>It is restricted[限制] to <strong>two processes</strong> that execute in <strong>strict alternation</strong>[严格的交替]<figure class="highlight sqf"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">do</span> &#123;</div><div class="line">	<span class="built_in">flag</span>[j] = <span class="literal">true</span>; <span class="comment">// j wants to enter critical section turn = i; // allow i to access first</span></div><div class="line">	<span class="keyword">while</span> (<span class="built_in">flag</span>[i] &amp;&amp; turn == i);</div><div class="line">	<span class="comment">// whilst i wants to access critical section</span></div><div class="line">	<span class="comment">// and its i’s turn, apply busy waiting </span></div><div class="line">	</div><div class="line">	<span class="comment">// CRITICAL SECTION</span></div><div class="line">	</div><div class="line">	<span class="built_in">flag</span>[j] = <span class="literal">false</span>;</div><div class="line">	<span class="comment">// remainder section</span></div><div class="line">&#125; <span class="keyword">while</span> (...);</div></pre></td></tr></table></figure>
</li>
</ul>
<a id="more"></a>
<ul>
<li>Peterson’s solution satisfies all requirements for mutual exclusion</li>
<li><strong>Mutual exclusion requirement</strong>: the variable turn can have <strong>at most one value at a time</strong><ul>
<li>while(flag[i] &amp;&amp; turn == i)or while(flag[j] &amp;&amp; turn == j) is true and at most <strong>one process can enter its critical section</strong> (mutual exclusion)</li>
</ul>
</li>
</ul>
<h3 id="Disabling-Interrupts"><a href="#Disabling-Interrupts" class="headerlink" title="Disabling Interrupts"></a>Disabling Interrupts</h3><ul>
<li><p><strong>Disable interrupts</strong> whilst <strong>executing a critical section</strong> and prevent interruption (i.e., interrupts from timers, I/O devices, etc.)</p>
<ul>
<li>Think of the counter++ example <figure class="highlight plain"><figcaption><span>= counter;</span></figcaption><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">register = register + 1</div><div class="line">counter = register;</div></pre></td></tr></table></figure>
</li>
</ul>
</li>
<li><p>Disabling interrupts “may” be appropriate on <strong>a single CPU machine</strong> </p>
</li>
<li>This is <strong>inefficient on modern multi-core/multi processor machines</strong><ul>
<li>Disabling interrupts on all cores/CPUs <strong>takes time</strong> and <strong>causes delays</strong> </li>
<li><strong>CPU capacity[能力] is lost</strong> on other cores</li>
</ul>
</li>
</ul>
<h3 id="Atomic-Instructions"><a href="#Atomic-Instructions" class="headerlink" title="Atomic Instructions"></a>Atomic Instructions</h3><ul>
<li>Implement <code>test_and_set()</code> and <code>swap_and_compare()</code> instructions as a <strong>set of atomic (UN-interruptible) instructions</strong><ul>
<li>Reading and setting the variable(s) is done as one “complete” set of instructions</li>
<li>If <code>test_and_set()</code> or <code>compare_and_swap()</code> are called simultaneously, they will be <strong>executed sequentially</strong></li>
</ul>
</li>
<li>They are used in in combination with <strong>global lock variables</strong>, assumed to be true if the lock is <strong>in use</strong></li>
</ul>
<h4 id="test-and-set"><a href="#test-and-set" class="headerlink" title="test_and_set()"></a>test_and_set()</h4><blockquote>
<p>Test and set must be <strong>atomic/UN-interruptable</strong><br><figure class="highlight cs"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// Test and set method</span></div><div class="line"><span class="function">boolean <span class="title">test_and_set</span>(<span class="params">boolean * <span class="keyword">lock</span></span>) </span>&#123; </div><div class="line">	boolean rv = *<span class="keyword">lock</span>;</div><div class="line">	*<span class="keyword">lock</span> = <span class="literal">true</span>;</div><div class="line">	<span class="keyword">return</span> rv;</div><div class="line">&#125;</div><div class="line"><span class="comment">// Example of using test and set method</span></div><div class="line"><span class="keyword">do</span> &#123;</div><div class="line">	<span class="comment">// WHILE the lock is in use, apply busy waiting</span></div><div class="line">	<span class="keyword">while</span> (test_and_set(&amp;<span class="keyword">lock</span>)); </div><div class="line">	<span class="comment">// Lock was false, now true</span></div><div class="line">	<span class="comment">// CRITICAL SECTION</span></div><div class="line">	...</div><div class="line">	<span class="keyword">lock</span> = <span class="literal">false</span>;</div><div class="line">	...</div><div class="line">	<span class="comment">// remainder section</span></div><div class="line">&#125; <span class="keyword">while</span> (...)</div></pre></td></tr></table></figure></p>
</blockquote>
<h4 id="compare-and-swap"><a href="#compare-and-swap" class="headerlink" title="compare_and_swap()"></a>compare_and_swap()</h4><figure class="highlight cs"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// Compare and swap method</span></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">compare_and_swap</span>(<span class="params"><span class="keyword">int</span> *<span class="keyword">lock</span>, <span class="keyword">int</span> expected, <span class="keyword">int</span> new_value</span>) </span>&#123; </div><div class="line">	<span class="keyword">int</span> temp = *<span class="keyword">lock</span>;</div><div class="line">	<span class="keyword">if</span>(*<span class="keyword">lock</span> == expected)</div><div class="line">	*<span class="keyword">lock</span> = new_value; <span class="keyword">return</span> temp;</div><div class="line">&#125;</div><div class="line"><span class="comment">// Example using compare and swap method</span></div><div class="line"><span class="keyword">do</span> &#123;</div><div class="line">	<span class="comment">// While the lock is in use (i.e. == 1), apply busy waiting</span></div><div class="line">	<span class="keyword">while</span> (compare_and_swap(&amp;<span class="keyword">lock</span>, <span class="number">0</span>, <span class="number">1</span>) != <span class="number">0</span>); </div><div class="line">	<span class="comment">// Lock was false, now true</span></div><div class="line">	<span class="comment">// CRITICAL SECTION</span></div><div class="line">	...</div><div class="line">	<span class="keyword">lock</span> = <span class="number">0</span>;</div><div class="line">	...</div><div class="line">	<span class="comment">// remainder section</span></div><div class="line">&#125; <span class="keyword">while</span> (...);</div></pre></td></tr></table></figure>
<p>Disadvantages:</p>
<ul>
<li><code>test_and_set()</code>and <code>compare_and_swap()</code> are <strong>hardware instructions</strong> and (usually) <strong>not directly accessible</strong> to the user</li>
<li><strong>Busy waiting</strong> is used </li>
<li><strong>Starvation</strong> is possible </li>
<li><strong>Deadlock</strong> is possible</li>
</ul>
<p>The <strong>OS uses the hardware instructions</strong> to implement <strong>higher level mechanisms/instructions</strong> for mutual exclusion, i.e. <strong>mutexes</strong> and <strong>semaphores</strong></p>
<p>##Summary</p>
<ul>
<li>Peterson’s solution (software)</li>
<li>Hardware instructions: interrupt disabling, (test_and_set, compare_and_swap)</li>
</ul>
]]></content>
    
    <summary type="html">
    
      &lt;h1 id=&quot;Concurrency-2&quot;&gt;&lt;a href=&quot;#Concurrency-2&quot; class=&quot;headerlink&quot; title=&quot;Concurrency 2&quot;&gt;&lt;/a&gt;Concurrency 2&lt;/h1&gt;&lt;hr&gt;
&lt;h2 id=&quot;Overview&quot;&gt;&lt;a href=&quot;#Overview&quot; class=&quot;headerlink&quot; title=&quot;Overview&quot;&gt;&lt;/a&gt;Overview&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Software approaches&lt;/strong&gt;: Peterson’s solution Hardware approaches:&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Disabling interrupts&lt;/strong&gt;:&lt;ul&gt;
&lt;li&gt;&lt;code&gt;test_and_set()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;compare_and_swap()&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Higher level approaches include &lt;strong&gt;mutexes&lt;/strong&gt; and &lt;strong&gt;semaphores&lt;/strong&gt;&lt;h3 id=&quot;Peterson’s-Solution&quot;&gt;&lt;a href=&quot;#Peterson’s-Solution&quot; class=&quot;headerlink&quot; title=&quot;Peterson’s Solution&quot;&gt;&lt;/a&gt;Peterson’s Solution&lt;/h3&gt;&lt;/li&gt;
&lt;li&gt;Peterson’s solution is a &lt;strong&gt;software based solution&lt;/strong&gt; which worked well for &lt;strong&gt;older machines&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Two &lt;strong&gt;shared variables&lt;/strong&gt; are used:&lt;ul&gt;
&lt;li&gt;turn: indicates &lt;strong&gt;which process is next&lt;/strong&gt; to enter its critical section &lt;/li&gt;
&lt;li&gt;boolean flag[2]: indicates that a &lt;strong&gt;process is ready&lt;/strong&gt; to enter its critical section&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;It is restricted[限制] to &lt;strong&gt;two processes&lt;/strong&gt; that execute in &lt;strong&gt;strict alternation&lt;/strong&gt;[严格的交替]&lt;figure class=&quot;highlight sqf&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre&gt;&lt;div class=&quot;line&quot;&gt;1&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;2&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;3&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;4&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;5&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;6&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;7&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;8&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;9&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;10&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;11&lt;/div&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;div class=&quot;line&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;do&lt;/span&gt; &amp;#123;&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;	&lt;span class=&quot;built_in&quot;&gt;flag&lt;/span&gt;[j] = &lt;span class=&quot;literal&quot;&gt;true&lt;/span&gt;; &lt;span class=&quot;comment&quot;&gt;// j wants to enter critical section turn = i; // allow i to access first&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;	&lt;span class=&quot;keyword&quot;&gt;while&lt;/span&gt; (&lt;span class=&quot;built_in&quot;&gt;flag&lt;/span&gt;[i] &amp;amp;&amp;amp; turn == i);&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;	&lt;span class=&quot;comment&quot;&gt;// whilst i wants to access critical section&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;	&lt;span class=&quot;comment&quot;&gt;// and its i’s turn, apply busy waiting &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;	&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;	&lt;span class=&quot;comment&quot;&gt;// CRITICAL SECTION&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;	&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;	&lt;span class=&quot;built_in&quot;&gt;flag&lt;/span&gt;[j] = &lt;span class=&quot;literal&quot;&gt;false&lt;/span&gt;;&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;	&lt;span class=&quot;comment&quot;&gt;// remainder section&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;line&quot;&gt;&amp;#125; &lt;span class=&quot;keyword&quot;&gt;while&lt;/span&gt; (...);&lt;/div&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/figure&gt;
&lt;/li&gt;
&lt;/ul&gt;
    
    </summary>
    
      <category term="笔记" scheme="http://www.barondu.com/categories/%E7%AC%94%E8%AE%B0/"/>
    
    
      <category term="OSC" scheme="http://www.barondu.com/tags/OSC/"/>
    
      <category term="concurrency" scheme="http://www.barondu.com/tags/concurrency/"/>
    
  </entry>
  
  <entry>
    <title>笔记 OSC_Concurrency1</title>
    <link href="http://www.barondu.com/2017/01/09/Concurrency1/"/>
    <id>http://www.barondu.com/2017/01/09/Concurrency1/</id>
    <published>2017-01-09T06:09:27.000Z</published>
    <updated>2019-07-01T19:28:01.000Z</updated>
    
    <content type="html"><![CDATA[<h1 id="Concurrency-1"><a href="#Concurrency-1" class="headerlink" title="Concurrency 1"></a>Concurrency 1</h1><hr>
<h2 id="Overview"><a href="#Overview" class="headerlink" title="Overview"></a>Overview</h2><ul>
<li>Examples of <strong>concurrency issues</strong> (e.g. counter++)</li>
<li>Root causes of <strong>concurrency issues</strong></li>
<li><strong>Critical sections</strong> and <strong>mutual exclusion</strong> </li>
<li><strong>Requirements and approaches</strong> for mutual exclusion</li>
</ul>
<h3 id="Concurrency"><a href="#Concurrency" class="headerlink" title="Concurrency"></a>Concurrency</h3><ul>
<li>Threads and processes execute concurrently or in parallel and can share resources<ul>
<li>Multiprogramming/multiprocessing <strong>improves system utilisation</strong></li>
</ul>
</li>
<li>A process/thread can be <strong>interrupted at any point in time</strong> (I/O, timer)<ul>
<li>The process “state” is <strong>saved</strong> in the p<strong>rocess control block</strong> </li>
</ul>
</li>
<li>The outcome of programs may become <strong>unpredictable</strong>[不可预知的]<ul>
<li>Sharing data can lead to <strong>inconsistencies</strong>[矛盾]</li>
<li>I.e., the <strong>outcome of execution</strong> may <strong>depend on the order</strong> win which instructions are carried out</li>
</ul>
</li>
</ul>
<a id="more"></a>
<h4 id="Incrementing-a-counter"><a href="#Incrementing-a-counter" class="headerlink" title="Incrementing a counter"></a>Incrementing a counter</h4><ul>
<li><code>counter++</code> consists of three separate actions:<ul>
<li>1 read the value of counter and <strong>store it in a register</strong></li>
<li>2 add one to the value in the register</li>
<li>3 store the value of the register <strong>in counter</strong></li>
</ul>
</li>
<li>The above actions are <strong>NOT</strong> “atomic”, e.g. they can be interrupted by the timer (⇒ <strong>context switch</strong>)</li>
</ul>
<h4 id="Bounded-Buffers"><a href="#Bounded-Buffers" class="headerlink" title="Bounded Buffers"></a>Bounded Buffers</h4><ul>
<li>Consider a <strong>bounded buffer</strong> in which N items can be stored</li>
<li>A <strong>counter</strong> is maintained to count the number of items currently in the buffer<ul>
<li><strong>Incremented</strong> when an item is <strong>added</strong> </li>
<li><strong>Decremented</strong> when an item is <strong>removed</strong></li>
</ul>
</li>
<li>Similar <strong>concurrency problems</strong> as with the calculation of sums happen in the bounded buffer (producer/consumer) problem</li>
</ul>
<h4 id="Race-Conditions"><a href="#Race-Conditions" class="headerlink" title="Race Conditions"></a>Race Conditions</h4><ul>
<li>A <strong>race condition occurs</strong> when multiple threads/processes <strong>access shared data</strong> and the result is dependent on <strong>the order in which the instructions are interleaved</strong></li>
</ul>
<h3 id="Concurrency-within-the-OS"><a href="#Concurrency-within-the-OS" class="headerlink" title="Concurrency within the OS"></a>Concurrency within the OS</h3><p>Data Structures</p>
<ul>
<li><strong>Kernels are preemptive</strong> these days (⇔ non-preemptive) <ul>
<li><strong>Multiple processes are running</strong> in the kernel</li>
<li>I.e. kernel <strong>processes can be interrupted</strong> at any point</li>
</ul>
</li>
<li>The kernel maintains <strong>data structures</strong>, e.g. process tables, memory structures, open file lists, etc.</li>
<li>These data structures are accessed <strong>concurrently/in parallel</strong> </li>
<li>These can be subject to <strong>concurrency issues</strong></li>
</ul>
<p>Resources</p>
<ul>
<li>Processes share resources, including memory, files, processor time, printers, I/O devices, etc.</li>
<li>The operating system must:<ul>
<li><strong>Allocate and deallocate</strong> these resources safely (i.e. avoid interference, deadlocks and starvation)</li>
<li>Make sure that interactions within the OS d<strong>o not result in race conditions</strong></li>
<li>The operating system must provide locking mechanisms to implement/support mutual exclusion (and prevent starvation and deadlocks)</li>
</ul>
</li>
</ul>
<h3 id="Critical-Sections-临界段-Mutual-Exclusion-互斥"><a href="#Critical-Sections-临界段-Mutual-Exclusion-互斥" class="headerlink" title="Critical Sections[临界段], Mutual Exclusion[互斥]"></a>Critical Sections[临界段], Mutual Exclusion[互斥]</h3><ul>
<li>A critical section is a set of instructions in which <strong>shared variables are changed</strong></li>
<li><p><strong>Mutual exclusion</strong> must be enforced <strong>for critical sections</strong></p>
<ul>
<li>Only <strong>one process at a time</strong> should be in the critical section (mutual exclusion)</li>
<li>Processes have to <strong>get “permission”</strong> before entering their critical section<figure class="highlight lasso"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">do</span></div><div class="line">&#123;</div><div class="line">	<span class="params">...</span></div><div class="line">	<span class="comment">// ENTRY to critical section</span></div><div class="line">	critical section, e.g.counter++;</div><div class="line">	<span class="comment">// EXIT critical section</span></div><div class="line">	remaining code</div><div class="line">	<span class="params">...</span></div><div class="line">&#125; <span class="keyword">while</span> (<span class="params">...</span>);</div></pre></td></tr></table></figure>
</li>
</ul>
</li>
<li><p><strong>Any solution to the critical section problem</strong> must satisfy the following <strong>requirements</strong>:</p>
<ul>
<li><strong>Mutual exclusion</strong>: only one process can be in its critical section at any one point in time</li>
<li><strong>Progress</strong>: any process must be able to enter its critical section at some point in time</li>
<li><strong>Fairness/bounded waiting</strong>: processes cannot be made to wait indefinitely</li>
</ul>
</li>
<li>These requirements have to be satisfied, <strong>independent of the order</strong> in which sequences are executed</li>
</ul>
<h4 id="Enforcing-Mutual-Exclusion"><a href="#Enforcing-Mutual-Exclusion" class="headerlink" title="Enforcing Mutual Exclusion"></a>Enforcing Mutual Exclusion</h4><ul>
<li><strong>Approaches</strong> for mutual exclusion can be:<ul>
<li><strong>Software based:</strong> Peterson’s solution</li>
<li><strong>Hardware based</strong>: <code>test_and_set(), swap_and_compare()</code></li>
<li><strong>Based on</strong>:<ul>
<li>Mutexes</li>
<li>Semaphores</li>
<li>Monitors (software construct within the programming languages)</li>
</ul>
</li>
</ul>
</li>
<li>In addition to mutual exclusion, <strong>deadlocks</strong> have to be prevented (next week’s subject)</li>
</ul>
<h2 id="Summary"><a href="#Summary" class="headerlink" title="Summary"></a>Summary</h2><ul>
<li>Examples of <strong>concurrency issues</strong> (e.g. counter++)</li>
<li>Root causes of <strong>concurrency issues</strong></li>
<li><strong>Critical sections</strong> and <strong>mutual exclusion</strong> </li>
<li><strong>Requirements and approaches</strong> for mutual exclusion</li>
</ul>
]]></content>
    
    <summary type="html">
    
      &lt;h1 id=&quot;Concurrency-1&quot;&gt;&lt;a href=&quot;#Concurrency-1&quot; class=&quot;headerlink&quot; title=&quot;Concurrency 1&quot;&gt;&lt;/a&gt;Concurrency 1&lt;/h1&gt;&lt;hr&gt;
&lt;h2 id=&quot;Overview&quot;&gt;&lt;a href=&quot;#Overview&quot; class=&quot;headerlink&quot; title=&quot;Overview&quot;&gt;&lt;/a&gt;Overview&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;Examples of &lt;strong&gt;concurrency issues&lt;/strong&gt; (e.g. counter++)&lt;/li&gt;
&lt;li&gt;Root causes of &lt;strong&gt;concurrency issues&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Critical sections&lt;/strong&gt; and &lt;strong&gt;mutual exclusion&lt;/strong&gt; &lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Requirements and approaches&lt;/strong&gt; for mutual exclusion&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;Concurrency&quot;&gt;&lt;a href=&quot;#Concurrency&quot; class=&quot;headerlink&quot; title=&quot;Concurrency&quot;&gt;&lt;/a&gt;Concurrency&lt;/h3&gt;&lt;ul&gt;
&lt;li&gt;Threads and processes execute concurrently or in parallel and can share resources&lt;ul&gt;
&lt;li&gt;Multiprogramming/multiprocessing &lt;strong&gt;improves system utilisation&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;A process/thread can be &lt;strong&gt;interrupted at any point in time&lt;/strong&gt; (I/O, timer)&lt;ul&gt;
&lt;li&gt;The process “state” is &lt;strong&gt;saved&lt;/strong&gt; in the p&lt;strong&gt;rocess control block&lt;/strong&gt; &lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;The outcome of programs may become &lt;strong&gt;unpredictable&lt;/strong&gt;[不可预知的]&lt;ul&gt;
&lt;li&gt;Sharing data can lead to &lt;strong&gt;inconsistencies&lt;/strong&gt;[矛盾]&lt;/li&gt;
&lt;li&gt;I.e., the &lt;strong&gt;outcome of execution&lt;/strong&gt; may &lt;strong&gt;depend on the order&lt;/strong&gt; win which instructions are carried out&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
    
    </summary>
    
      <category term="笔记" scheme="http://www.barondu.com/categories/%E7%AC%94%E8%AE%B0/"/>
    
    
      <category term="OSC" scheme="http://www.barondu.com/tags/OSC/"/>
    
      <category term="concurrency" scheme="http://www.barondu.com/tags/concurrency/"/>
    
  </entry>
  
  <entry>
    <title>笔记 OSC_Processes4</title>
    <link href="http://www.barondu.com/2017/01/09/Processes4/"/>
    <id>http://www.barondu.com/2017/01/09/Processes4/</id>
    <published>2017-01-09T06:05:27.000Z</published>
    <updated>2019-07-01T20:46:26.000Z</updated>
    
    <content type="html"><![CDATA[<h1 id="Processes-4"><a href="#Processes-4" class="headerlink" title="Processes 4"></a>Processes 4</h1><hr>
<h2 id="Overview"><a href="#Overview" class="headerlink" title="Overview"></a>Overview</h2><ul>
<li>Multi-level feedback queues</li>
<li>Scheduling in Window 7 + illustration Scheduling in </li>
<li>Linux (implementation in labs) Scheduling related processes/threads</li>
</ul>
<h3 id="Multi-level-Feedback-Queues"><a href="#Multi-level-Feedback-Queues" class="headerlink" title="Multi-level Feedback Queues"></a>Multi-level Feedback Queues</h3><ul>
<li>Different <strong>scheduling algorithms</strong> can be used for the <strong>individual queues</strong> (e.g., round robin, SJF, FCFS)</li>
<li><p><strong>Feedback queues</strong> allow <strong>priorities to change dynamically</strong>[动态的], i.e., jobs can move between queues:</p>
<ul>
<li>Move to <strong>lower priority queue</strong> if too much CPU time is used (prioritise I/O and interactive processes)</li>
<li>Move to <strong>higher priority queue</strong> to prevent <strong>starvation</strong> and avoid <strong>inversion[反转] of control</strong></li>
</ul>
</li>
<li><p>Defining characteristics of feedback queues include: </p>
<ul>
<li>The <strong>number of queues</strong></li>
<li>The <strong>scheduling algorithms</strong> used for the individual queues </li>
<li><strong>Migration policy</strong>[迁移政策] between queues</li>
<li>Initial <strong>access</strong> to the queues<br>Feedback queues are highly <strong>configurable</strong>[可配置] and offer significant <strong>flexibility</strong>[灵活性，适应性]</li>
</ul>
</li>
</ul>
<a id="more"></a>
<h4 id="Windows-7"><a href="#Windows-7" class="headerlink" title="Windows 7"></a>Windows 7</h4><ul>
<li>An <strong>interactive system</strong>[交互系统] using a <strong>preemptive scheduler</strong> with <strong>dynamic priority levels</strong><ul>
<li>Two priority classes with 16 different priority levels exist<ul>
<li>“<strong>Real time</strong>” processes/threads have a <strong>fixed priority level</strong></li>
<li>“<strong>Variable</strong>” processes/threads can have their priorities <strong>boosted temporarily</strong></li>
</ul>
</li>
</ul>
</li>
<li><p>A <strong>round robin algorithm</strong> is used within the queues</p>
</li>
<li><p>Priorities are based on the <strong>process base priority</strong> (between 0-15) and <strong>thread base priority</strong> (±2 relative to the process priority)</p>
</li>
<li>A thread’s priority dynamically changes during execution between its base priority and the maximum priority within its class<ul>
<li><strong>Interactive I/O bound processes</strong> (e.g. keyboard) receive a <strong>larger boost</strong> </li>
<li>Boosting priorities prevents <strong>priority inversion</strong></li>
</ul>
</li>
</ul>
<h3 id="Scheduling-in-Linux"><a href="#Scheduling-in-Linux" class="headerlink" title="Scheduling in Linux"></a>Scheduling in Linux</h3><h4 id="The-Completely-Fair-Scheduler"><a href="#The-Completely-Fair-Scheduler" class="headerlink" title="The Completely Fair Scheduler"></a>The Completely Fair Scheduler</h4><ul>
<li>Linux distinguishes[区分] between two types of tasks for scheduling:<ul>
<li><strong>Real time tasks</strong> (to be POSIX compliant), divided into: <ul>
<li>Real time FIFO tasks</li>
<li>Real time Round Robin tasks</li>
</ul>
</li>
<li><strong>Time sharing tasks</strong> using a <strong>preemptive</strong> approach (similar to <strong>variable</strong> in Windows)</li>
</ul>
</li>
<li>The most recent scheduling algorithm in Linux for <strong>time sharing tasks</strong> is the “<strong>completely fair scheduler</strong>” </li>
</ul>
<p>Real-Time Tasks</p>
<ul>
<li><strong>Real time FIFO</strong> tasks have the <strong>highest priority</strong> and are scheduled using a <strong>FCFS approach</strong>, using <strong>preemption if a higher priority</strong> job shows up</li>
<li><strong>Real time round robin tasks</strong> are preemptable by <strong>clock interrupts</strong> and have a time slice associated with them</li>
<li>Both approaches <strong>cannot guarantee hard deadlines</strong></li>
</ul>
<p>Time Sharing Tasks</p>
<ul>
<li>The CFS <strong>divides the CPU time</strong> between all processes </li>
<li>If all N processes have the <strong>same priority</strong>:<ul>
<li>They will be allocated a “time slice” equal to 1/N times the available CPU time<ul>
<li>I.e., if N equals 5, every process will receive 20% of the processor’s time</li>
</ul>
</li>
</ul>
</li>
<li>The length of the <strong>time slice</strong> and the “available CPU time” are based on the <strong>targeted latency[延迟]</strong> (⇒ every process should <strong>run at least once</strong> during this interval)</li>
<li>If N is <strong>very large</strong>, the <strong>context switch time will be dominant</strong>, hence a lower bound on the “time slice” is imposed by the minimum granularity[粒度]<ul>
<li>A process’s time slice can be no <strong>less than</strong> the <strong>minimum granularity</strong> (response time will deteriorate[恶化])</li>
</ul>
</li>
<li>A <strong>weighting scheme</strong> is used to take different priorities into account If process have <strong>different priorities:</strong><ul>
<li>Every process <em>i</em> is allocated a <strong>weight</strong> <em>Wi</em> that reflects its priority</li>
</ul>
</li>
<li>The tasks with the <strong>lowest amount</strong> of “<strong>used CPU time</strong>” are <strong>selected first</strong></li>
</ul>
<p>Shared Queues  </p>
<ul>
<li>A single or multi-level queue <strong>shared</strong> between all CPUs </li>
<li>Advantage: automatic <strong>load balancing</strong><br>Disadvantages:<ul>
<li><strong>Contention</strong>[竞争] for the queues (locking is needed)</li>
<li>“All CPUs are equal, but some are more equal than others” : does not account for <strong>processor affinity</strong>[紧密度]:<ul>
<li><strong>Cache</strong> becomes invalid when moving to a different CPU</li>
<li>Translation look aside buffers (TLBs - part of the MMU) become invalid</li>
</ul>
</li>
</ul>
</li>
<li>Windows will allocate the <strong>highest priority threads</strong> to the individual CPUs/cores</li>
</ul>
<p>Private Queues</p>
<ul>
<li>Each process/thread is assigned to a queue <strong>private</strong> to an individual CPU </li>
<li>Advantages:<ul>
<li><strong>CPU affinity</strong> is automatically satisfied </li>
<li><strong>Contention</strong> for shared queue is minimised</li>
</ul>
</li>
<li>Disadvantages: less <strong>load balancing</strong></li>
<li><strong>Push</strong> and <strong>pull migration</strong> between CPUs is possible</li>
</ul>
<h3 id="Related-vs-Unrelated-Threads"><a href="#Related-vs-Unrelated-Threads" class="headerlink" title="Related vs. Unrelated Threads"></a>Related vs. Unrelated Threads</h3><p><strong>Related: multiple threads</strong> that <strong>communicate</strong> with one another and <strong>ideally run</strong> together (e.g. search algorithm)</p>
<p><strong>Unrelated</strong>: e.g. processes threads that are <strong>independent</strong>, possibly started by <strong>different users</strong> running <strong>different programs</strong></p>
<h4 id="Related-Threads"><a href="#Related-Threads" class="headerlink" title="Related Threads"></a>Related Threads</h4><ul>
<li>The aim is to get threads <strong>running</strong>, as much as possible, at the <strong>same</strong> time across <strong>multiple CPUs</strong></li>
<li>Approaches include:<ul>
<li><strong>Space</strong> sharing</li>
<li><strong>Gang</strong> scheduling</li>
</ul>
</li>
</ul>
<h4 id="Gang-scheduling"><a href="#Gang-scheduling" class="headerlink" title="Gang scheduling"></a>Gang scheduling</h4><ul>
<li>Time slices are <strong>synchronised</strong>[同步的] and the scheduler <strong>groups threads</strong> together to run simultaneously (as much as possible)</li>
<li>A <strong>preemptive</strong> algorithm</li>
<li><strong>Blocking threads</strong> result in idle CPUs</li>
</ul>
<h2 id="Summary"><a href="#Summary" class="headerlink" title="Summary"></a>Summary</h2><ul>
<li>Scheduling on Windows and Linux</li>
<li>Multi-processor/core scheduling is “a bit different” (load balancing, processor affinity, etc.)<ul>
<li>Related and unrelated threads </li>
<li>Shared or private queues</li>
<li>Space scheduling or gang scheduling</li>
</ul>
</li>
</ul>
]]></content>
    
    <summary type="html">
    
      &lt;h1 id=&quot;Processes-4&quot;&gt;&lt;a href=&quot;#Processes-4&quot; class=&quot;headerlink&quot; title=&quot;Processes 4&quot;&gt;&lt;/a&gt;Processes 4&lt;/h1&gt;&lt;hr&gt;
&lt;h2 id=&quot;Overview&quot;&gt;&lt;a href=&quot;#Overview&quot; class=&quot;headerlink&quot; title=&quot;Overview&quot;&gt;&lt;/a&gt;Overview&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;Multi-level feedback queues&lt;/li&gt;
&lt;li&gt;Scheduling in Window 7 + illustration Scheduling in &lt;/li&gt;
&lt;li&gt;Linux (implementation in labs) Scheduling related processes/threads&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;Multi-level-Feedback-Queues&quot;&gt;&lt;a href=&quot;#Multi-level-Feedback-Queues&quot; class=&quot;headerlink&quot; title=&quot;Multi-level Feedback Queues&quot;&gt;&lt;/a&gt;Multi-level Feedback Queues&lt;/h3&gt;&lt;ul&gt;
&lt;li&gt;Different &lt;strong&gt;scheduling algorithms&lt;/strong&gt; can be used for the &lt;strong&gt;individual queues&lt;/strong&gt; (e.g., round robin, SJF, FCFS)&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Feedback queues&lt;/strong&gt; allow &lt;strong&gt;priorities to change dynamically&lt;/strong&gt;[动态的], i.e., jobs can move between queues:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Move to &lt;strong&gt;lower priority queue&lt;/strong&gt; if too much CPU time is used (prioritise I/O and interactive processes)&lt;/li&gt;
&lt;li&gt;Move to &lt;strong&gt;higher priority queue&lt;/strong&gt; to prevent &lt;strong&gt;starvation&lt;/strong&gt; and avoid &lt;strong&gt;inversion[反转] of control&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Defining characteristics of feedback queues include: &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;number of queues&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;scheduling algorithms&lt;/strong&gt; used for the individual queues &lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Migration policy&lt;/strong&gt;[迁移政策] between queues&lt;/li&gt;
&lt;li&gt;Initial &lt;strong&gt;access&lt;/strong&gt; to the queues&lt;br&gt;Feedback queues are highly &lt;strong&gt;configurable&lt;/strong&gt;[可配置] and offer significant &lt;strong&gt;flexibility&lt;/strong&gt;[灵活性，适应性]&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
    
    </summary>
    
      <category term="笔记" scheme="http://www.barondu.com/categories/%E7%AC%94%E8%AE%B0/"/>
    
    
      <category term="OSC" scheme="http://www.barondu.com/tags/OSC/"/>
    
      <category term="processes" scheme="http://www.barondu.com/tags/processes/"/>
    
  </entry>
  
  <entry>
    <title>笔记 OSC_Processes3</title>
    <link href="http://www.barondu.com/2017/01/08/Processes3/"/>
    <id>http://www.barondu.com/2017/01/08/Processes3/</id>
    <published>2017-01-08T15:27:27.000Z</published>
    <updated>2019-07-01T20:09:51.000Z</updated>
    
    <content type="html"><![CDATA[<h1 id="Processes-3"><a href="#Processes-3" class="headerlink" title="Processes 3"></a>Processes 3</h1><hr>
<h2 id="Overview"><a href="#Overview" class="headerlink" title="Overview"></a>Overview</h2><p>1 Threads vs. processes<br>2 Different thread implementations<br>3 POSIX Threads (PThreads)</p>
<h3 id="Threads"><a href="#Threads" class="headerlink" title="Threads"></a>Threads</h3><h4 id="Threads-from-an-OS-Perspective-观点"><a href="#Threads-from-an-OS-Perspective-观点" class="headerlink" title="Threads from an OS Perspective[观点]"></a>Threads from an OS Perspective[观点]</h4><ul>
<li>A process consists of two <strong>fundamental units</strong><ul>
<li><strong>Resources:</strong> all related resources are grouped together<ul>
<li>A logical address space containing the process image (program, data, heap, stack)</li>
<li>Files, I/O devices, I/O channels, . . .</li>
</ul>
</li>
<li><strong>Execution trace</strong>[执行追踪], i.e., an entity that gets executed</li>
</ul>
</li>
<li>A process can <strong>share its resources</strong> between <strong>multiple execution traces</strong>, i.e., multiple threads running in the same resource environment<br><img src="https://app.yinxiang.com/shard/s67/res/fcbec65f-172c-405a-83fc-bee2a9a3f4ea.jpg" alt=""></li>
</ul>
<a id="more"></a>
<ul>
<li>Every thread has its own <strong>execution context</strong> (e.g. program counter, stack, registers)</li>
<li>All threads have <strong>access</strong> to the process’ <strong>shared resources</strong><ul>
<li>E.g. files, one thread opens a file, all threads of the same process can access the file</li>
<li>Global variables, memory, etc. (⇒ synchronisation!)</li>
</ul>
</li>
<li>Some CPUs (hyperthreaded ones) have direct <strong>hardware support</strong> for <strong>multi-threading</strong></li>
<li>Similar to processes, threads have:<ul>
<li><strong>States</strong> and <strong>transitions</strong> (new, running, blocked, ready, terminated) </li>
<li>A <strong>thread control block</strong><br>Threads create/terminate/switch with <strong>less overhead</strong>  (address space remains the same for threads of the same process)</li>
</ul>
</li>
<li><strong>Inter-thread</strong> communication is easier/faster than <strong>inter-process</strong> communication (threads share memory by default)</li>
<li><strong>No protection boundaries</strong>[边界] are required in the address space (threads are cooperating, belong to the same user, and have a common goal)</li>
<li><strong>Synchronisation</strong> has to be considered carefully!</li>
</ul>
<h4 id="Why-Use-Threads"><a href="#Why-Use-Threads" class="headerlink" title="Why Use Threads"></a>Why Use Threads</h4><ul>
<li><strong>Multiple related activities</strong> apply to the <strong>same resources</strong>, these resources should be accessible/<strong>shared</strong></li>
<li>Processes will often contain <strong>multiple blocking tasks</strong><ul>
<li>I/O operations (thread blocks, interrupt marks completion)</li>
<li>Memory access: pages faults are result in blocking</li>
</ul>
</li>
<li>Such activities should be carried out in <strong>parallel/concurrently</strong></li>
<li><strong>Application examples</strong>: webservers, make program, spreadsheets, word processors, processing large data volumes</li>
</ul>
<h4 id="OS-Implementations-of-Threads"><a href="#OS-Implementations-of-Threads" class="headerlink" title="OS Implementations of Threads"></a>OS Implementations of Threads</h4><ul>
<li><strong>User</strong> threads</li>
<li><strong>Kernel</strong> threads</li>
<li><strong>Hybrid</strong>[混合] implementations</li>
</ul>
<h3 id="User-Threads"><a href="#User-Threads" class="headerlink" title="User Threads"></a>User Threads</h3><p>Many-to-One</p>
<p><img src="https://app.yinxiang.com/shard/s67/res/bffd3464-1e96-4ed7-8ef7-175e8d3ff770.jpg" alt=""></p>
<ul>
<li><strong>Thread management</strong> (creating, destroying, scheduling, thread control block manipulation[处理]) is carried out <strong>in user space</strong> with the help of a user library</li>
<li>The process maintains a <strong>thread table</strong> managed by the <strong>runtime system</strong> without the <strong>kernel’s knowledge</strong><ul>
<li>Similar to <strong>process table</strong></li>
<li>Used for <strong>thread switching</strong> </li>
<li>Tracks thread related information</li>
</ul>
</li>
</ul>
<p>Advantages:</p>
<ul>
<li>Threads are in user space (i.e., <strong>no mode switches</strong> required)</li>
<li><strong>Full control</strong> over the thread scheduler</li>
<li><strong>OS independent</strong>[独立不受约束] (threads can run on OS that do not support them)</li>
</ul>
<p>Disadvantages:</p>
<ul>
<li><strong>Blocking system calls</strong> suspend[延缓] the entire process (user threads are mapped onto a single process, managed by the kernel)</li>
<li><strong>No true parallelism</strong> (a process is scheduled on a single CPU)</li>
<li><strong>Clock interrupts</strong> are non-existent (i.e. user threads are non-preemptive) </li>
<li><strong>Page faults</strong>[错误] result in blocking the process</li>
</ul>
<h3 id="Kernel-Threads"><a href="#Kernel-Threads" class="headerlink" title="Kernel Threads"></a>Kernel Threads</h3><p>One-to-One<br><img src="https://app.yinxiang.com/shard/s67/res/3561639a-3713-4516-9a84-2ff445932f5a.jpg" alt=""></p>
<ul>
<li>The <strong>kernel manages</strong> the threads, user application accesses threading facilities[工具] through <strong>API</strong> and <strong>system calls</strong><ul>
<li><strong>Thread table</strong> is in the kernel, containing thread control blocks (subset of process control blocks)</li>
<li>If a <strong>thread blocks</strong>, the kernel chooses thread from same or different process (↔ user threads)</li>
</ul>
</li>
<li>Windows and Linux apply this approach</li>
</ul>
<p>Advantages:</p>
<ul>
<li>True parallelism can be achieved </li>
<li>No run-time system needed</li>
</ul>
<p>Disadvantage:</p>
<ul>
<li>Frequent <strong>mode switches</strong> take place, resulting in lower performance <ul>
<li>Frequent <strong>mode switches</strong> take place, resulting in <strong>lower performance</strong></li>
</ul>
</li>
</ul>
<h3 id="Performance"><a href="#Performance" class="headerlink" title="Performance"></a>Performance</h3><p><img src="https://app.yinxiang.com/shard/s67/res/711d575f-df77-4427-95f8-ef69746adcbc.jpg" alt=""></p>
<h3 id="Hybrid-Implementations"><a href="#Hybrid-Implementations" class="headerlink" title="Hybrid Implementations"></a>Hybrid Implementations</h3><p>Many-to-Many<br><img src="https://app.yinxiang.com/shard/s67/res/ecb0e81e-b736-456b-ba9b-e4e088167714.jpg" alt=""></p>
<ul>
<li>User threads are <strong>multiplexed</strong> onto kernel threads</li>
<li>Kernel sees and schedules the kernel threads (a limited number) </li>
<li>User application sees user threads and creates/schedules these (an “unrestricted” number)</li>
</ul>
<h3 id="Comparison"><a href="#Comparison" class="headerlink" title="Comparison"></a>Comparison</h3><p><img src="https://app.yinxiang.com/shard/s67/res/15bd0016-160c-4497-9691-ac580c27f165.jpg" alt=""></p>
<h3 id="Thread-Management"><a href="#Thread-Management" class="headerlink" title="Thread Management"></a>Thread Management</h3><ul>
<li>Thread libraries provide an <strong>API/interface</strong> for managing threads (e.g. creating, running, destroying, synchronising, etc.)</li>
<li>Thread libraries can be implemented:<ul>
<li>Entirely in <strong>user spac</strong>e (i.e. user threads)</li>
<li>Based on <strong>system calls</strong></li>
</ul>
</li>
<li>Examples of thread APIs include <strong>POSIX’s PThreads</strong>, Windows Threads, and Java Threads</li>
<li>The PThread specification can be implemented as user or kernel threads</li>
<li>POSIX threads are a <strong>specification</strong> that “anyone” can implement, i.e., it defines a set of APIs (function calls, over 60 of them) and what they do</li>
</ul>
<h2 id="Summary"><a href="#Summary" class="headerlink" title="Summary"></a>Summary</h2><ol>
<li>Threads vs. processes</li>
<li>Thread implementations (user, kernel and hybrid)</li>
<li>PThreads</li>
</ol>
]]></content>
    
    <summary type="html">
    
      &lt;h1 id=&quot;Processes-3&quot;&gt;&lt;a href=&quot;#Processes-3&quot; class=&quot;headerlink&quot; title=&quot;Processes 3&quot;&gt;&lt;/a&gt;Processes 3&lt;/h1&gt;&lt;hr&gt;
&lt;h2 id=&quot;Overview&quot;&gt;&lt;a href=&quot;#Overview&quot; class=&quot;headerlink&quot; title=&quot;Overview&quot;&gt;&lt;/a&gt;Overview&lt;/h2&gt;&lt;p&gt;1 Threads vs. processes&lt;br&gt;2 Different thread implementations&lt;br&gt;3 POSIX Threads (PThreads)&lt;/p&gt;
&lt;h3 id=&quot;Threads&quot;&gt;&lt;a href=&quot;#Threads&quot; class=&quot;headerlink&quot; title=&quot;Threads&quot;&gt;&lt;/a&gt;Threads&lt;/h3&gt;&lt;h4 id=&quot;Threads-from-an-OS-Perspective-观点&quot;&gt;&lt;a href=&quot;#Threads-from-an-OS-Perspective-观点&quot; class=&quot;headerlink&quot; title=&quot;Threads from an OS Perspective[观点]&quot;&gt;&lt;/a&gt;Threads from an OS Perspective[观点]&lt;/h4&gt;&lt;ul&gt;
&lt;li&gt;A process consists of two &lt;strong&gt;fundamental units&lt;/strong&gt;&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Resources:&lt;/strong&gt; all related resources are grouped together&lt;ul&gt;
&lt;li&gt;A logical address space containing the process image (program, data, heap, stack)&lt;/li&gt;
&lt;li&gt;Files, I/O devices, I/O channels, . . .&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Execution trace&lt;/strong&gt;[执行追踪], i.e., an entity that gets executed&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;A process can &lt;strong&gt;share its resources&lt;/strong&gt; between &lt;strong&gt;multiple execution traces&lt;/strong&gt;, i.e., multiple threads running in the same resource environment&lt;br&gt;&lt;img src=&quot;https://app.yinxiang.com/shard/s67/res/fcbec65f-172c-405a-83fc-bee2a9a3f4ea.jpg&quot; alt=&quot;&quot;&gt;&lt;/li&gt;
&lt;/ul&gt;
    
    </summary>
    
      <category term="笔记" scheme="http://www.barondu.com/categories/%E7%AC%94%E8%AE%B0/"/>
    
    
      <category term="OSC" scheme="http://www.barondu.com/tags/OSC/"/>
    
      <category term="processes" scheme="http://www.barondu.com/tags/processes/"/>
    
  </entry>
  
  <entry>
    <title>笔记 OSC_Processes2</title>
    <link href="http://www.barondu.com/2017/01/08/Processes2/"/>
    <id>http://www.barondu.com/2017/01/08/Processes2/</id>
    <published>2017-01-08T15:26:14.000Z</published>
    <updated>2019-07-01T21:31:59.000Z</updated>
    
    <content type="html"><![CDATA[<h1 id="Processes-2"><a href="#Processes-2" class="headerlink" title="Processes 2"></a>Processes 2</h1><hr>
<h2 id="Overview"><a href="#Overview" class="headerlink" title="Overview"></a>Overview</h2><ul>
<li>Introduction to <strong>process scheduling</strong></li>
<li>Types of <strong>process schedulers</strong></li>
<li><strong>Evaluation criteria</strong> for scheduling algorithms</li>
<li>Typical <strong>process scheduling algorithms</strong></li>
</ul>
<h3 id="Process-Scheduling"><a href="#Process-Scheduling" class="headerlink" title="Process Scheduling"></a>Process Scheduling</h3><p>Context</p>
<ul>
<li>The OS is responsible for <strong>managing</strong> and <strong>scheduling processes</strong><ul>
<li>Decide when to <strong>admit</strong> processes to the system (new → ready)</li>
<li>Decide which process to r<strong>un</strong> next (ready → run)</li>
<li>Decide when and which processes to <strong>interrupt</strong> (running → ready)</li>
</ul>
</li>
<li>It relies on the <strong>scheduler</strong> (dispatcher) to decide which process to run next, which uses a <strong>scheduling algorithm</strong> to do so</li>
<li>The type of algorithm used by the scheduler is influenced by the <strong>type of operating system</strong> (e.g., real time vs. batch)</li>
</ul>
<a id="more"></a>
<p>Classification by Time Horizon</p>
<ul>
<li><strong>Long term</strong>: applies to new processes and controls the degree of multiprogramming by deciding which processes to <strong>admit</strong> to the system<ul>
<li>A good <strong>mix</strong> of <strong>CPU</strong> and <strong>I/O bound processes</strong> is favourable to keep all resources as busy as possible</li>
<li><strong>Usually absent</strong> in popular modern OS</li>
</ul>
</li>
<li><strong>Medium term</strong>: controls <strong>swapping</strong> and the degree of multi-programming </li>
<li><strong>Short term</strong>: decide which process to <strong>run</strong> next<ul>
<li>Usually called in response to <strong>clock interrupts</strong>, <strong>I/O interrupts</strong>, or <strong>blocking system calls</strong></li>
<li>Invoked[调用] very <strong>frequently,</strong> hence must be fast</li>
<li>Manages the <strong>ready queue</strong></li>
</ul>
</li>
</ul>
<p>Classification by Approach</p>
<ul>
<li><strong>Non-preemptive</strong>: processes are only interrupted voluntarily[自愿的] (e.g., I/O operation or “nice” system call – yield())</li>
<li><strong>Preemptive</strong>[优先的]: processes can be <strong>interrupted forcefully</strong> or <strong>voluntarily</strong><ul>
<li>This requires context switches which generate overhead, too many of them should be avoided<br>Prevents processes from <strong>monopolising[垄断的] the CPU</strong></li>
<li><strong>Most popula</strong>r modern operating systems are preemptive</li>
</ul>
</li>
</ul>
<h3 id="Performance-Assessment-性能评估"><a href="#Performance-Assessment-性能评估" class="headerlink" title="Performance Assessment[性能评估]"></a>Performance Assessment[性能评估]</h3><p><strong>User oriented[导向]criteria:</strong></p>
<ul>
<li><strong>Response time:</strong> minimise the time between creating the job and its first execution</li>
<li><strong>Turnaround time</strong>: minimise the time between creating the job and finishing it</li>
<li><strong>Predictability</strong>[可调度性]: minimise the variance[差异，方差] in processing times</li>
</ul>
<p><strong>System oriented criteria:</strong></p>
<ul>
<li><strong>Throughput</strong>[吞吐量]: maximise the number of jobs processed per hour</li>
<li><strong>Fairness</strong>[公平性]:<ul>
<li>Are processing power/waiting time equally distributed?</li>
<li>Are some processes kept waiting excessively long (starvation)</li>
</ul>
</li>
</ul>
<p>Evaluation criteria can be <strong>conflicting</strong>, i.e., <strong>reducing the response time</strong> may i<strong>ncrease context switches</strong> and may <strong>worsen the throughput</strong> and <strong>increase the turn around time</strong></p>
<h3 id="Scheduling-Algorithms"><a href="#Scheduling-Algorithms" class="headerlink" title="Scheduling Algorithms"></a>Scheduling Algorithms</h3><p><strong>Algorithms</strong> considered:</p>
<ol>
<li>First Come First Served (<strong>FCFS</strong>)/ First In First Out (<strong>FIFO</strong>)</li>
<li><strong>Shortest job first</strong></li>
<li><strong>Round Robin</strong></li>
<li><strong>Priority queues</strong></li>
</ol>
<p>Performance measures used:</p>
<ul>
<li><strong>Average response time</strong></li>
<li><strong>Average turnaround time</strong></li>
</ul>
<p><code>First Come First Served</code></p>
<ul>
<li>Concept: a <strong>non-preemtive algorithm</strong> that operates as a <strong>strict[严格的] queueing mechanism[机制]</strong> and schedules the processes in the same order that they were added to the queue</li>
<li>Advantages: <strong>positional fairness</strong> and easy to implement </li>
<li>Disadvantages:<ul>
<li><strong>good for long processes</strong> over short ones</li>
<li>Could <strong>compromise[危害] resource utilisation,</strong> i.e., CPU vs. I/O devices<br><img src="https://app.yinxiang.com/shard/s67/res/22893583-e57f-4ffc-a3ee-ccc26a4e022e.jpg" alt="FCFS"></li>
</ul>
</li>
</ul>
<p><code>Shortest Job First</code></p>
<ul>
<li>Concept: A <strong>non-preemtive algorithm</strong> that starts processes in order of <strong>ascending[递增] processing time</strong> using a provided/known estimate of the processing</li>
<li>Advantages: always result in the <strong>good turn around time</strong> </li>
<li>Disadvantages:<ul>
<li><strong>Starvation</strong>[饿死] might occur</li>
<li><strong>Fairness</strong> and <strong>predictability</strong> are compromised </li>
<li><strong>Processing times have to be known</strong> beforehand<br><img src="https://app.yinxiang.com/shard/s67/res/9338de83-4f53-48c5-b449-5b1ccd5d03f9.jpg" alt="Shortest Job First"></li>
</ul>
</li>
</ul>
<p><code>Round Robin</code></p>
<ul>
<li>Concept: a <strong>preemptive version of FCFS</strong> that forces <strong>context switches</strong> at <strong>periodic[周期性] intervals[间隔]</strong> or <strong>time slices</strong></li>
<li>Processes run in the order that they were added to the queue </li>
<li>Processes are <strong>forcefully interrupted by the timer</strong><br>Advantages:</li>
<li>Improved <strong>response time</strong></li>
<li><strong>Effective</strong> for general purpose <strong>time sharing systems</strong><br>Disadvantages:</li>
<li>Increased <strong>context switching</strong> and thus overhead</li>
<li><strong>Favours CPU bound processes</strong> (which usually run long) over I/O processes (which do not run long)</li>
<li>Can <strong>reduce to FCFS</strong></li>
</ul>
<p>The length of the time slice must be carefully considered!</p>
<ul>
<li>a <strong>low response time</strong> is achieved with <strong>a small time slice</strong> (e.g. 1ms) ⇒ low throughput</li>
<li>a <strong>high throughput</strong> is achieved with <strong>a large time slice</strong> (e.g. 1000ms) ⇒ high response time</li>
</ul>
<p>If a time slice is only <strong>used</strong> <strong>partially</strong>, the next process <strong>starts immediately</strong><br><img src="https://app.yinxiang.com/shard/s67/res/0a123f9a-693e-4ad2-a44e-d113f4a88a93.jpg" alt="Round Robin"></p>
<p><code>Priority Queues</code></p>
<ul>
<li>Concept: A <strong>preemptive algorithm</strong> that schedules processes by <strong>priority</strong> (high → low)<ul>
<li>The process priority is saved in the <strong>process control block</strong> </li>
</ul>
</li>
<li>Advantages: can <strong>prioritise[优先] I/O bound jobs</strong></li>
<li>Disadvantages: low priority processes may suffer from <strong>starvation</strong> (with static priorities)</li>
<li><img src="https://app.yinxiang.com/shard/s67/res/6c578ed6-d139-41b6-8783-f9d1ee1d3ee4.jpg" alt="Priority Queues"></li>
</ul>
<p>##Summary</p>
<ul>
<li><strong>Types of schedulers</strong>: preemptive/non-preemptive, long/medium/short term)</li>
<li>Performance <strong>evaluation criteria</strong></li>
<li><strong>Scheduling algorithms</strong>: FCFS, SJF, Round Robin, Priority Queues</li>
</ul>
]]></content>
    
    <summary type="html">
    
      &lt;h1 id=&quot;Processes-2&quot;&gt;&lt;a href=&quot;#Processes-2&quot; class=&quot;headerlink&quot; title=&quot;Processes 2&quot;&gt;&lt;/a&gt;Processes 2&lt;/h1&gt;&lt;hr&gt;
&lt;h2 id=&quot;Overview&quot;&gt;&lt;a href=&quot;#Overview&quot; class=&quot;headerlink&quot; title=&quot;Overview&quot;&gt;&lt;/a&gt;Overview&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;Introduction to &lt;strong&gt;process scheduling&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Types of &lt;strong&gt;process schedulers&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Evaluation criteria&lt;/strong&gt; for scheduling algorithms&lt;/li&gt;
&lt;li&gt;Typical &lt;strong&gt;process scheduling algorithms&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;Process-Scheduling&quot;&gt;&lt;a href=&quot;#Process-Scheduling&quot; class=&quot;headerlink&quot; title=&quot;Process Scheduling&quot;&gt;&lt;/a&gt;Process Scheduling&lt;/h3&gt;&lt;p&gt;Context&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The OS is responsible for &lt;strong&gt;managing&lt;/strong&gt; and &lt;strong&gt;scheduling processes&lt;/strong&gt;&lt;ul&gt;
&lt;li&gt;Decide when to &lt;strong&gt;admit&lt;/strong&gt; processes to the system (new → ready)&lt;/li&gt;
&lt;li&gt;Decide which process to r&lt;strong&gt;un&lt;/strong&gt; next (ready → run)&lt;/li&gt;
&lt;li&gt;Decide when and which processes to &lt;strong&gt;interrupt&lt;/strong&gt; (running → ready)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;It relies on the &lt;strong&gt;scheduler&lt;/strong&gt; (dispatcher) to decide which process to run next, which uses a &lt;strong&gt;scheduling algorithm&lt;/strong&gt; to do so&lt;/li&gt;
&lt;li&gt;The type of algorithm used by the scheduler is influenced by the &lt;strong&gt;type of operating system&lt;/strong&gt; (e.g., real time vs. batch)&lt;/li&gt;
&lt;/ul&gt;
    
    </summary>
    
      <category term="笔记" scheme="http://www.barondu.com/categories/%E7%AC%94%E8%AE%B0/"/>
    
    
      <category term="OSC" scheme="http://www.barondu.com/tags/OSC/"/>
    
      <category term="processes" scheme="http://www.barondu.com/tags/processes/"/>
    
  </entry>
  
  <entry>
    <title>笔记 OSC_Processes1</title>
    <link href="http://www.barondu.com/2017/01/06/Processes1/"/>
    <id>http://www.barondu.com/2017/01/06/Processes1/</id>
    <published>2017-01-06T03:42:14.000Z</published>
    <updated>2019-07-01T20:16:12.000Z</updated>
    
    <content type="html"><![CDATA[<h1 id="Processes-1"><a href="#Processes-1" class="headerlink" title="Processes 1"></a>Processes 1</h1><hr>
<h2 id="Overview"><a href="#Overview" class="headerlink" title="Overview"></a>Overview</h2><ul>
<li>Introduction to <strong>processes</strong> and their <strong>implementation</strong> </li>
<li>Process <strong>states</strong> and state <strong>transitions</strong></li>
<li><strong>System calls</strong> for process management</li>
</ul>
<h3 id="Processes-and-Implementation"><a href="#Processes-and-Implementation" class="headerlink" title="Processes and Implementation"></a>Processes and Implementation</h3><blockquote>
<p>Definition: <em>“a process is a <strong>running instance</strong> of a program”</em><br><code>进程是程序的运行实例</code></p>
</blockquote>
<ul>
<li>A process is registered with the OS using its “<strong>control structures</strong>”: i.e. an entry in the OS’s <strong>process tab</strong>le to a <strong>process control blocks</strong> (PCB)</li>
<li>The <strong>process control block</strong> contains all information necessary to <strong>manage the process</strong> and is necessary for <strong>context switching</strong> in <strong>multi-programmed systems</strong></li>
<li>A process’ memory image contains:<ul>
<li>The program <strong>code</strong> (could be shared between multiple processes running the same code)</li>
<li>A <strong>data</strong> segment, <strong>stack</strong> and <strong>heap</strong></li>
</ul>
</li>
<li>Every process has its own <strong>logical address</strong> space, in which the stack and heap are placed at opposite[相反的] sides to allow them to grow</li>
</ul>
<a id="more"></a>
<p><img src="https://app.yinxiang.com/shard/s67/res/f2a21b3c-2c65-48e6-8fc5-c8c8879ae34d.jpg" alt="process’ memory image"></p>
<h3 id="Process-States-and-Transitions"><a href="#Process-States-and-Transitions" class="headerlink" title="Process States and Transitions"></a>Process States and Transitions</h3><p><img src="https://app.yinxiang.com/shard/s67/res/a41da748-6230-4674-b240-bd39445f862f.jpg" alt="Process States and Transitions"><br>Sates:</p>
<ul>
<li>A <strong>new</strong> process has just been created (has a PCB) and is waiting to be admitted (it may not yet be in memory)</li>
<li>A <strong>ready</strong> process is waiting for CPU to become available (e.g. unblocked or timer interrupt)</li>
<li>A <strong>running</strong> process “owns” the CPU</li>
<li>A <strong>blocked</strong> process cannot continue, e.g. is waiting for I/O</li>
<li>A <strong>terminated</strong> process is no longer executable (the data structures - PCB - may be temporarily preserved)</li>
<li>A <strong>suspended</strong>[废除的] process is swapped out[换出] (not discussed further)</li>
</ul>
<p>Transitions：</p>
<ol>
<li><strong>New -&gt; ready</strong>: admit the process and commit to execution</li>
<li><strong>Running -&gt; blocked:</strong> e.g. process is waiting for input or carried out a<br>system call</li>
<li><strong>Ready -&gt; running:</strong> the process is selected by the process scheduler</li>
<li><strong>Blocked -&gt; ready</strong>: event happens, e.g. I/O operation has finished</li>
<li>. <strong>Running -&gt; ready</strong>: the process is preempted, e.g., by a timer interrupt or<br>by pause</li>
<li><strong>Running -&gt;strong text</strong> exit: process has finished, e.g. program ended or exception<br>encountered</li>
</ol>
<p>The interrupts/traps/system calls lie on the basis of the transitions</p>
<p><img src="https://app.yinxiang.com/shard/s67/res/1b1ddbee-3544-4bf3-9d2e-162d1033f7a1.jpg" alt="OS queues"></p>
<h3 id="Context-Switching"><a href="#Context-Switching" class="headerlink" title="Context Switching"></a>Context Switching</h3><p>Multi-programming</p>
<ul>
<li>Modern computers are <strong>multi-programming systems</strong></li>
<li><p>Assuming a single processor system, the instructions of individual processes are executed sequentially</p>
<ul>
<li>Multi-programming goes back to the “<strong>MULTICS</strong>” age </li>
<li>Multi-programming is achieved by <strong>alternating</strong>[交替]processes and <strong>context switching</strong></li>
<li><strong>True parallelism</strong> requires <strong>multiple processors</strong><br><code>并不是真正的multi-programming</code></li>
</ul>
</li>
<li><p>When a <strong>context switch</strong> takes place, the system <strong>saves the state</strong> of the old process and <strong>loads the state</strong> of the new process (creates <strong>overhead</strong>)</p>
<ul>
<li><strong>Saved</strong> -&gt; the process control block is <strong>updated</strong></li>
<li><strong>(Re-)started</strong> -&gt; the process control block <strong>read</strong></li>
</ul>
</li>
<li>A <strong>trade-off</strong>[权衡] exists between the length of the <strong>time-slice</strong> and the <strong>context switch time</strong><ul>
<li><strong>Short time slices</strong> result in <strong>good response times</strong> but <strong>low effective “utilisation”</strong>[使用]<ul>
<li>e.g.: 99*(1+1)=198ms</li>
</ul>
</li>
<li><strong>Long time slices</strong> result in <strong>poor response</strong> times but b<strong>etter effective “utilisation”</strong><ul>
<li>e.g.: 99 * (100 + 1) = 9999ms</li>
</ul>
</li>
</ul>
</li>
<li>A <strong>process control block</strong> contains <strong>three</strong> types of <strong>attributes:</strong><ul>
<li><strong>Process identification</strong> (PID, UID, Parent PID)</li>
<li><strong>Process control information</strong> (process state, scheduling information, etc.) </li>
<li><strong>Process state information</strong> (user registers, program counter, stack pointer, program status word, memory management information, files, etc.)</li>
</ul>
</li>
<li><strong>Process control blocks</strong> are <strong>kernel data structures</strong>, i.e. they are <strong>protected</strong> and only accessible in <strong>kernel mode!</strong><ul>
<li>Allowing user applications to access them directly could <strong>compromise[威胁] their integrity[完整性]</strong></li>
<li>The <strong>operating system manages</strong> them on the user’s behalf through <strong>system calls</strong> (e.g. to set <strong>process priority</strong>)</li>
</ul>
</li>
</ul>
<p>Tables and Control Blocks</p>
<ul>
<li>An operating system <strong>maintains information</strong> about the status of “resources” in <strong>tables</strong><ul>
<li><strong>Process tables</strong> (process control blocks)</li>
<li><strong>Memory tables</strong> (memory allocation, memory protection, virtual memory) </li>
<li><strong>I/O tables</strong> (availability, status, transfer information)</li>
<li><strong>File tables</strong> (location, status)</li>
</ul>
</li>
<li>The <strong>process table</strong> holds a p<strong>rocess control block</strong> for each process, allocated upon <strong>process creation</strong></li>
<li>Tables are maintained by the <strong>kernel</strong> and are usually <strong>cross referenced</strong><br>Switching Processes<blockquote>
<ol>
<li>Save process state (program counter, registers)</li>
<li>Update PCB (running -&gt; ready/blocked)</li>
<li>Move PCB to appropriate queue (ready/blocked)</li>
<li>Run scheduler, select new process</li>
<li>Update to running state in the new PCB</li>
<li>Update memory management unit (MMU)</li>
<li>Restore process</li>
</ol>
</blockquote>
</li>
</ul>
<h3 id="System-Calls"><a href="#System-Calls" class="headerlink" title="System Calls"></a>System Calls</h3><ul>
<li>System calls are necessary to <strong>notify the OS</strong> that the <strong>process has terminated</strong><ul>
<li>Resources must be de-allocated</li>
<li>Output must be flushed</li>
<li>Process admin may have to be carried out</li>
</ul>
</li>
<li>A system calls for process termination:<ul>
<li>UNIX/Linux: <strong>exit()</strong>, kill() </li>
<li>Windows: <strong>TerminateProcess()</strong></li>
</ul>
</li>
</ul>
<h2 id="Summary"><a href="#Summary" class="headerlink" title="Summary"></a>Summary</h2><ul>
<li><strong>Definition of a process</strong> and their <strong>implementation</strong> in operating systems </li>
<li><strong>States</strong>, state <strong>transitions</strong> of processes</li>
<li><strong>Kernel structures</strong> for processes and process management</li>
<li><strong>System calls</strong> for process management</li>
</ul>
]]></content>
    
    <summary type="html">
    
      &lt;h1 id=&quot;Processes-1&quot;&gt;&lt;a href=&quot;#Processes-1&quot; class=&quot;headerlink&quot; title=&quot;Processes 1&quot;&gt;&lt;/a&gt;Processes 1&lt;/h1&gt;&lt;hr&gt;
&lt;h2 id=&quot;Overview&quot;&gt;&lt;a href=&quot;#Overview&quot; class=&quot;headerlink&quot; title=&quot;Overview&quot;&gt;&lt;/a&gt;Overview&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;Introduction to &lt;strong&gt;processes&lt;/strong&gt; and their &lt;strong&gt;implementation&lt;/strong&gt; &lt;/li&gt;
&lt;li&gt;Process &lt;strong&gt;states&lt;/strong&gt; and state &lt;strong&gt;transitions&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;System calls&lt;/strong&gt; for process management&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;Processes-and-Implementation&quot;&gt;&lt;a href=&quot;#Processes-and-Implementation&quot; class=&quot;headerlink&quot; title=&quot;Processes and Implementation&quot;&gt;&lt;/a&gt;Processes and Implementation&lt;/h3&gt;&lt;blockquote&gt;
&lt;p&gt;Definition: &lt;em&gt;“a process is a &lt;strong&gt;running instance&lt;/strong&gt; of a program”&lt;/em&gt;&lt;br&gt;&lt;code&gt;进程是程序的运行实例&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;ul&gt;
&lt;li&gt;A process is registered with the OS using its “&lt;strong&gt;control structures&lt;/strong&gt;”: i.e. an entry in the OS’s &lt;strong&gt;process tab&lt;/strong&gt;le to a &lt;strong&gt;process control blocks&lt;/strong&gt; (PCB)&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;process control block&lt;/strong&gt; contains all information necessary to &lt;strong&gt;manage the process&lt;/strong&gt; and is necessary for &lt;strong&gt;context switching&lt;/strong&gt; in &lt;strong&gt;multi-programmed systems&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;A process’ memory image contains:&lt;ul&gt;
&lt;li&gt;The program &lt;strong&gt;code&lt;/strong&gt; (could be shared between multiple processes running the same code)&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;data&lt;/strong&gt; segment, &lt;strong&gt;stack&lt;/strong&gt; and &lt;strong&gt;heap&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Every process has its own &lt;strong&gt;logical address&lt;/strong&gt; space, in which the stack and heap are placed at opposite[相反的] sides to allow them to grow&lt;/li&gt;
&lt;/ul&gt;
    
    </summary>
    
      <category term="笔记" scheme="http://www.barondu.com/categories/%E7%AC%94%E8%AE%B0/"/>
    
    
      <category term="OSC" scheme="http://www.barondu.com/tags/OSC/"/>
    
      <category term="processes" scheme="http://www.barondu.com/tags/processes/"/>
    
  </entry>
  
  <entry>
    <title>笔记 OSC_Introduction2</title>
    <link href="http://www.barondu.com/2017/01/03/Introduction2/"/>
    <id>http://www.barondu.com/2017/01/03/Introduction2/</id>
    <published>2017-01-03T15:50:14.000Z</published>
    <updated>2019-07-01T20:48:23.000Z</updated>
    
    <content type="html"><![CDATA[<h1 id="Introduction-2"><a href="#Introduction-2" class="headerlink" title="Introduction 2"></a>Introduction 2</h1><hr>
<h2 id="Overview"><a href="#Overview" class="headerlink" title="Overview"></a>Overview</h2><ul>
<li>CPU design</li>
<li><strong>Address spaces, interruts</strong></li>
<li>OS <strong>structures/implementation</strong></li>
</ul>
<h3 id="CPU-design"><a href="#CPU-design" class="headerlink" title="CPU design"></a>CPU design</h3><ul>
<li>A CPU basic cycle consist of <strong>fetch[取], decode, execute</strong></li>
<li>Every CPU has his own <strong>instruction set</strong></li>
<li>CPU has a  set of <strong>registers</strong></li>
<li>Registers are used to store <strong>data</strong> and for <strong>special functions</strong> (e.g. program counter, program status word – mode bit)</li>
<li>The <strong>compiler</strong>/programmer decides what to keep in the registers</li>
<li><strong>Context switching</strong>[上下文切换] must save and restore the CPU’s internal state, including its <strong>registers</strong></li>
</ul>
<a id="more"></a>
<h3 id="Memory-Management-Unit-MMU"><a href="#Memory-Management-Unit-MMU" class="headerlink" title="Memory Management Unit (MMU)"></a>Memory Management Unit (MMU)</h3><ul>
<li>Memory adsresses from 0 to MAX</li>
<li>Variables are <strong>mnemonic</strong>[帮助记忆的] <strong>names</strong> for memory addresses</li>
<li><p>You don’t know where the process will run in <strong>physical memory</strong> at compile time</p>
<ul>
<li><strong>Multiple processes</strong> run on modern machines</li>
<li>The compiler <strong>assumes</strong> that it will start <strong>running at 0 (logical address space)</strong></li>
<li>An <strong>offset</strong>[补偿] is added at runtime by the MMU <strong>(physical address space)</strong><blockquote>
<p>physical address = logical address + offset </p>
</blockquote>
</li>
</ul>
</li>
<li><p>Modern computer use a <strong>logical</strong> and <strong>physical</strong> memory addresses:</p>
<ul>
<li>Every process has a logical address space – [0,<em>MAX64</em>] (theoretically理论上)</li>
<li>The machine has a physical address space – [0, <em>MAX</em> ] (MAX determined by the amount of physical memory)</li>
</ul>
</li>
<li><p><strong>Address translation</strong> takes place in MMU</p>
<blockquote>
<p>physical = f (logical )</p>
</blockquote>
</li>
<li><p>A <strong>context switch</strong> between processes <strong>invalidates</strong>[使无效] the MMU (as well as registers, cache, … )</p>
</li>
</ul>
<h3 id="Timer-interrupts"><a href="#Timer-interrupts" class="headerlink" title="Timer interrupts"></a>Timer interrupts</h3><ul>
<li>Interrupts <strong>temporarily pause</strong> a process’s normal operation</li>
<li>Different types of interrupts:<ul>
<li>Timer interrupts by <strong>CPU clock</strong></li>
<li><strong>I/O interrupts</strong> for I/O completion or error codes </li>
<li><strong>Software generated</strong>, e.g. errors and exceptions<br><img src="https://app.yinxiang.com/shard/s67/res/acbf9eb9-bdb4-426c-85b1-8ea8ff5ab1b7.jpg" alt=""><blockquote>
<ol>
<li>Timer generates an interrupt</li>
<li>CPU finishes current instruction and tests for interrupt</li>
<li>Transfer to interrupt service routine</li>
</ol>
<ul>
<li>Hardware saves current process state (PSW, program counter)</li>
<li>Set program counter to interrupt service routine</li>
<li>Save registers and other state information</li>
</ul>
<ol>
<li>Carry out[执行] interrupt service routine (scheduler)</li>
<li>Restore next process to run</li>
</ol>
</blockquote>
</li>
</ul>
</li>
</ul>
<h3 id="Moore’s-“law”"><a href="#Moore’s-“law”" class="headerlink" title="Moore’s “law”"></a>Moore’s “law”</h3><blockquote>
<p><strong>Moore’s “law”:</strong>  “The number of transistors on an integrated circuit (chip) doubles roughly every two years” </p>
</blockquote>
<ul>
<li>Closely linked, but not necessarily related to performance</li>
<li>The <strong>“power wall”</strong> slows performance improvements of single core/single processor systems<ul>
<li>A few cores for multiple “programs” is easy to justify</li>
<li>How to use <strong>massively[大规模的] parallel</strong> computers/CPUs/many core machines </li>
<li>Can we <strong>extract parallelism automatically</strong>, can we implement parallelism at the lowest level (similar to multiprogramming) </li>
</ul>
</li>
</ul>
<h3 id="Multi-core-hyperthreaded-processors"><a href="#Multi-core-hyperthreaded-processors" class="headerlink" title="Multi-core, hyperthreaded processors"></a>Multi-core, hyperthreaded processors</h3><ul>
<li>Modern CPUs contain <strong>multiple cores</strong> and are often <strong>hyper-threaded</strong> </li>
<li><strong>Evolution in hardware</strong> has implications on operating system design<ul>
<li>XP did not support multi processor architectures</li>
<li><strong>Process scheduling</strong> needs to account for <strong>load balancing</strong> and <strong>CPU affinity</strong>[亲和性]</li>
<li><strong>Cache coherency</strong>[缓存一致性] becomes important</li>
</ul>
</li>
</ul>
<h3 id="Memory"><a href="#Memory" class="headerlink" title="Memory"></a>Memory</h3><ul>
<li>Memory <strong>hierarchies</strong>[层级] used to balance <strong>cost</strong> and <strong>performance</strong> <ul>
<li>Fast and expensive memory is used for <strong>caching</strong></li>
<li>Slow and inexpensive memory is used for <strong>long term storage</strong></li>
<li>Memory includes, registers, L1/L2 cache, main/core memory, disk, etc.</li>
<li><strong>L2 Cache</strong> can be <strong>shared</strong> or <strong>dedicated</strong>[专注的] to individual cores </li>
<li><strong>Cache management</strong> is mainly done by <strong>hardware</strong></li>
<li>The CPU can only <strong>access main memory directly</strong> (i.e. files have to be brought into memory first)</li>
</ul>
</li>
</ul>
<h3 id="I-O-Devices"><a href="#I-O-Devices" class="headerlink" title="I/O Devices"></a>I/O Devices</h3><ul>
<li><strong>Device driver</strong> interacts[交互] with the <strong>controller</strong>, controller interacts with the device (e.g., disk controller)</li>
<li>The operating system/device driver typically <strong>communicates with the controller through registers</strong></li>
<li>I/O can take place through:<ul>
<li><strong>Busy waiting</strong></li>
<li><strong>Interrupt based</strong></li>
<li>Direct memory access (using <strong>DMA</strong> chip)</li>
</ul>
</li>
</ul>
<h3 id="Operating-System-Structure"><a href="#Operating-System-Structure" class="headerlink" title="Operating System Structure"></a>Operating System Structure</h3><ul>
<li>Systems contain a lot of functionality</li>
<li>Operating Systems are structured by <strong>Micro kernels</strong>[微内核] and <strong>Monolithic</strong>[单内核]</li>
</ul>
<h3 id="Micro-Kernels"><a href="#Micro-Kernels" class="headerlink" title="Micro Kernels"></a>Micro Kernels</h3><ul>
<li>All <strong>non-essential functionality</strong> is <strong>extracted</strong>[取出] from the kernel <ul>
<li><strong>Communication, memory management</strong> and <strong>CPU scheduling</strong> are likely to be included in the kernel</li>
<li><strong>The file system, GUI, device drivers</strong> are likely to be user processes<br><code>除了保留基本功能，其他功能移出到user mode</code></li>
</ul>
</li>
<li>Micro kernels are more <strong>easy to extend</strong>, more <strong>portable</strong>[便携], and usually more reliable</li>
<li>Frequent <strong>system calls</strong> and <strong>kernel traps</strong>[陷阱] cause significant <strong>overhead</strong>[开销] (mode switches)</li>
<li>Some Unix version, Mac OS X, Minix, and early versions of Windows (NT4.0) were (partially) micro kernels</li>
</ul>
<h3 id="Monolithic-Systems"><a href="#Monolithic-Systems" class="headerlink" title="Monolithic Systems"></a>Monolithic Systems</h3><ul>
<li>All procedures are <strong>linked together</strong> into one <strong>single executable running</strong> in <strong>kernel mode</strong></li>
<li>Monolithic kernels are <strong>difficult to maintain</strong></li>
<li>Current versions of Windows, Linux are implemented as monolithic kernels<br><code>操作系统高度紧密，移植性不佳。但是若设计完善，效率高</code></li>
</ul>
<h2 id="Summary"><a href="#Summary" class="headerlink" title="Summary"></a>Summary</h2><ul>
<li>Operating Systems are closely linked to <strong>computer architecture</strong> </li>
<li><strong>Address translation</strong> and <strong>interrupts</strong></li>
<li><strong>OS structures</strong></li>
</ul>
]]></content>
    
    <summary type="html">
    
      &lt;h1 id=&quot;Introduction-2&quot;&gt;&lt;a href=&quot;#Introduction-2&quot; class=&quot;headerlink&quot; title=&quot;Introduction 2&quot;&gt;&lt;/a&gt;Introduction 2&lt;/h1&gt;&lt;hr&gt;
&lt;h2 id=&quot;Overview&quot;&gt;&lt;a href=&quot;#Overview&quot; class=&quot;headerlink&quot; title=&quot;Overview&quot;&gt;&lt;/a&gt;Overview&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;CPU design&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Address spaces, interruts&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;OS &lt;strong&gt;structures/implementation&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;CPU-design&quot;&gt;&lt;a href=&quot;#CPU-design&quot; class=&quot;headerlink&quot; title=&quot;CPU design&quot;&gt;&lt;/a&gt;CPU design&lt;/h3&gt;&lt;ul&gt;
&lt;li&gt;A CPU basic cycle consist of &lt;strong&gt;fetch[取], decode, execute&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Every CPU has his own &lt;strong&gt;instruction set&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;CPU has a  set of &lt;strong&gt;registers&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Registers are used to store &lt;strong&gt;data&lt;/strong&gt; and for &lt;strong&gt;special functions&lt;/strong&gt; (e.g. program counter, program status word – mode bit)&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;compiler&lt;/strong&gt;/programmer decides what to keep in the registers&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Context switching&lt;/strong&gt;[上下文切换] must save and restore the CPU’s internal state, including its &lt;strong&gt;registers&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
    
    </summary>
    
      <category term="笔记" scheme="http://www.barondu.com/categories/%E7%AC%94%E8%AE%B0/"/>
    
    
      <category term="OSC" scheme="http://www.barondu.com/tags/OSC/"/>
    
      <category term="Introduction" scheme="http://www.barondu.com/tags/Introduction/"/>
    
  </entry>
  
  <entry>
    <title>笔记 OSC_Introduction1</title>
    <link href="http://www.barondu.com/2017/01/01/Introduction1/"/>
    <id>http://www.barondu.com/2017/01/01/Introduction1/</id>
    <published>2017-01-01T07:06:04.000Z</published>
    <updated>2017-01-02T07:06:04.000Z</updated>
    
    <content type="html"><![CDATA[<h1 id="Introduction-1"><a href="#Introduction-1" class="headerlink" title="Introduction 1"></a>Introduction 1</h1><hr>
<h2 id="Overview"><a href="#Overview" class="headerlink" title="Overview"></a>Overview</h2><ul>
<li>“<strong>Defining</strong>“ operating systems</li>
<li>What is <strong>multi-programming</strong></li>
<li><strong>Kernel-user mode</strong></li>
</ul>
<h3 id="Defining-Operating-Systems"><a href="#Defining-Operating-Systems" class="headerlink" title="Defining Operating Systems"></a>Defining Operating Systems</h3><ul>
<li><p>In the early days, programmers had to <strong>deal directly with hardware</strong></p>
<ul>
<li>Real computer <strong>hardware is urgly</strong></li>
<li>Hardware is <strong>extremely difficult</strong> to program</li>
</ul>
</li>
<li><p>An operating system is a layer[层] of indirection[间接] on top of the hardware:</p>
<ul>
<li>It provide <strong>abstractions</strong> for application programs</li>
<li>it provide a <strong>cleaner and easier interface to the hardware</strong></li>
</ul>
</li>
</ul>
<a id="more"></a>
<h3 id="Multi-programming"><a href="#Multi-programming" class="headerlink" title="Multi-programming"></a>Multi-programming</h3><ul>
<li>Morden OS use <strong>multi-programming</strong> to <strong>improve user experience</strong> and <strong>maximise the use of resource</strong><ul>
<li>Disk is <strong>slow</strong>. CPU is <strong>faster</strong> than disk. </li>
<li>Without multi-programming, CPU time is <strong>waste</strong> while wating for I/O requests.</li>
<li><strong>Multi-programming</strong> has important <strong>consequences[结果]</strong> for operating system design </li>
</ul>
</li>
<li>The operating system must allocate[分配]/share resources (CPU, memory, I/O devices) <strong>fairly</strong> and <strong>safely</strong> between competing processes:<ul>
<li>In time, e.g. CPUs and printers </li>
<li>In space, e.g., memory and disks</li>
</ul>
</li>
<li>The execution of <strong>multiple programs</strong> (processes) needs to be interleaved[交错] with one another. This requires:<ul>
<li>This requires <strong>context switches</strong> and <strong>process scheduling</strong> ⇒ <strong>mutual exclusion[相互排斥]</strong>, <strong>deadlock avoidance</strong>, <strong>protection</strong>, . . .</li>
</ul>
</li>
</ul>
<h3 id="Kernel-user-mode"><a href="#Kernel-user-mode" class="headerlink" title="Kernel-user mode"></a>Kernel-user mode</h3><ul>
<li>Modern operating systems have multiple <strong>modes</strong>:<ul>
<li>The operating system runs in <strong>kernel mode</strong> and has access to <strong>all instructions</strong></li>
<li>Applications run in <strong>user mode</strong> and have access to <strong>a subset of instructions</strong></li>
</ul>
</li>
<li>Transitions from user mode to kernel mode happen in a controlled manner (<strong>interrupts, exceptions, system calls</strong>) and are <strong>mirrored in hardware</strong></li>
</ul>
<h2 id="Summary"><a href="#Summary" class="headerlink" title="Summary"></a>Summary</h2><ul>
<li>Some properties:<ul>
<li>Sits <strong>directly</strong> on <strong>top</strong> of the hardware</li>
<li>Has access to the <strong>full capabilities of the hardware</strong></li>
<li>Provides <strong>abstractions</strong> for the user/programmer</li>
<li>Makes sure that everything is <strong>organised</strong> and runs in <strong>order</strong>     </li>
<li>Improve the <strong>hardware interface</strong></li>
</ul>
</li>
</ul>
]]></content>
    
    <summary type="html">
    
      &lt;h1 id=&quot;Introduction-1&quot;&gt;&lt;a href=&quot;#Introduction-1&quot; class=&quot;headerlink&quot; title=&quot;Introduction 1&quot;&gt;&lt;/a&gt;Introduction 1&lt;/h1&gt;&lt;hr&gt;
&lt;h2 id=&quot;Overview&quot;&gt;&lt;a href=&quot;#Overview&quot; class=&quot;headerlink&quot; title=&quot;Overview&quot;&gt;&lt;/a&gt;Overview&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;“&lt;strong&gt;Defining&lt;/strong&gt;“ operating systems&lt;/li&gt;
&lt;li&gt;What is &lt;strong&gt;multi-programming&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Kernel-user mode&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;Defining-Operating-Systems&quot;&gt;&lt;a href=&quot;#Defining-Operating-Systems&quot; class=&quot;headerlink&quot; title=&quot;Defining Operating Systems&quot;&gt;&lt;/a&gt;Defining Operating Systems&lt;/h3&gt;&lt;ul&gt;
&lt;li&gt;&lt;p&gt;In the early days, programmers had to &lt;strong&gt;deal directly with hardware&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Real computer &lt;strong&gt;hardware is urgly&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Hardware is &lt;strong&gt;extremely difficult&lt;/strong&gt; to program&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;An operating system is a layer[层] of indirection[间接] on top of the hardware:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;It provide &lt;strong&gt;abstractions&lt;/strong&gt; for application programs&lt;/li&gt;
&lt;li&gt;it provide a &lt;strong&gt;cleaner and easier interface to the hardware&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
    
    </summary>
    
      <category term="笔记" scheme="http://www.barondu.com/categories/%E7%AC%94%E8%AE%B0/"/>
    
    
      <category term="OSC" scheme="http://www.barondu.com/tags/OSC/"/>
    
      <category term="Introduction" scheme="http://www.barondu.com/tags/Introduction/"/>
    
  </entry>
  
</feed>
