4 days ago
my attempt to do the exercises in sicp.
Wednesday, March 17, 2010
sicp exercise 3.38
;; Exercise 3.38. Suppose that Peter, Paul, and Mary share a joint bank account that initially contains $100. Concurrently, Peter deposits $10, Paul withdraws $20, and Mary withdraws half the money in the account, by executing the following commands:
;; Peter: (set! balance (+ balance 10))
;; Paul: (set! balance (- balance 20))
;; Mary: (set! balance (- balance (/ balance 2)))
;; a. List all the different possible values for balance after these three transactions have been completed, assuming that the banking system forces the three processes to run sequentially in some order.
;; b. What are some other values that could be produced if the system allows the processes to be interleaved? Draw timing diagrams like the one in figure 3.29 to explain how these values can occur.
;; Ans: a.
;; Peter->Paul->Mary 45
;; Peter->Mary->Paul 35
;; Paul->Peter->Mary 45
;; Paul->Mary->Peter 50
;; Mary->Peter->Paul 40
;; Mary->Paul->Peter 40
;; If interleving is allowed, many possibilities exist because one command can change the value read by other commands in between second command's operation.
;; e.g. the 'balance' can be written by Paul in between two reads by Mary.
;; skipping the timing diagrams