|
| 1 | +#+LATEX_CLASS: ramsay-org-article |
| 2 | +#+LATEX_CLASS_OPTIONS: [oneside,A4paper,12pt] |
| 3 | +#+AUTHOR: Ramsay Leung |
| 4 | + |
| 5 | +#+DATE: 2025-07-20 Sun 16:06 |
| 6 | +* 问题a. |
| 7 | +存在 3! = 6 种可能性,Peter 和 Paul 操作顺序并没有关系,但是 Mary 的操作影响最大. |
| 8 | +** 1. Peter -> Paul -> Mary |
| 9 | + #+begin_src emacs-lisp :exports both |
| 10 | + (/(- (+ 100 10) 20) 2) |
| 11 | + #+end_src |
| 12 | + |
| 13 | + #+RESULTS: |
| 14 | + : 45 |
| 15 | +** 2. Peter -> Mary -> Paul |
| 16 | + #+begin_src emacs-lisp :exports both |
| 17 | + (-(/ (+ 100 10) 2) 20) |
| 18 | + #+end_src |
| 19 | + |
| 20 | + #+RESULTS: |
| 21 | + : 35 |
| 22 | +** 3. Paul -> Peter -> Mary |
| 23 | + #+begin_src emacs-lisp :exports both |
| 24 | + (/(+ (- 100 20) 10) 2) |
| 25 | + #+end_src |
| 26 | + |
| 27 | + #+RESULTS: |
| 28 | + : 45 |
| 29 | +** 4. Paul -> Mary -> Peter |
| 30 | + |
| 31 | + #+begin_src emacs-lisp :exports both |
| 32 | + (+(/ (- 100 20) 2) 10) |
| 33 | + #+end_src |
| 34 | + |
| 35 | + #+RESULTS: |
| 36 | + : 50 |
| 37 | +** 5. Mary -> Peter -> Paul |
| 38 | + #+begin_src emacs-lisp :exports both |
| 39 | + (-(+(/ 100 2)10)20) |
| 40 | + #+end_src |
| 41 | + |
| 42 | + #+RESULTS: |
| 43 | + : 40 |
| 44 | + |
| 45 | +** 6. Marry -> Pual -> Peter |
| 46 | + #+begin_src emacs-lisp :exports both |
| 47 | + (+(-(/ 100 2)20)20) |
| 48 | + #+end_src |
| 49 | + |
| 50 | + #+RESULTS: |
| 51 | + : 50 |
| 52 | + |
| 53 | +* 问题b. |
| 54 | + 情况可能更糟糕,考虑作为每个提款进程一部分的表达式: |
| 55 | + |
| 56 | + #+begin_src scheme |
| 57 | + (set! balance (- balance amount)) |
| 58 | + #+end_src |
| 59 | + |
| 60 | + 这一表达式的执行包含三个步骤: |
| 61 | + 1. 取得变量 =balance= 的值 |
| 62 | + 2. 计算出新的余额 |
| 63 | + 3. 将变量 =balance= 设置为新的值 |
| 64 | + |
| 65 | + 如果 Peter 和 Pual 以及 Mery 在提款过程中并发执行这一语句,那么三次操作账户在访问 =balance= 和将它设置为新值的动作就可能交错 |
| 66 | + |
| 67 | + 除去问题a 可能性外, 还可能是: |
| 68 | +** 余额110 |
| 69 | + #+begin_src plantuml :file ../img/chapter3/exercise3-38-1.png :exports results |
| 70 | + @startuml |
| 71 | + title Interleaved Transaction Example 1: Final Balance $110 |
| 72 | + |
| 73 | + participant Peter |
| 74 | + participant Paul |
| 75 | + participant Mary |
| 76 | + box "Shared Resource" |
| 77 | + participant Balance as "Account Balance" |
| 78 | + end box |
| 79 | + |
| 80 | + autonumber |
| 81 | + |
| 82 | + Peter -> "Account Balance": Initial balance = 100 |
| 83 | + |
| 84 | + Peter -> Peter: temp_P = 0 // Local variable for Peter |
| 85 | + Paul -> Paul: temp_L = 0 // Local variable for Paul |
| 86 | + Mary -> Mary: temp_M = 0 // Local variable for Mary |
| 87 | + |
| 88 | + "Account Balance" --> Peter: current balance = 100 |
| 89 | + Peter -> Peter: temp_P = 100 (read) |
| 90 | + |
| 91 | + "Account Balance" --> Paul: current balance = 100 |
| 92 | + Paul -> Paul: temp_L = 100 (read) |
| 93 | + |
| 94 | + "Account Balance" --> Mary: current balance = 100 |
| 95 | + Mary -> Mary: temp_M = 100 (read) |
| 96 | + |
| 97 | + Paul -> Paul: temp_L = 100 - 20 = 80 (calculate) |
| 98 | + Paul -> "Account Balance": Write 80 |
| 99 | + "Account Balance" -> "Account Balance": balance = 80 (updated by Paul) |
| 100 | + |
| 101 | + Mary -> Mary: temp_M = 100 / 2 = 50 (calculate) |
| 102 | + Mary -> "Account Balance": Write 50 |
| 103 | + "Account Balance" -> "Account Balance": balance = 50 (updated by Mary) |
| 104 | + |
| 105 | + Peter -> Peter: temp_P = 100 + 10 = 110 (calculate) // Peter's calculation uses his original read (100) |
| 106 | + Peter -> "Account Balance": Write 110 |
| 107 | + "Account Balance" -> "Account Balance": balance = 110 (updated by Peter) |
| 108 | + |
| 109 | + note right of Peter: Peter's update overwrites Paul's and Mary's changes. |
| 110 | + @enduml |
| 111 | + #+end_src |
| 112 | + |
| 113 | + #+RESULTS: |
| 114 | + [[file:../img/chapter3/exercise3-38-1.png]] |
| 115 | + |
| 116 | +** 余额60 |
| 117 | + #+begin_src plantuml :file ../img/chapter3/exercise3-38-2.png :exports results |
| 118 | + @startuml |
| 119 | + title Interleaved Transaction Example 2: Final Balance $60 |
| 120 | + |
| 121 | + participant Peter |
| 122 | + participant Paul |
| 123 | + participant Mary |
| 124 | + box "Shared Resource" |
| 125 | + participant Balance as "Account Balance" |
| 126 | + end box |
| 127 | + |
| 128 | + autonumber |
| 129 | + |
| 130 | + Peter -> "Account Balance": Initial balance = 100 |
| 131 | + |
| 132 | + Peter -> Peter: temp_P = 0 |
| 133 | + Paul -> Paul: temp_L = 0 |
| 134 | + Mary -> Mary: temp_M = 0 |
| 135 | + |
| 136 | + "Account Balance" --> Paul: current balance = 100 |
| 137 | + Paul -> Paul: temp_L = 100 (read) |
| 138 | + |
| 139 | + "Account Balance" --> Mary: current balance = 100 |
| 140 | + Mary -> Mary: temp_M = 100 (read) |
| 141 | + |
| 142 | + Paul -> Paul: temp_L = 100 - 20 = 80 (calculate) |
| 143 | + Paul -> "Account Balance": Write 80 |
| 144 | + "Account Balance" -> "Account Balance": balance = 80 (updated by Paul) |
| 145 | + |
| 146 | + Mary -> Mary: temp_M = 100 / 2 = 50 (calculate) // Mary's calculation uses her original read (100) |
| 147 | + Mary -> "Account Balance": Write 50 |
| 148 | + "Account Balance" -> "Account Balance": balance = 50 (updated by Mary) |
| 149 | + |
| 150 | + note left of Mary: Mary's update overwrites Paul's previous change. |
| 151 | + |
| 152 | + "Account Balance" --> Peter: current balance = 50 |
| 153 | + Peter -> Peter: temp_P = 50 (read) |
| 154 | + |
| 155 | + Peter -> Peter: temp_P = 50 + 10 = 60 (calculate) |
| 156 | + Peter -> "Account Balance": Write 60 |
| 157 | + "Account Balance" -> "Account Balance": balance = 60 (updated by Peter) |
| 158 | + |
| 159 | + @enduml |
| 160 | + #+end_src |
| 161 | + |
| 162 | + #+RESULTS: |
| 163 | + [[file:../img/chapter3/exercise3-38-2.png]] |
| 164 | + |
| 165 | +** 余额80 |
| 166 | + #+begin_src plantuml :file ../img/chapter3/exercise3-38-3.png :exports results |
| 167 | + @startuml |
| 168 | + title Interleaved Transaction Example 3: Final Balance $80 |
| 169 | + |
| 170 | + participant Peter |
| 171 | + participant Paul |
| 172 | + participant Mary |
| 173 | + box "Shared Resource" |
| 174 | + participant Balance as "Account Balance" |
| 175 | + end box |
| 176 | + |
| 177 | + autonumber |
| 178 | + |
| 179 | + Peter -> "Account Balance": Initial balance = 100 |
| 180 | + |
| 181 | + Peter -> Peter: temp_P = 0 // Local variable for Peter |
| 182 | + Paul -> Paul: temp_L = 0 // Local variable for Paul |
| 183 | + Mary -> Mary: temp_M = 0 // Local variable for Mary |
| 184 | + |
| 185 | + "Account Balance" --> Peter: current balance = 100 |
| 186 | + Peter -> Peter: temp_P = 100 (read) |
| 187 | + |
| 188 | + "Account Balance" --> Paul: current balance = 100 |
| 189 | + Paul -> Paul: temp_L = 100 (read) |
| 190 | + |
| 191 | + "Account Balance" --> Mary: current balance = 100 |
| 192 | + Mary -> Mary: temp_M = 100 (read) |
| 193 | + |
| 194 | + Paul -> Paul: temp_L = 100 - 20 = 80 (calculate) |
| 195 | + |
| 196 | + Mary -> Mary: temp_M = 100 / 2 = 50 (calculate) |
| 197 | + Mary -> "Account Balance": Write 50 |
| 198 | + "Account Balance" -> "Account Balance": balance = 50 (updated by Mary) |
| 199 | + |
| 200 | + Peter -> Peter: temp_P = 100 + 10 = 110 (calculate) // Peter's calculation uses his original read (100) |
| 201 | + Peter -> "Account Balance": Write 110 |
| 202 | + "Account Balance" -> "Account Balance": balance = 110 (updated by Peter) |
| 203 | + |
| 204 | + Paul -> "Account Balance": Write 80 |
| 205 | + "Account Balance" -> "Account Balance": balance = 80 (updated by Paul) |
| 206 | + |
| 207 | + note right of Paul: Paul's update overwrites Peter's and Mary's changes. |
| 208 | + @enduml |
| 209 | + #+end_src |
| 210 | + |
| 211 | + #+RESULTS: |
| 212 | + [[file:../img/chapter3/exercise3-38-3.png]] |
| 213 | + |
| 214 | +** 余额30 |
| 215 | + #+begin_src plantuml :file ../img/chapter3/exercise3-38-4.png :exports results |
| 216 | + @startuml |
| 217 | + title Interleaved Transaction Example 4: Final Balance $30 |
| 218 | + |
| 219 | + participant Peter |
| 220 | + participant Paul |
| 221 | + participant Mary |
| 222 | + box "Shared Resource" |
| 223 | + participant Balance as "Account Balance" |
| 224 | + end box |
| 225 | + |
| 226 | + autonumber |
| 227 | + |
| 228 | + Peter -> "Account Balance": Initial balance = 100 |
| 229 | + |
| 230 | + Peter -> Peter: temp_P = 0 // Local variable for Peter |
| 231 | + Paul -> Paul: temp_L = 0 // Local variable for Paul |
| 232 | + Mary -> Mary: temp_M = 0 // Local variable for Mary |
| 233 | + |
| 234 | + "Account Balance" --> Peter: current balance = 100 |
| 235 | + Peter -> Peter: temp_P = 100 (read) |
| 236 | + |
| 237 | + "Account Balance" --> Mary: current balance = 100 |
| 238 | + Mary -> Mary: temp_M = 100 (read) |
| 239 | + |
| 240 | + Mary -> Mary: temp_M = 100 / 2 = 50 (calculate) |
| 241 | + |
| 242 | + Peter -> Peter: temp_P = 100 + 10 = 110 (calculate) // Peter's calculation uses his original read (100) |
| 243 | + Peter -> "Account Balance": Write 110 |
| 244 | + "Account Balance" -> "Account Balance": balance = 110 (updated by Peter) |
| 245 | + |
| 246 | + Mary -> "Account Balance": Write 50 |
| 247 | + "Account Balance" -> "Account Balance": balance = 50 (updated by Mary) |
| 248 | + |
| 249 | + "Account Balance" --> Paul: current balance = 50 |
| 250 | + Paul -> Paul: temp_L = 50 (read) |
| 251 | + |
| 252 | + Paul -> Paul: temp_L = 50 - 20 = 30 (calculate) |
| 253 | + |
| 254 | + Paul -> "Account Balance": Write 30 |
| 255 | + "Account Balance" -> "Account Balance": balance = 30 (updated by Paul) |
| 256 | + |
| 257 | + note right of Paul: Paul's update overwrites Peter's and Mary's changes. |
| 258 | + @enduml |
| 259 | + #+end_src |
| 260 | + |
| 261 | + #+RESULTS: |
| 262 | + [[file:../img/chapter3/exercise3-38-4.png]] |
0 commit comments