File: chap8.html

package info (click to toggle)
gap 4.15.1-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 110,212 kB
  • sloc: ansic: 97,261; xml: 48,343; cpp: 13,946; sh: 4,900; perl: 1,650; javascript: 255; makefile: 252; ruby: 9
file content (107 lines) | stat: -rw-r--r-- 6,890 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>GAP (hpc) - Chapter 8: Semaphores</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta name="generator" content="GAPDoc2HTML" />
<link rel="stylesheet" type="text/css" href="manual.css" />
<script src="manual.js" type="text/javascript"></script>
<script type="text/javascript">overwriteStyle();</script>
</head>
<body class="chap8"  onload="jscontent()">


<div class="chlinktop"><span class="chlink1">Goto Chapter: </span><a href="chap0.html">Top</a>  <a href="chap1.html">1</a>  <a href="chap2.html">2</a>  <a href="chap3.html">3</a>  <a href="chap4.html">4</a>  <a href="chap5.html">5</a>  <a href="chap6.html">6</a>  <a href="chap7.html">7</a>  <a href="chap8.html">8</a>  <a href="chap9.html">9</a>  <a href="chap10.html">10</a>  <a href="chap11.html">11</a>  <a href="chapInd.html">Ind</a>  </div>

<div class="chlinkprevnexttop">&nbsp;<a href="chap0.html">[Top of Book]</a>&nbsp;  <a href="chap0.html#contents">[Contents]</a>&nbsp;  &nbsp;<a href="chap7.html">[Previous Chapter]</a>&nbsp;  &nbsp;<a href="chap9.html">[Next Chapter]</a>&nbsp;  </div>

<p id="mathjaxlink" class="pcenter"><a href="chap8_mj.html">[MathJax on]</a></p>
<p><a id="X83F63F0F7827767E" name="X83F63F0F7827767E"></a></p>
<div class="ChapSects"><a href="chap8.html#X83F63F0F7827767E">8 <span class="Heading">Semaphores</span></a>
<div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap8.html#X83F63F0F7827767E">8.1 <span class="Heading">Semaphores</span></a>
</span>
<div class="ContSSBlock">
<span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap8.html#X7AC5500483465AE3">8.1-1 CreateSemaphore</a></span>
<span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap8.html#X834E47327A3FC5A2">8.1-2 WaitSemaphore</a></span>
<span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap8.html#X7F0C1F1F8540CF8C">8.1-3 SignalSemaphore</a></span>
<span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap8.html#X83D868417F0069F0">8.1-4 <span class="Heading">Simulating locks</span></a>
</span>
</div></div>
</div>

<h3>8 <span class="Heading">Semaphores</span></h3>

<p><a id="X83F63F0F7827767E" name="X83F63F0F7827767E"></a></p>

<h4>8.1 <span class="Heading">Semaphores</span></h4>

<p>Semaphores are synchronized counters; they can also be used to simulate locks.</p>

<p><a id="X7AC5500483465AE3" name="X7AC5500483465AE3"></a></p>

<h5>8.1-1 CreateSemaphore</h5>

<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; CreateSemaphore</code>( [<var class="Arg">value</var>] )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
<p>The function <code class="func">CreateSemaphore</code> takes an optional argument, which defaults to zero. It is the counter with which the semaphore is initialized.</p>


<div class="example"><pre>
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">sem := CreateSemaphore(1);</span>
&lt;semaphore 0x1108e81c0: count = 1&gt;
</pre></div>

<p><a id="X834E47327A3FC5A2" name="X834E47327A3FC5A2"></a></p>

<h5>8.1-2 WaitSemaphore</h5>

<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; WaitSemaphore</code>( <var class="Arg">sem</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
<p><code class="func">WaitSemaphore</code> receives a previously created semaphore as its argument. If the semaphore's counter is greater than zero, it decrements the counter and returns; if the counter is zero, it waits until another thread increases it via <code class="func">SignalSemaphore</code> (<a href="chap8.html#X7F0C1F1F8540CF8C"><span class="RefLink">8.1-3</span></a>), then decrements the counter and returns.</p>


<div class="example"><pre>
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">sem := CreateSemaphore(1);</span>
&lt;semaphore 0x1108e81c0: count = 1&gt;
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">WaitSemaphore(sem);</span>
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">sem;</span>
&lt;semaphore 0x1108e81c0: count = 0&gt;
</pre></div>

<p><a id="X7F0C1F1F8540CF8C" name="X7F0C1F1F8540CF8C"></a></p>

<h5>8.1-3 SignalSemaphore</h5>

<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; SignalSemaphore</code>( <var class="Arg">sem</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
<p><code class="func">SignalSemaphore</code> receives a previously created semaphore as its argument. It increments the semaphore's counter and returns.</p>


<div class="example"><pre>
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">sem := CreateSemaphore(1);</span>
&lt;semaphore 0x1108e81c0: count = 1&gt;
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">WaitSemaphore(sem);</span>
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">sem;</span>
&lt;semaphore 0x1108e81c0: count = 0&gt;
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">SignalSemaphore(sem);</span>
<span class="GAPprompt">gap&gt;</span> <span class="GAPinput">sem;</span>
&lt;semaphore 0x1108e81c0: count = 1&gt;
</pre></div>

<p><a id="X83D868417F0069F0" name="X83D868417F0069F0"></a></p>

<h5>8.1-4 <span class="Heading">Simulating locks</span></h5>

<p>In order to use semaphores to simulate locks, create a semaphore with an initial value of 1. <code class="func">WaitSemaphore</code> (<a href="chap8.html#X834E47327A3FC5A2"><span class="RefLink">8.1-2</span></a>) is then equivalent to a lock operation, <code class="func">SignalSemaphore</code> (<a href="chap8.html#X7F0C1F1F8540CF8C"><span class="RefLink">8.1-3</span></a>) is equivalent to an unlock operation.</p>


<div class="chlinkprevnextbot">&nbsp;<a href="chap0.html">[Top of Book]</a>&nbsp;  <a href="chap0.html#contents">[Contents]</a>&nbsp;  &nbsp;<a href="chap7.html">[Previous Chapter]</a>&nbsp;  &nbsp;<a href="chap9.html">[Next Chapter]</a>&nbsp;  </div>


<div class="chlinkbot"><span class="chlink1">Goto Chapter: </span><a href="chap0.html">Top</a>  <a href="chap1.html">1</a>  <a href="chap2.html">2</a>  <a href="chap3.html">3</a>  <a href="chap4.html">4</a>  <a href="chap5.html">5</a>  <a href="chap6.html">6</a>  <a href="chap7.html">7</a>  <a href="chap8.html">8</a>  <a href="chap9.html">9</a>  <a href="chap10.html">10</a>  <a href="chap11.html">11</a>  <a href="chapInd.html">Ind</a>  </div>

<hr />
<p class="foot">generated by <a href="https://www.math.rwth-aachen.de/~Frank.Luebeck/GAPDoc">GAPDoc2HTML</a></p>
</body>
</html>