【SICP练习】114 练习3.38-3.39
来源:互联网 发布:钢梁挠度计算软件 编辑:程序博客网 时间:2024/05/22 06:11
练习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.
分析
a小题中题目假定银行系统强迫着三个进程按照某种顺序方式进行。将3个人(或者说3个进程)全排序有A(3,3)=3X2X1=6种方式。具体为:
Peter (110) -> Paul (90) -> Mary (45)
Peter (110) -> Mary (55) -> Paul (35)
Paul (80) -> Peter (90) -> Mary (45)
Paul (80) -> Mary (40) -> Peter (50)
Mary (50) -> Peter (60) -> Paul (40)
Mary (50) -> Paul (30) -> Peter (40)
b小题参照书中第209页即可,由于编辑较困难在此就不予列出了。
练习3-39
原文
Exercise 3.39. Which of the five possibilities in the parallel execution shown above remain if we instead serialize execution as follows:
(define x 10) (define s (make-serializer)) (parallel-execute (lambda () (set! x ((s (lambda () (* x x)))))) (s (lambda () (set! x (+ x 1)))))
分析
做这道题之前必须理解书中的示例。另外加入了make-serializer进行串行化后,P1和P2(表示传入parallel-execute的无参过程)的执行不回交错进行。题目中的以下两行代码均为串行化操作。
((s (lambda () (* x x))))((s (lambda () (set! x (+ x 1)))))
如果将这两段代码用LS1和LS2来代替,则题中的代码简化为:
(parallel-execute (lambda () (set! x LS1)) LS2)
因此会有2种可能的执行顺序:
LS2 - > (set! x LS1)
(set! x LS1) - > LS2
相应的执行结果如下:
1) LS2 - > (set! x (+ x 1)) - > x = 11
(set! x (* x x)) - > x = 121
2) (set! x LS1) - > (set! x (* x x)) - > x = 100
(set! x (+ x 1)) - > x = 101
但还有一种可能,当执行(set! x LS1)时,由于要先执行LS1,而不可能这个操作并未执行完LS2并已经开始了,最后又折回来执行(set! x LS1)。执行顺序为:
LS1 - > (set! x LS1) - > LS2 - > (set! x LS1)
不过在第一个(set! x LS1)时,该步骤并未彻底执行完。相应的执行结果为121。
感谢访问,希望对您有所帮助。 欢迎关注或收藏、评论或点赞。
为使本文得到斧正和提问,转载请注明出处:
http://blog.csdn.net/nomasp
- 【SICP练习】114 练习3.38-3.39
- 【SICP练习】2 练习1.6
- 【SICP练习】3 练习1.7
- 【SICP练习】4 练习1.8
- 【SICP练习】5 练习1.9
- 【SICP练习】6 练习1.10
- 【SICP练习】7 练习1.11
- 【SICP练习】8 练习1.12
- 【SICP练习】9 练习1.15
- 【SICP练习】10 练习1.16
- 【SICP练习】11 练习1.17
- 【SICP练习】12 练习1.18
- 【SICP练习】13 练习1.19
- 【SICP练习】14 练习1.20
- 【SICP练习】15 练习1.21
- 【SICP练习】16 练习1.22
- 【SICP练习】17 练习1.23
- 【SICP练习】18 练习1.24
- 测试驱动开发与软件重构
- 玩转百度即用API(4)——手机号码归属地查询
- 【第三篇章-android平台buffer播放源码】核心展示
- web登入界面中,「登录」和「登陆」哪个正确?
- MapReduce的两表join一般操作
- 【SICP练习】114 练习3.38-3.39
- Android SDK的默认目录导致的AVD启动时各种找不到
- 程序员修炼要点
- Fedora 20 yum利用缓存离线安装软件及其依赖
- labview通过编程自定义控件运行时快捷菜单
- Android 的Handle之postDelayed方法
- MASM32+Visual studio 2010写汇编程序入门(图文)
- coreData +entityForName: could not locate an entity named '*****' in ****
- Redis Sentinel初体验