Serialized-exchange with the deadlock-avoidance method

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.)

There are no comments yet.

Authentication required

You must log in to post a comment.

(define (make-serializer)
  (let ((mutex (make-mutex)))
    (lambda (p)
      (define (serialized-p . args)
        (mutex 'acquire)
        (let ((val (apply p args)))
          (mutex 'release)

(define (make-mutex)
  (let ((cell (list false)))            
    (define (the-mutex m)
      (cond ((eq? m 'acquire)
             (if (test-and-set! cell)
                 (the-mutex 'acquire))) ; retry
            ((eq? m 'release) (clear! cell))))
(define (clear! cell)
  (set-car! cell false))

(define (test-and-set! cell)
  (if (car cell)
      (begin (set-car! cell true)

(define (exchange account1 account2)
  (let ((difference (- (account1 'balance)
                       (account2 'balance))))
    ((account1 'withdraw) difference)
    ((account2 'deposit) difference)))

(define peter-acc (make-account 100 1))

(check-equal? (peter-acc 'number) 1)
(check-equal? (peter-acc 'balance) 100)
(check-equal? ((peter-acc 'withdraw) 20) 80)
(check-equal? ((peter-acc 'deposit) 70) 150)

(define paul-acc (make-account 50 2))

(serialized-exchange peter-acc paul-acc)
(check-equal? (peter-acc 'balance) 50)
(check-equal? (paul-acc 'balance) 150)