<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Nisei Kimura&#039;s Blog &#187; Unclassified</title>
	<atom:link href="http://science.kinokoru.jp/category/unclassified/feed/" rel="self" type="application/rss+xml" />
	<link>http://science.kinokoru.jp</link>
	<description></description>
	<lastBuildDate>Mon, 14 Jul 2014 10:24:03 +0000</lastBuildDate>
	<language>en</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=3.9.40</generator>
	<item>
		<title>[SICP Excercise 3.47] Some observation about semaphore</title>
		<link>http://science.kinokoru.jp/sicp-excercise-3-47-observation-semaphore/</link>
		<comments>http://science.kinokoru.jp/sicp-excercise-3-47-observation-semaphore/#comments</comments>
		<pubDate>Mon, 14 Jul 2014 10:16:04 +0000</pubDate>
		<dc:creator><![CDATA[Nisei]]></dc:creator>
				<category><![CDATA[Unclassified]]></category>

		<guid isPermaLink="false">http://science.kinokoru.jp/?p=78</guid>
		<description><![CDATA[<p>(define (make-semaphore n) (let ((the-mutex (make-mutex)) (count 0)) (define (the-semaphore m) (cond ((eq? m 'acquire) (the-mutex 'acquire) (if (= count n) (begin (the-mutex 'release) (the-semaphore 'acquire)) (begin (set! count (+ count 1)) (the-mutex 'release)))) ((eq? m 'release) (the-mutex 'acquire) (if (> count 0) (set! count (- count 1))) (the-mutex 'release)))) the-semaphore)) (I&#8217;ve Borrowed code above [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://science.kinokoru.jp/sicp-excercise-3-47-observation-semaphore/">[SICP Excercise 3.47] Some observation about semaphore</a> appeared first on <a rel="nofollow" href="http://science.kinokoru.jp">Nisei Kimura&#039;s Blog</a>.</p>
]]></description>
				<content:encoded><![CDATA[<pre>
(define (make-semaphore n)
  (let ((the-mutex (make-mutex))
        (count 0))
    (define (the-semaphore m)
      (cond ((eq? m 'acquire)
             (the-mutex 'acquire)
             (if (= count n)
                 (begin
                   (the-mutex 'release)
                   (the-semaphore 'acquire))
                 (begin
                   (set! count (+ count 1))
                   (the-mutex 'release))))
            ((eq? m 'release)
             (the-mutex 'acquire)
             (if (> count 0)
                 (set! count (- count 1)))
             (the-mutex 'release))))
    the-semaphore))
</pre>
<p>(I&#8217;ve Borrowed code above from <a href="http://wqzhang.wordpress.com/2009/08/04/sicp-exercise-3-47/" target="_blank">Weiqun Zhang&#8217;s Blog &#8211; SICP Exercise 3.47</a>.)</p>
<p>It took some time to understand the structure of semaphore. Here is the problem: </p>
<h2>The place of &#8216;acquire&#8217;</h2>
<p>Next question that bothered me was that: does the code still work if I replace acquire method as follows?</p>
<pre>
(define (make-semaphore n)
  (let ((the-mutex (make-mutex))
        (count 0))
    (define (the-semaphore m)
      (cond ((eq? m 'acquire)
             (if (= count n)
                 (the-semaphore 'acquire))
                 (begin
                   (the-mutex 'acquire) ; Added acquire  method here instead
                   (set! count (+ count 1))
                   (the-mutex 'release))))
            ((eq? m 'release)
             (the-mutex 'acquire)
             (if (> count 0)
                 (set! count (- count 1)))
             (the-mutex 'release))))
    the-semaphore))
</pre>
<p>After a little bit observation I noticed that this code  has a problem:</p>
<p><a href="http://science.kinokoru.jp/wp-content/uploads/2014/07/not_correct_process.png"><img src="http://science.kinokoru.jp/wp-content/uploads/2014/07/not_correct_process.png" alt="not_correct_process" width="400" height="685" class="alignnone size-full wp-image-79" /></a></p>
<p>So the place of &#8216;acquire&#8217; should be before (if (= count n) &#8230; :</p>
<p><a href="http://science.kinokoru.jp/wp-content/uploads/2014/07/correct_process.png"><img src="http://science.kinokoru.jp/wp-content/uploads/2014/07/correct_process.png" alt="correct_process" width="402" height="628" class="alignnone size-full wp-image-80" /></a></p>
<p>The post <a rel="nofollow" href="http://science.kinokoru.jp/sicp-excercise-3-47-observation-semaphore/">[SICP Excercise 3.47] Some observation about semaphore</a> appeared first on <a rel="nofollow" href="http://science.kinokoru.jp">Nisei Kimura&#039;s Blog</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://science.kinokoru.jp/sicp-excercise-3-47-observation-semaphore/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
