計算機プログラムの構造と解釈[第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.48.を解いてみる。
その他参考書籍
- Instructor's Manual to Accompany Structure & Interpretation of Computer Programs
- プログラミングGauche (Kahuaプロジェクト (著), 川合 史朗 (監修), オライリージャパン)
問題 3.48.
口座に番号を付け、プロセスはより小さい番号の方の口座を先に獲得しようとすることが、交換問題でデッドロックを回避する理由。
- Paulがa2をa1と交換しようとしているときに、Peterがa1とa2を交換しようとする。
- Peterのプロセスが直列変換器に入り、a1を保護する。
- 直後にPaulのプロセスがa1にアクセスしようとするが、a1はPeterのプロセスにより保護されているのでアクセス出来ない。
- Peterのプロセスがa2を保護する。
- Peterのプロセスがa1とa2の口座を交換する。
- a1、a2が解放される。
- Paulのプロセスがa1を保護する。
- Paulのプロセスがa2を保護する。
- Paulのプロセスが口座の交換をする。
- a1、a2が解放される。
コード(BBEdit, Emacs)
sample48.scm
#!/usr/bin/env gosh
;; -*- coding: utf-8 -*-
(define (make-account balance id)
(define (withdraw amount)
(if (>= balance amount)
(begin (set! balance (- balance amount))
balance)
"Insufficient funds"))
(define (deposit amount)
(set! balance (+ balance amount))
balance)
(let ((balance-serializer (make-serializer)))
(define (dispath m)
(cond ((eq? m 'withdraw) withdraw)
((eq? m 'deposit) deposit)
((eq? m 'balance) balance)
((eq? m 'serializer) balance-serializer)
((eq? m 'id) id)
(else (error "Unknown request -- MAKE-ACCOUNT" m))))
dispath))
(define (serialized-exchange account1 account2)
(let ((serializer1 (account1 'serializer))
(serializer2 (account2 'serializer))
(id1 (account1 'id))
(id2 (account2 'id)))
(if (< id1 id2)
((serializer2 (serializer1 exchange)) account1 account2)
((serializer1 (serializer2 exchange)) account1 account2))))
sample3_38.scm
入出力結果(Terminal(gosh), REPL(Read, Eval, Print, Loop))
$ ./sample48.scm $
0 コメント:
コメントを投稿