@@ -1,12 +1,28 @@
|
|
|
|
|
|
|
|
|
|
1 |
main:
|
2 |
while true:
|
3 |
case read_char() of:
|
4 |
'R': create_thread(reader)
|
5 |
'W': create_thread(writer)
|
6 |
EOF: return
|
7 |
|
|
|
|
|
8 |
reader:
|
|
|
|
|
|
|
|
|
9 |
read()
|
|
|
|
|
|
|
|
|
10 |
|
11 |
writer:
|
|
|
12 |
write()
|
|
1 |
+
shared readers := 0
|
2 |
+
shared readers_mutex := semaphore(1)
|
3 |
+
shared can_access := semaphore(1)
|
4 |
+
|
5 |
main:
|
6 |
while true:
|
7 |
case read_char() of:
|
8 |
'R': create_thread(reader)
|
9 |
'W': create_thread(writer)
|
10 |
EOF: return
|
11 |
|
12 |
+
// WRRW
|
13 |
+
|
14 |
reader:
|
15 |
+
wait(readers_mutex)
|
16 |
+
if ++readers = 1 then
|
17 |
+
wait(can_access)
|
18 |
+
signal(readers_mutex)
|
19 |
read()
|
20 |
+
wait(readers_mutex)
|
21 |
+
if --readers = 0 then
|
22 |
+
signal(can_access);
|
23 |
+
signal(readers_mutex)
|
24 |
|
25 |
writer:
|
26 |
+
wait(can_access)
|
27 |
write()
|
28 |
+
signal(can_access)
|