@@ -1,17 +1,35 @@
|
|
1 |
procedure main()
|
|
|
|
|
|
|
|
|
2 |
while true do
|
3 |
case read_char() of
|
4 |
'R': create_thread(reader)
|
5 |
'W': create_thread(writer)
|
6 |
EOF: return
|
7 |
end case
|
8 |
end while
|
9 |
end procedure
|
10 |
|
11 |
procedure reader()
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
read()
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
end procedure
|
14 |
|
15 |
procedure writer()
|
|
|
16 |
write()
|
|
|
17 |
end procedure
|
1 |
procedure main()
|
2 |
+
shared can_access_medium := semaphore(1)
|
3 |
+
shared can_access_reading := semaphore(1)
|
4 |
+
shared reading_count := 0
|
5 |
+
|
6 |
while true do
|
7 |
case read_char() of
|
8 |
'R': create_thread(reader)
|
9 |
'W': create_thread(writer)
|
10 |
EOF: return
|
11 |
end case
|
12 |
end while
|
13 |
end procedure
|
14 |
|
15 |
procedure reader()
|
16 |
+
wait(can_access_reading)
|
17 |
+
reading_count := reading_count + 1
|
18 |
+
if reading_count = 1 then
|
19 |
+
wait(can_access_medium)
|
20 |
+
end if
|
21 |
+
signal(can_access_reading)
|
22 |
read()
|
23 |
+
wait(can_access_reading)
|
24 |
+
reading_count := reading_count - 1
|
25 |
+
if reading_count = 0 then
|
26 |
+
signal(can_access_medium)
|
27 |
+
end if
|
28 |
+
signal(can_access_reading)
|
29 |
end procedure
|
30 |
|
31 |
procedure writer()
|
32 |
+
wait(can_access_medium)
|
33 |
write()
|
34 |
+
signal(can_access_medium)
|
35 |
end procedure
|