Download pseudo source code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
procedure main()
  shared can_access_medium := semaphore(1)
  shared can_access_reading := semaphore(1)
  shared reading_count := 0

  while true do
    case read_char() of
      'R': create_thread(reader)
      'W': create_thread(writer)
      EOF: return
    end case
  end while
end procedure

procedure reader()
  wait(can_access_reading)
    reading_count := reading_count + 1
    if reading_count = 1 then
      wait(can_access_medium)
    end if
  signal(can_access_reading)
  read()
  wait(can_access_reading)
    reading_count := reading_count - 1
    if reading_count = 0 then
      signal(can_access_medium)
    end if
  signal(can_access_reading)
end procedure

procedure writer()
  wait(can_access_medium)
  write()
  signal(can_access_medium)
end procedure