-
Notifications
You must be signed in to change notification settings - Fork 0
/
readerWritter.py
173 lines (146 loc) · 4.84 KB
/
readerWritter.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import time
import threading
from datetime import datetime
import atexit
def timestamp(data, typ, process, i="/"):
typ = "Reader " if typ == "r" else "Writter"
print(f'{datetime.now().strftime("%X")} : {typ} {process} : Iteration {i} : {data}')
def exit_handler(start):
t = round((time.perf_counter_ns() - start)/(10**9), 2)
print(f"\nFinished... in {t}s\n")
class Problem:
def __init__(
self,
readers,
readerStart,
readerWait,
readerEnd,
readerRepeat,
writters,
writerData,
writterStart,
writterWait,
wriiterEnd,
writterRepeat,
):
"""
Reader Writter Problem
‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
readers - int - number of readers
readerStart - list - waiting time before reader starts
readerWait - list - waiting time before reading data
readerEnd - list - waiting time after reading data
readerRepeat - list - number of time reader reades data
writters - int - number of writters
writerData - list - data to be written by writter
writterStart - list - waiting time before writter starts writting
writterWait - list - waiting time before writting
wriiterEnd - list - waiting time after writting
writterRepeat - list - number of times writter writes
val - list - value to be written/read
rdCount - list - number of writters currently in critical section
"""
self.readers = readers
self.readerStart = readerStart
self.readerWait = readerWait
self.readerEnd = readerEnd
self.readerRepeat = readerRepeat
self.writters = writters
self.writerData = writerData
self.writterStart = writterStart
self.writterWait = writterWait
self.wriiterEnd = wriiterEnd
self.writterRepeat = writterRepeat
self.val = -1
self.rdCount = 0
self.start()
def start(self):
# Writters
for i in range(self.writters):
threading.Thread(
target=self.writter,
args=(
i + 1,
self.writterStart[i],
self.writterWait[i],
self.wriiterEnd[i],
self.writterRepeat[i],
self.writerData[i],
),
).start()
# Readers
for i in range(self.readers):
threading.Thread(
target=self.reader,
args=(
i + 1,
self.readerStart[i],
self.readerStart[i],
self.readerEnd[i],
self.readerRepeat[i],
),
).start()
def wait(self):
time.sleep(0.001)
def reader(self, idx, start, wait, end, repeat):
time.sleep(start)
timestamp("started", "r", idx)
for i in range(repeat):
waitOne = 0
while self.rdCount < 0:
if waitOne:
waitOne = 1
timestamp(f"waiting", "r", idx, i)
self.wait()
self.rdCount += 1
time.sleep(wait)
timestamp(f"< {self.val} >", "r", idx, i)
self.rdCount -= 1
time.sleep(end)
def writter(self, idx, start, wait, end, repeat, data):
time.sleep(start)
timestamp("started", "w", idx)
for i in range(repeat):
waitOne = 0
while self.rdCount > 0:
if waitOne:
waitOne = 1
timestamp(f"waiting", "w", idx, i)
self.wait()
self.rdCount -= 1
time.sleep(wait)
self.val = data
timestamp(f"< {self.val} >", "w", idx, i)
self.rdCount += 1
time.sleep(end)
def linput(data):
return list(map(int, input(data).split()))
def iinput(data):
return int(input(data))
readers = iinput("Number of readers : ")
readerStart = linput("Reader start time : ")
readerWait = linput("Reader Wait time : ")
readerEnd = linput("Reader End time : ")
readerRepeat = linput("Reader Repeats : ")
writters = iinput("Number of writters : ")
writerData = linput("Writter data : ")
writterStart = linput("Writter start time : ")
writterWait = linput("Writter wait time : ")
wriiterEnd = linput("Writter end time : ")
writterRepeat = linput("Writter repeats : ")
start = time.perf_counter_ns()
atexit.register(exit_handler, start=start)
print()
problem = Problem(
readers,
readerStart,
readerWait,
readerEnd,
readerRepeat,
writters,
writerData,
writterStart,
writterWait,
wriiterEnd,
writterRepeat,
)