From: Ramakrishnan Muthukrishnan Date: Sun, 15 May 2011 17:33:41 +0000 (+0530) Subject: Added a bunch of solutions to the exercise problems in the concurrency X-Git-Url: https://git.rkrishnan.org/pf/content/en//%22%22.?a=commitdiff_plain;h=d83b2606c047966977fe28edceed33ef2d2a6a7e;p=sicp.git Added a bunch of solutions to the exercise problems in the concurrency section. --- diff --git a/src/sicp/ex3_39.rkt b/src/sicp/ex3_39.rkt new file mode 100644 index 0000000..47ee525 --- /dev/null +++ b/src/sicp/ex3_39.rkt @@ -0,0 +1,11 @@ +#lang racket + +#| + +Only the below three cases will remain: + +101: P1 sets x to 100 and then P2 increments x to 101. +121: P2 increments x to 11 and then P1 sets x to x times x (121). +100: P1 accesses x (twice), then P2 sets x to 11, then P1 sets x. + +|# \ No newline at end of file diff --git a/src/sicp/ex3_40.rkt b/src/sicp/ex3_40.rkt new file mode 100644 index 0000000..9329db8 --- /dev/null +++ b/src/sicp/ex3_40.rkt @@ -0,0 +1,41 @@ +#lang racket + +#| + +(define x 10) + +(parallel-execute (lambda () (set! x (* x x))) + (lambda () (set! x (* x x x)))) + +list all possible values of x. + +|# + +#| + +1. 100 +2. 1000 +3. (expt 100 3) = 1000000 +4. (expt (expt 10 3) 2) = 1000000 +5. (* 10 (* 10 10 10)) = 10000 +6. (* 10 (* 10 10) (* 10 10)) = 100000 +7. (* 10 10 (* 10 10)) = 10000 + +|# + +#| + +Which of these possibilities remain if we instead use serialized procedures: + +(define x 10) + +(define s (make-serializer)) + +(parallel-execute (s (lambda () (set! x (* x x)))) + (s (lambda () (set! x (* x x x))))) + +ans: + +=> 1000000 + +|# \ No newline at end of file diff --git a/src/sicp/ex3_41.rkt b/src/sicp/ex3_41.rkt new file mode 100644 index 0000000..a20f95e --- /dev/null +++ b/src/sicp/ex3_41.rkt @@ -0,0 +1,11 @@ +#lang racket + +#| + +I don't agree. 'balance' is only read and for a particular account, it is +not usually read while another transaction is in progress. Further, even if +it is read concurrently with a transaction (deposit or withdrawal), one will +get either the value before the transaction or the one after. Both are +acceptable. + +|# \ No newline at end of file diff --git a/src/sicp/ex3_42.rkt b/src/sicp/ex3_42.rkt new file mode 100644 index 0000000..88ba9f5 --- /dev/null +++ b/src/sicp/ex3_42.rkt @@ -0,0 +1,7 @@ +#lang racket + +#| + +Yes, it is safe. + +|# \ No newline at end of file