計算機プログラムの構造と解釈[第2版]
(翔泳社)
ハロルド エイブルソン (著) ジュリー サスマン (著)
ジェラルド・ジェイ サスマン (著)
Harold Abelson (原著) Julie Sussman (原著)
Gerald Jay Sussman (原著) 和田 英一 (翻訳)
開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Scheme (プログラミング言語)
- Gauche (処理系)
計算機プログラムの構造と解釈[第2版](ハロルド エイブルソン (著)、ジュリー サスマン (著)、ジェラルド・ジェイ サスマン (著)、Harold Abelson (原著)、Julie Sussman (原著)、Gerald Jay Sussman (原著)、和田 英一 (翻訳)、翔泳社、原書: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の3(標準部品化力、オブジェクトおよび状態)、3.4(並列性: 時が本質的)、3.4.2(並列性の制御機構)、直列変換器の実装、問題 3.47.を解いてみる。
その他参考書籍
- Instructor's Manual to Accompany Structure & Interpretation of Computer Programs
- プログラミングGauche (Kahuaプロジェクト (著), 川合 史朗 (監修), オライリージャパン)
問題 3.47.
コード(BBEdit, Emacs)
sample3_47.scm
;; -*- coding: utf-8 -*-
;; a 相互排除器を使った場合
(define (make-semaphore n)
(let ((mutex (make-mutex))
(count 0))
(define (the-semaphore m)
(cond ((eq? m 'acquire)
(mutex 'acquire)
(if (= count n)
(begin (mutex 'release)
(the-semaphore 'acquire))
(begin (set! count (+ count 1))
(mutex 'release))))
((eq? m 'release)
(mutex 'acquire)
(if (> count 0)
(set! count (- count 1)))
(mutex 'release))))
the-semaphore))
;; b test-and-set!を使った場合
(define (make-semaphore n)
(let ((cell (list #f))
(count 0))
(define (the-semaphore m)
(cond ((eq? m 'acquire)
(if (or (test-and-set! cell)
(= count n))
(the-semaphore 'acquire)
(begin (set! count (+ count 1))
(clear! cell))))
((eq? m 'release)
(set! count (- count 1))
(clear! cell))))
the-semaphore))
入出力結果(Terminal(gosh), REPL(Read, Eval, Print, Loop))
$ ./sample3_47.scm $
0 コメント:
コメントを投稿