Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.28 KB | None | 0 0
  1. import multiprocessing
  2. from multiprocessing import Pipe, Lock
  3. from helper import Message
  4. import time
  5. import math
  6.  
  7.  
  8. class Worker(multiprocessing.Process):
  9.         # TODO : add desc
  10.  
  11.     def __init__(self, id, clock, lock, input_pipe, output_pipes, n):
  12.         multiprocessing.Process.__init__(self)
  13.         self.id = id  # process id
  14.         self.c = clock  # process interior logical clock
  15.         self.lock = lock  # shared Lock object for critical snippet execution
  16.         self.input = input_pipe  # input pipe end in which we send a message
  17.         self.outputs = output_pipes  # all output pipes ends from which we read messages
  18.         self.queue = []  # query of messages for each process
  19.         self.num = n  # number of processes
  20.  
  21.     def run(self):
  22.         """
  23.            Overriden process running method.
  24.        :return: None
  25.        """
  26.  
  27.         for num in range (0, 2):
  28.             # generate a demand for work
  29.             demand = Message("DEMAND", self.id, self.c)
  30.             for i in range(1, self.num):
  31.                 self.input.send(demand)
  32.  
  33.             time.sleep(0.2)
  34.  
  35.             # see if we have received any demands from other processes
  36.             for out in self.outputs:
  37.                 try:
  38.                     msg = out.recv()  # Read from the output pipe
  39.                     if msg is not None:
  40.                         self.queue.append(msg)
  41.  
  42.                 except EOFError:
  43.                     break
  44.  
  45.             time.sleep(0.2)
  46.  
  47.             # check all DEMANDS, update local logical closk and send RESPONSE messages
  48.             # if their clock value is lower than the local value
  49.             for q in self.queue:
  50.                 self.c = max(self.c, q.get_clock()) + 1
  51.                 if self.c > q.get_clock():
  52.                     response = Message("RESPONSE", self.id, q.get_clock())
  53.                     self.input.send(response)
  54.  
  55.             time.sleep(0.2)
  56.  
  57.             # collect all RESPONSES and update personal clock self.C
  58.             num_responses = 0
  59.  
  60.             while num_responses < 1:
  61.                 for out in self.outputs:
  62.                     try:
  63.                         msg = out.recv()  # Read from the output pipe
  64.                         if msg is not None:
  65.                             self.c = max(self.c, msg.get_clock()) + 1
  66.                             num_responses += 1
  67.  
  68.                     except EOFError:
  69.                         break
  70.  
  71.             time.sleep(0.2)
  72.  
  73.             # the dirty work is done here
  74.             self.lock.acquire()
  75.             print ("Process", self.id, ": ulazak u K.O. %s. put." % num)
  76.             time.sleep(2)
  77.             print ("Process", self.id, ": obavljanje posla u K.O.")
  78.  
  79.             for message in self.queue:
  80.                 response = Message("RESPONSE", self.id, message.get_proc_id())
  81.                 self.input.send(response)
  82.  
  83.             self.queue = []
  84.  
  85.             self.lock.release()
  86.  
  87.  
  88.         print ("Proces %s zavrsava sa radom." % self.id)
  89.  
  90.  
  91. if __name__ == '__main__':
  92.     lock = Lock()
  93.     out1, in1 = Pipe()
  94.     out2, in2 = Pipe()
  95.     out3, in3 = Pipe()
  96.  
  97.     w1 = Worker(1, 12, lock, in1, [out2], 3)
  98.     w2 = Worker(2, 7, lock, in2, [out1], 3)
  99.     w3 = Worker(3, 20, lock, in3, [out1, out2], 3)
  100.  
  101.     w1.start()
  102.     w2.start()
  103.     w3.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement