Question: python import threading import time import random # Constants for the number of writers, editors, and available review slots NUM _ WRITERS = 5 #
python
import threading
import time
import random
# Constants for the number of writers, editors, and available review slots
NUMWRITERS # Number of writers to simulate
NUMEDITORS # Number of editors to simulate
NUMSLOTS # Maximum simultaneous reviews
# Semaphore to manage available review slots
reviewslots threading.SemaphoreNUMSLOTS
# Lock to ensure one editor reviews an article at a time
editorlock threading.Lock
# Lock to prevent overlapping print output
printlock threading.Lock
# Shared variables to track the submission and review process
articlessubmitted # Number of articles submitted
TOTALARTICLES NUMWRITERS # Total articles expected
# Event to signal that all articles are reviewed
stopeditors threading.Event
def writertaskwriterid:
Simulates a writer drafting and submitting an article."""
with printlock:
printfWriter writerid is drafting an article."
# Simulate drafting time
time.sleeprandomuniform
with printlock: # console is a shared resource, so we need to lock it
printfWriter writerid is waiting for a review slot."
# TODO : Acquire the review slot before submission
with printlock: # console is a shared resource, so we need to lock it
printfWriter writerid has submitted an article for review."
# TODO : Safely update the shared variable articlessubmitted tracking the number of submitted articles
# TODO : Release the review slot after submission
def editortaskeditorid:
Simulates an editor reviewing articles."""
while not stopeditors.isset:
# Simulate the time before checking for an article
time.sleeprandomuniform
with printlock: # console is a shared resource, so we need to lock it
printfEditor editorid is checking for an article to review."
# TODO : Acquire the editor lock with a timeout to avoid deadlock
try:
# TODO : Check if there are articles to review
with printlock: # console is a shared resource, so we need to lock it
printfEditor editorid is reviewing an article."
time.sleeprandomuniform # Simulate review time
with printlock: # console is a shared resource, so we need to lock it
printfEditor editorid has finished reviewing an article."
# TODO : Safely decrement the number of submitted articles
# TODO : Stop editors if all articles are reviewed
finally:
# TODO : Ensure the editor lock is released
pass # TODO : Remove this line after adding the code
with printlock:
printfEditor editorid is stopping as all reviews are complete."
def main:
Main function to initialize the simulation."""
writerthreads
for i in rangeNUMWRITERS:
t threading.Threadtargetwritertask, argsi
writerthreads.appendt
tstart
editorthreads
for i in rangeNUMEDITORS:
t threading.Threadtargeteditortask, argsi
editorthreads.appendt
tstart
for t in writerthreads:
tjoin
for t in editorthreads:
tjoin
printAll articles have been submitted and reviewed."
if namemain:
main
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
