@@ -1,34 +1,39 @@
|
|
1 |
procedure main(argc, argv[]):
|
2 |
shared chopsticks[] := create_semaphores(5, 1)
|
3 |
|
4 |
for id := 0 to 5 do
|
5 |
create_thread(philosopher, id)
|
6 |
end for
|
7 |
end procedure
|
8 |
|
9 |
procedure philosopher(id):
|
10 |
while true do
|
11 |
think()
|
|
|
|
|
|
|
|
|
12 |
get_left_chopstick(id)
|
13 |
get_right_chopstick(id)
|
|
|
14 |
eat()
|
15 |
put_left_chopstick(id)
|
16 |
put_right_chopstick(id)
|
17 |
end while
|
18 |
end procedure
|
19 |
|
20 |
procedure get_left_chopstick(id):
|
21 |
wait(chopsticks[(id + 1) mod 5])
|
22 |
end procedure
|
23 |
|
24 |
procedure get_right_chopstick(id):
|
25 |
wait(chopsticks[id])
|
26 |
end procedure
|
27 |
|
28 |
procedure put_left_chopstick(id):
|
29 |
signal(chopsticks[(id + 1) mod 5])
|
30 |
end procedure
|
31 |
|
32 |
procedure put_right_chopstick(id):
|
33 |
signal(chopsticks[id])
|
34 |
end procedure
|
1 |
procedure main(argc, argv[]):
|
2 |
shared chopsticks[] := create_semaphores(5, 1)
|
3 |
|
4 |
for id := 0 to 5 do
|
5 |
create_thread(philosopher, id)
|
6 |
end for
|
7 |
end procedure
|
8 |
|
9 |
procedure philosopher(id):
|
10 |
while true do
|
11 |
think()
|
12 |
+
if id % 2 = 0 then
|
13 |
+
get_right_chopstick(id)
|
14 |
+
get_left_chopstick(id)
|
15 |
+
else
|
16 |
get_left_chopstick(id)
|
17 |
get_right_chopstick(id)
|
18 |
+
end if
|
19 |
eat()
|
20 |
put_left_chopstick(id)
|
21 |
put_right_chopstick(id)
|
22 |
end while
|
23 |
end procedure
|
24 |
|
25 |
procedure get_left_chopstick(id):
|
26 |
wait(chopsticks[(id + 1) mod 5])
|
27 |
end procedure
|
28 |
|
29 |
procedure get_right_chopstick(id):
|
30 |
wait(chopsticks[id])
|
31 |
end procedure
|
32 |
|
33 |
procedure put_left_chopstick(id):
|
34 |
signal(chopsticks[(id + 1) mod 5])
|
35 |
end procedure
|
36 |
|
37 |
procedure put_right_chopstick(id):
|
38 |
signal(chopsticks[id])
|
39 |
end procedure
|