@@ -1,34 +1,37 @@
|
|
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 |
+
shared quota := create_semaphore(4)
|
4 |
|
5 |
for id := 0 to 5 do
|
6 |
create_thread(philosopher, id)
|
7 |
end for
|
8 |
end procedure
|
9 |
|
10 |
procedure philosopher(id):
|
11 |
while true do
|
12 |
think()
|
13 |
+
wait(quota)
|
14 |
get_left_chopstick(id)
|
15 |
get_right_chopstick(id)
|
16 |
eat()
|
17 |
put_left_chopstick(id)
|
18 |
put_right_chopstick(id)
|
19 |
+
signal(quota)
|
20 |
end while
|
21 |
end procedure
|
22 |
|
23 |
procedure get_left_chopstick(id):
|
24 |
wait(chopsticks[(id + 1) mod 5])
|
25 |
end procedure
|
26 |
|
27 |
procedure get_right_chopstick(id):
|
28 |
wait(chopsticks[id])
|
29 |
end procedure
|
30 |
|
31 |
procedure put_left_chopstick(id):
|
32 |
signal(chopsticks[(id + 1) mod 5])
|
33 |
end procedure
|
34 |
|
35 |
procedure put_right_chopstick(id):
|
36 |
signal(chopsticks[id])
|
37 |
end procedure
|