灯下 登录
计算机科学 / SICP / 3.4.2 Mechanisms for Controlling Concurrency

Exercise 3.43 · 习题

Exercise 3.43: Suppose that the balances in

three accounts start out as 10,10,20, and $30, and that multiple processes run,

exchanging the balances in the accounts. Argue that if the processes are run

sequentially, after any number of concurrent exchanges, the account balances

should be 10,10,20, and $30 in some order. Draw a timing diagram like the one

in Figure 3.29 to show how this condition can be violated if the

exchanges are implemented using the first version of the account-exchange

program in this section. On the other hand, argue that even with this

exchange program, the sum of the balances in the accounts will be

preserved. Draw a timing diagram to show how even this condition would be

violated if we did not serialize the transactions on individual accounts.

练习 3.43:假设三个账户的余额初始为 1010、20 和 30,多个进程并发运行,相互交换这些账户的余额。请论证:若这些进程顺序执行,则在任意次并发交换之后,账户余额仍应以某种顺序为30,多个进程并发运行,相互交换这些账户的余额。请论证:若这些进程顺序执行,则在任意次并发交换之后,账户余额仍应以某种顺序为10、2020 和30。画出类似图 3.29 的时序图,说明若用本节第一版账户交换程序实现交换操作,上述条件是如何被违反的。另一方面,请论证:即使使用这个交换程序,各账户余额之和仍将被保持。再画一张时序图,说明若我们不对单个账户的事务加以串行化,则连这一条件也会被违反。