Sophie

Sophie

distrib > CentOS > 6 > i386 > by-pkgid > 2c51d8eb79f8810ada971ee8c30ce1e5 > files > 2194

kernel-doc-2.6.32-71.14.1.el6.noarch.rpm

<?xml version="1.0" encoding="ANSI_X3.4-1968" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=ANSI_X3.4-1968" /><title>Chapter&#160;2.&#160;The Problem With Concurrency</title><meta name="generator" content="DocBook XSL Stylesheets V1.75.2" /><link rel="home" href="index.html" title="Unreliable Guide To Locking" /><link rel="up" href="index.html" title="Unreliable Guide To Locking" /><link rel="prev" href="ch01.html" title="Chapter&#160;1.&#160;Introduction" /><link rel="next" href="ch03.html" title="Chapter&#160;3.&#160;Locking in the Linux Kernel" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter&#160;2.&#160;The Problem With Concurrency</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch01.html">Prev</a>&#160;</td><th width="60%" align="center">&#160;</th><td width="20%" align="right">&#160;<a accesskey="n" href="ch03.html">Next</a></td></tr></table><hr /></div><div class="chapter" title="Chapter&#160;2.&#160;The Problem With Concurrency"><div class="titlepage"><div><div><h2 class="title"><a id="races"></a>Chapter&#160;2.&#160;The Problem With Concurrency</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="sect1"><a href="ch02.html#race-condition">Race Conditions and Critical Regions</a></span></dt></dl></div><p>
      (Skip this if you know what a Race Condition is).
    </p><p>
      In a normal program, you can increment a counter like so:
    </p><pre class="programlisting">
      very_important_count++;
    </pre><p>
      This is what they would expect to happen:
    </p><div class="table"><a id="id3041009"></a><p class="title"><b>Table&#160;2.1.&#160;Expected Results</b></p><div class="table-contents"><table summary="Expected Results" border="1"><colgroup><col /><col /></colgroup><thead><tr><th align="left">Instance 1</th><th align="left">Instance 2</th></tr></thead><tbody><tr><td align="left">read very_important_count (5)</td><td align="left">&#160;</td></tr><tr><td align="left">add 1 (6)</td><td align="left">&#160;</td></tr><tr><td align="left">write very_important_count (6)</td><td align="left">&#160;</td></tr><tr><td align="left">&#160;</td><td align="left">read very_important_count (6)</td></tr><tr><td align="left">&#160;</td><td align="left">add 1 (7)</td></tr><tr><td align="left">&#160;</td><td align="left">write very_important_count (7)</td></tr></tbody></table></div></div><br class="table-break" /><p>
     This is what might happen:
    </p><div class="table"><a id="id3001906"></a><p class="title"><b>Table&#160;2.2.&#160;Possible Results</b></p><div class="table-contents"><table summary="Possible Results" border="1"><colgroup><col /><col /></colgroup><thead><tr><th align="left">Instance 1</th><th align="left">Instance 2</th></tr></thead><tbody><tr><td align="left">read very_important_count (5)</td><td align="left">&#160;</td></tr><tr><td align="left">&#160;</td><td align="left">read very_important_count (5)</td></tr><tr><td align="left">add 1 (6)</td><td align="left">&#160;</td></tr><tr><td align="left">&#160;</td><td align="left">add 1 (6)</td></tr><tr><td align="left">write very_important_count (6)</td><td align="left">&#160;</td></tr><tr><td align="left">&#160;</td><td align="left">write very_important_count (6)</td></tr></tbody></table></div></div><br class="table-break" /><div class="sect1" title="Race Conditions and Critical Regions"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="race-condition"></a>Race Conditions and Critical Regions</h2></div></div></div><p>
      This overlap, where the result depends on the
      relative timing of multiple tasks, is called a <em class="firstterm">race condition</em>.
      The piece of code containing the concurrency issue is called a
      <em class="firstterm">critical region</em>.  And especially since Linux starting running
      on SMP machines, they became one of the major issues in kernel
      design and implementation.
    </p><p>
      Preemption can have the same effect, even if there is only one
      CPU: by preempting one task during the critical region, we have
      exactly the same race condition.  In this case the thread which
      preempts might run the critical region itself.
    </p><p>
      The solution is to recognize when these simultaneous accesses
      occur, and use locks to make sure that only one instance can
      enter the critical region at any time.  There are many
      friendly primitives in the Linux kernel to help you do this.
      And then there are the unfriendly primitives, but I'll pretend
      they don't exist.
    </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch01.html">Prev</a>&#160;</td><td width="20%" align="center">&#160;</td><td width="40%" align="right">&#160;<a accesskey="n" href="ch03.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter&#160;1.&#160;Introduction&#160;</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">&#160;Chapter&#160;3.&#160;Locking in the Linux Kernel</td></tr></table></div></body></html>