20 hours ago
my attempt to do the exercises in sicp.
Wednesday, March 24, 2010
sicp exercise 3.48
;; Exercise 3.48. Explain in detail why the deadlock-avoidance method described above, (i.e., the accounts are numbered, and each process attempts to acquire the smaller-numbered account first) avoids deadlock in the exchange problem. Rewrite serialized-exchange to incorporate this idea. (You will also need to modify make-account so that each account is created with a number, which can be accessed by sending an appropriate message.)
;; if both processes acquire the mutex in the same order, deadlock can be avoided. By checking the account number and locking the least numbered account maintain the same order.
(define (serialized-exchange account1 account2)
(let ((serializer1 (account1 'serializer))
(serializer2 (account2 'serializer)))
(if (< (account1 'getid) (account2 'getid))
((serializer1 (serializer2 exchange)) account1 account2)
((serializer2 (serializer1 exchange)) account1 account2))))