0% found this document useful (0 votes)
265 views

OS-Lab-10 Solution

The document outlines exercises for an operating systems lab assignment. Exercise 1 involves implementing the producer-consumer problem with a bounded buffer and creating a process class. Exercise 2 provides code for using multiprocessing queues to send tasks from a writer process to a reader process, and times how long it takes to send different amounts of data. The code is then corrected to properly implement the multiprocessing queue.

Uploaded by

Kashif Umair
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
265 views

OS-Lab-10 Solution

The document outlines exercises for an operating systems lab assignment. Exercise 1 involves implementing the producer-consumer problem with a bounded buffer and creating a process class. Exercise 2 provides code for using multiprocessing queues to send tasks from a writer process to a reader process, and times how long it takes to send different amounts of data. The code is then corrected to properly implement the multiprocessing queue.

Uploaded by

Kashif Umair
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Operating System – Lab#10

Name: M. Imran Shaikh


Course Title: OPERATING SYSTEM

-------------------------------------------------------------------------------------------------- -------------------

Exercise 1:
1. Implement producer consumer problem for bounded buffer.

pg. 1
Operating System – Lab#10

Name: M. Imran Shaikh


Course Title: OPERATING SYSTEM

-------------------------------------------------------------------------------------------------- -------------------
2. Implement a process class that can create a process with user defined
attributes.

pg. 2
Operating System – Lab#10

Name: M. Imran Shaikh


Course Title: OPERATING SYSTEM

-------------------------------------------------------------------------------------------------- -------------------
3. Implement Reader Writer Problem using multiprocessing package.

pg. 3
Operating System – Lab#10

Name: M. Imran Shaikh


Course Title: OPERATING SYSTEM

-------------------------------------------------------------------------------------------------- -------------------

Exercise 2:

Explain and correct the code if required

from multiprocessing import Process, Queue


import time
def reader(queue):
while True
msg = queue.get()
if (msg == 'DONE'):
break
def writer(count, queue):
for i in range(0, count):
queue.put(iii)
queue.put('DONE')
if __name__=='__main__':
for count in [10**4, 10**5, 10**6]:
queue = 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))

Correction:
from multiprocessing import Process, Queue
import time

def reader(queue):
while True:
msg = queue.get()
if (msg == 'DONE'):
break

def writer(count, queue):


for ii in xrange(0, count):
queue.put(ii)
queue.put('DONE')

if __name__=='__main__':

pg. 4
Operating System – Lab#10

Name: M. Imran Shaikh


Course Title: OPERATING SYSTEM

-------------------------------------------------------------------------------------------------- -------------------
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

You might also like