diff --git a/ex-3_46-xx.scm b/ex-3_46-49.scm similarity index 81% rename from ex-3_46-xx.scm rename to ex-3_46-49.scm index c3b88b0..2b0ea3a 100644 --- a/ex-3_46-xx.scm +++ b/ex-3_46-49.scm @@ -124,9 +124,9 @@ (s 'release) (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) (if (>= balance amount) (begin (set! balance (- balance amount)) @@ -141,6 +141,7 @@ ((eq? m 'deposit) deposit) ((eq? m 'balance) balance) ((eq? m 'serializer) balance-serializer) + ((eq? m 'number) number) (else (error "Unknown request -- MAKE-ACCOUNT" m)))) dispatch)) @@ -159,12 +160,24 @@ (define (serialized-exchange account1 account2) (let ((serializer1 (account1 'serializer)) (serializer2 (account2 'serializer))) - ((serializer1 (serializer2 exchange)) - account1 - account2))) + (if (< (account1 'number) (account2 'number)) + ((serializer1 (serializer2 exchange)) account1 account2) + ((serializer2 (serializer1 exchange)) account1 account2)))) -(define a (make-account-and-serializer 100)) -(define b (make-account-and-serializer 40)) +(define a (make-account-and-serializer 100 1)) +(define b (make-account-and-serializer 40 2)) (serialized-exchange a b) (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. + diff --git a/ex-3_50-xx.scm b/ex-3_50-xx.scm new file mode 100644 index 0000000..5db46ba --- /dev/null +++ b/ex-3_50-xx.scm @@ -0,0 +1,5 @@ +(load "util.scm") + +(display "\nex-3.50\n") + +