1. Solution 1.
COMP 2432 Operating Systems Tutorial 12 Solution
There can be numerous possible sequences of events (or execution sequences) in these questions. Here are just some examples. Note that there can be multiple statements in
(a) Time PBolivia PPeru flag (b) Time PBolivia PPeru flag
Copyright By PowCoder代写 加微信 powcoder
0 false 0 1111
22 true 21
31 32true 4 1 4 2true 5353 61 63crash 737
8 4 false 8 959
11 2 true 12 1
184 false 195
22 2 true 23 1
25 1 blocked
2. Solution 2. Initially, flag = false
Time PBolivia PPeru flag
while (true) do {
1: while (flag == true) do { };
2: < critical section >
3: flag = true;
4: < remainder section >
while (true) do {
1: while (flag == false) do { };
2: < critical section >
3: flag = false;
4: < remainder section >
10 4 111 121 133 144 151 161 17 1 181 192
3 1 42 52 63
Peruvian cannot take a second run until Bolivan takes its second!
Solution 3.
Initially, flag[0] = false and flag[1] = false
while (true) do {
while (true) do {
flag[0] = true;
if (flag[1] == false) then
else flag[0] = false;
< critical section >
flag[0] = false;
< remainder section >
while (true) do {
while (true) do {
flag[1] = true;
if (flag[0] == false) then
else flag[1] = false;
< critical section >
flag[1] = false;
< remainder section >
Time PBolivia 0
flag[0] false
flag[1] false
(b) Time PBolivia PPeru flag[0] 0 false
flag[1] false
43 5453 67 63 72 true 75 false 8 3 8 5 95false 91
3 2 true 42
11 8 false 12 9 132true 143
16 7 178false 189
20 2 213 224 237
112 true 122
15 5 false 165
181 192true
20 2 true 213
23 5 false
24 5 false 25 1
livelock for 1 both trains
4. Critical Section Problem.
Initially, both flag[1] and flag[2] are false and turn = 1.
Program for P1 Program for P2
There can be many possible answers. Only one of them is shown here.
ME Time P1 P2 flag[1] flag[2] turn Time P1 P2 flag[1] flag[2] turn 0 false false 1 84 11true 94
while (true) do {
1: flag[1] = true;
2: turn = 1;
3: while (flag[2] and turn == 2) do { };
4: < critical section >
5: flag[1] = false;
6: < remainder section >
while (true) do {
1: flag[2] = true;
2: turn = 2;
3: while (flag[1] and turn == 1) do { };
4: < critical section >
5: flag[2] = false;
6: < remainder section >
5 2 213 63 14 74 15
1 10 11 12
PG Time P1 P2 flag[1] flag[2] turn Time P1 P2 flag[1] flag[2] turn
82 2211true
9 3 22 2 2
false false 1 13 1
true 14 1 1true152 1163 174 18 5 false 19 6
false 202 1
11 5 false 24 3 126 254
3: someone wants to enter
BW Time P1 P2 flag[1] flag[2] turn Time P1 P2 flag[1] flag[2] turn 0 false false 1 13 1 true 11true142 1 2 1 true 15 3
4 2 53 63 74 84 93
17 3 184 193 204
21 5 false 226 231true
115false242 1 12 6 25 3 P2starves
3: I want to enter
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com