Finish 3.49

This commit is contained in:
2020-12-31 16:38:21 -05:00
parent 3762a87235
commit 9129b66959
2 changed files with 25 additions and 7 deletions

View File

@@ -124,9 +124,9 @@
(s 'release) (s 'release)
(assert (s 'remaining) 3) (assert (s 'remaining) 3)
(display "\nex-3.48\n") (display "\nex-3.48 - exchange without deadlocks\n")
(define (make-account-and-serializer balance) (define (make-account-and-serializer balance number)
(define (withdraw amount) (define (withdraw amount)
(if (>= balance amount) (if (>= balance amount)
(begin (set! balance (- balance amount)) (begin (set! balance (- balance amount))
@@ -141,6 +141,7 @@
((eq? m 'deposit) deposit) ((eq? m 'deposit) deposit)
((eq? m 'balance) balance) ((eq? m 'balance) balance)
((eq? m 'serializer) balance-serializer) ((eq? m 'serializer) balance-serializer)
((eq? m 'number) number)
(else (error "Unknown request -- MAKE-ACCOUNT" (else (error "Unknown request -- MAKE-ACCOUNT"
m)))) m))))
dispatch)) dispatch))
@@ -159,12 +160,24 @@
(define (serialized-exchange account1 account2) (define (serialized-exchange account1 account2)
(let ((serializer1 (account1 'serializer)) (let ((serializer1 (account1 'serializer))
(serializer2 (account2 'serializer))) (serializer2 (account2 'serializer)))
((serializer1 (serializer2 exchange)) (if (< (account1 'number) (account2 'number))
account1 ((serializer1 (serializer2 exchange)) account1 account2)
account2))) ((serializer2 (serializer1 exchange)) account1 account2))))
(define a (make-account-and-serializer 100)) (define a (make-account-and-serializer 100 1))
(define b (make-account-and-serializer 40)) (define b (make-account-and-serializer 40 2))
(serialized-exchange a b) (serialized-exchange a b)
(assert (a 'balance) 40) (assert (a 'balance) 40)
; Assuming number(a) = 1 and number(b) = 2 the new procedure forces the lock
; for a to be taken before the one for b. This means the process that gets a
; first is able to complete because the second process cannot lock b.
(display "\nex-3.49\n")
(display "[answered]")
; If we have a procedure that takes and account and then executes exchanges for
; a list of other accounts related to that account. In a situation where two
; accounts reference each other we might again run into a deadlock. That is a bad
; example, but I cannot think of a better one right now.

5
ex-3_50-xx.scm Normal file
View File

@@ -0,0 +1,5 @@
(load "util.scm")
(display "\nex-3.50\n")