Question
Sharing Transmission Lines Suppose N communicating processes share 3 transmission lines, A, B, C . Before using a transmission line, a process calls request() ,
Sharing Transmission Lines
Suppose N communicating processes share 3 transmission lines, A, B, C. Before using a transmission line, a process calls request(), which returns the identity of a free line. After using the line, the process returns it by calling done(line). Complete the implementation below! You do not need to be concerned about fairness among transmission lines, but assuming fairness of the semaphores, your solutions should not starve a communicating process.
Complete the Python implementation below.
from threading import Thread, Semaphore from time import sleep from sys import stdout from random import randint
N = 8
# YOUR CODE HERE raise NotImplementedError() class Communicating(Thread): def __init__(self, i): self.i = i; super().__init__() def run(self): while True: sleep(randint(0, 6)) line = request(); stdout.write(str(self.i) + ' communicating on ' + str(line) + ' '); sleep(randint(0, 3)) stdout.write(str(self.i) + ' done '); done(line)
for i in range(N): Communicating(i).start()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started