OS-Lab-10 Solution
OS-Lab-10 Solution
-------------------------------------------------------------------------------------------------- -------------------
Exercise 1:
1. Implement producer consumer problem for bounded buffer.
pg. 1
Operating System – Lab#10
-------------------------------------------------------------------------------------------------- -------------------
2. Implement a process class that can create a process with user defined
attributes.
pg. 2
Operating System – Lab#10
-------------------------------------------------------------------------------------------------- -------------------
3. Implement Reader Writer Problem using multiprocessing package.
pg. 3
Operating System – Lab#10
-------------------------------------------------------------------------------------------------- -------------------
Exercise 2:
Correction:
from multiprocessing import Process, Queue
import time
def reader(queue):
while True:
msg = queue.get()
if (msg == 'DONE'):
break
if __name__=='__main__':
pg. 4
Operating System – Lab#10
-------------------------------------------------------------------------------------------------- -------------------
for count in [10**4, 10**5, 10**6]:
queue = Queue()
# writer() writes to queue
reader_p = Process(target=reader, args=((queue),))
reader_p.daemon = True
reader_p.start()
_start = time.time()
writer(count, queue)
reader_p.join()
print "Sending %s numbers to Queue() took %s seconds" % (count,
(time.time() - _start))
Answer:
This given correction I’ve done showing us how to use queues to feed tasks to a
collection of worker processes and collect the results & print.
pg. 5