[go: up one dir, main page]

0% found this document useful (0 votes)
39 views6 pages

CS604P Assignment 2 Solution Spring 2024

khe the
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)
39 views6 pages

CS604P Assignment 2 Solution Spring 2024

khe the
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/ 6

CS604P ASSIGNMENT 2 SOLUTION SPRING 2024

Due Date: 24-June-2024


Total Marks: 20

DO NOT COPY PASTE OTHERWISE MARKS WITH BE ZERO

FOR MORE SOLUTION CONTACT WHATSAPP 03162965677

Questions No 01 20 marks

You are required to find out mistakes in the following pseudo code. Make correction in pseducode
and explain working of this puesudo code.

Pseudocode Example:

printer_queue = Queue()
printer_lock = Lock()

def printer_daemon():
while True:
if not printer_queue.empty():
printer_lock.acquire()
job = printer_queue.get()
print(f"Printing job: {job}")

JOIN WHATSAPP GROUP


https://chat.whatsapp.com/IF8sHHq2GYz8BJwG9XEJB1
time.sleep(2)
print(f"Completed job: {job}")
printer_lock.release()

def send_print_job(employee_id, job):


print(f"Employee {employee_id} is sending job: {job}")
printer_queue.put(job)
time.sleep(1)

printer_thread = Thread(target=printer_daemon, daemon=True)

employees = [
(1, 'Report_A'),

(3, 'Report_C')
]

for emp_id, job in employees:


Thread(target=send_print_job, (emp_id, job)).start()

Solution:
The provided pseudocode contains a few mistakes that need correction. Here are the necessary
changes and the corresponding explanations

Corrected Pseudocode

from queue import Queue

JOIN WHATSAPP GROUP


https://chat.whatsapp.com/IF8sHHq2GYz8BJwG9XEJB1
from threading import Lock, Thread

import time

printer_queue = Queue()

printer_lock = Lock()

def printer_daemon():

while True:

if not printer_queue.empty():

printer_lock.acquire()

job = printer_queue.get()

print(f"Printing job: {job}")

time.sleep(2)

print(f"Completed job: {job}")

printer_lock.release()

else:

time.sleep(0.1) # Sleep briefly to avoid busy waiting

def send_print_job(employee_id, job):

print(f"Employee {employee_id} is sending job: {job}")

printer_queue.put(job)

JOIN WHATSAPP GROUP


https://chat.whatsapp.com/IF8sHHq2GYz8BJwG9XEJB1
time.sleep(1)

printer_thread = Thread(target=printer_daemon, daemon=True)

printer_thread.start()

employees = [

(1, 'Report_A'),

(3, 'Report_C')

for emp_id, job in employees:

Thread(target=send_print_job, args=(emp_id, job)).start()

Corrections and Explanations

1. Import Statements:
o Ensure the necessary modules are imported:

from queue import Queue

from threading import Lock, Thread

import time

2. Daemon Thread Start:


o Start the printer_thread to run the printer_daemon:

printer_thread.start()

3. Thread Target Arguments:

JOIN WHATSAPP GROUP


https://chat.whatsapp.com/IF8sHHq2GYz8BJwG9XEJB1
o Correct the syntax for passing arguments to the thread target function:

Thread(target=send_print_job, args=(emp_id, job)).start()

o The args keyword is necessary to correctly pass arguments to the send_print_job


function in each new thread.
4. Busy Waiting:
o Add a small sleep in the else branch to prevent busy waiting:

else:

time.sleep(0.1) # Sleep briefly to avoid busy waiting

Working of the Pseudocode

1. Initialization:
o A printer_queue (of type Queue) is created to hold the print jobs.
o A printer_lock (of type Lock) is created to synchronize access to the printer
resource.
2. Printer Daemon Function:
o The printer_daemon function runs indefinitely, checking if there are jobs in the
queue.
o If a job is available, it acquires the lock, processes the job (simulated by
time.sleep(2)), and releases the lock.
o If no job is available, it sleeps briefly (0.1 seconds) to prevent busy waiting,
which reduces CPU usage.
3. Send Print Job Function:
o The send_print_job function prints a message indicating that an employee is
sending a job.
o It puts the job into the printer_queue and simulates a delay (1 second) to represent
the time taken to send the job.
4. Printer Daemon Thread:
o A thread running the printer_daemon function is created and started as a daemon
thread, which means it will run in the background and automatically exit when the
main program exits.
5. Employee Jobs Submission:
o For each employee in the employees list, a new thread is created to run the
send_print_job function with the employee's ID and job as arguments.
o Each thread is started immediately, simulating concurrent job submissions.

JOIN WHATSAPP GROUP


https://chat.whatsapp.com/IF8sHHq2GYz8BJwG9XEJB1
It setup ensures that multiple employees can send print jobs concurrently and the printer daemon
processes them one at a time in the order they were received, maintaining thread safety with the
use of a lock.

REGARD - SARIM
WHATSAPP +923162965677

PLEASE NOTE:
Don't copy-paste the same answer.
Make sure you can make some changes to your solution file before
submitting copy paste solution will be marked zero.
If you found any mistake then correct yourself and inform me.
Before submitting an assignment must check your assignment requirement
file.
If you need some help or question about file and solutions feel free to ask.

FOR FREE ASSIGNMENTS SOLUTIONS VISIT

VUStudentspk.com

JOIN WHATSAPP GROUP


https://chat.whatsapp.com/IF8sHHq2GYz8BJwG9XEJB1

You might also like