Question
Help needed related python ! Thanx from threading import Thread, Semaphore from time import sleep from sys import stdout from random import randint N =
Help needed related python ! Thanx
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()
Output :
A possible output could start with:
3 communicating on C 5 communicating on B 4 communicating on A 4 done 4 communicating on A 3 done 1 communicating on C 0 communicating on C 0 done
Now assume that each communicating processes as a priority. For simplicity, we assume that the process number is the priority, with 0 being the highest priority. Modify request and done such that a line is allocated to the highest priority waiting process!
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(self.i) 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()
Question 2: 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 Question 2: 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 processStep 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