def sitOnTable(self):
        forkl,forkr = self.left_fork,self.right_fork
        while self.is_running:
            self.display(values.serialize([self.philosopher_id, values.STATE_WAITING]))
            fork1 = self.get_fork1(forkl)
            state_fork2 = self.get_fork2(forkr)

            fork2 = state_fork2[1]
            #If you cant get the second fork, just drop the first fork
            # wait for some time and try again to pick up the second fork
            if state_fork2[0] == values.ACTION_FAIL:
                self.dropfork(fork1)
                forkl, forkr = forkr, forkl
            else:
                break

        if self.is_running:
            #
            self.display(values.serialize([self.philosopher_id, values.STATE_EATING]))  # IS EATING
            time.sleep(random.randint(2, 6))
            # Release both forks after done eating
            self.dropfork(fork1)
            self.dropfork(fork2)
        else:
            if fork1:
                fork1.close()
            if fork2:
                fork2.close()
Esempio n. 2
0
 def dropfork(self, philosopher_request, connection):
     # philosopher that was using the fork want to clean the dirty fork and put it down
     # only the owner can put this fork down
     if self.is_dirty and self.current_philosopher == philosopher_request[0]:
         last_user = self.current_philosopher
         self.is_dirty = False  #celan the fork
         self.current_philosopher = None  #now the fork belongs to no one and any one of two adjascent philosopher can pick it
         connection.send(values.serialize(
             [self.id, last_user, 0,
              1]))  # let philosophers know the status of this fork
     else:
         connection.send(
             values.serialize([self.id, philosopher_request[0], 0, 0]))
 def dropfork(self, client):
     # Tell fork that this philosopher wants to drop the fork and make it
     # available to other who are trying to access it
     client.send(values.serialize([self.philosopher_id, values.ACTION_DROP_FORK]))
     data = client.receive()
     if data:
         response = pickle.loads(data)
         if \
                 response[1] == self.philosopher_id \
                 and response[2] == values.ACTION_DROP_FORK \
                 and response[3] == values.ACTION_SUCCESS:
             client.close()
         else:
             pass
 def get_fork2(self, fork):
     client = TCPClient(fork[0], fork[1])
     client.connect()
     client.send(values.serialize([self.philosopher_id, values.ACTION_PICK_FORK]))
     data = client.receive()
     if data:
         data_from_fork = pickle.loads(data)
         if \
                 data_from_fork[1] == self.philosopher_id \
                 and data_from_fork[2] == values.ACTION_PICK_FORK \
                 and data_from_fork[3] == values.ACTION_SUCCESS:
             return [values.ACTION_SUCCESS, client]
     client.close()
     return [values.ACTION_FAIL, client]
def get_all_forks():
    global forks
    port = 4001
    try:
        msocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        msocket.sendto(values.serialize(["philosopher",port]),(values.MONITOR_IP,values.MONITOR_PORT2))
    except socket.error as e:
        print("Connection error 0: "+str(e))
    try:
        #this is test
        data, server = msocket.recvfrom(2048)
        forks = values.unserialize(data)
        values.forks = forks
        print(forks)
    except socket.error as e:
        print("Connection error 1: "+str(e))
Esempio n. 6
0
def register_forks():
    host = "localhost"
    port = 4446
    try:
        msocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        msocket.sendto(values.serialize(["fork", port]),
                       (values.MONITOR_IP, values.MONITOR_PORT2))
    except socket.error as e:
        print("Connection error 0: " + str(e))
    try:
        #this is test
        data, server = msocket.recvfrom(values.DATA_SIZE)
        data_array = values.unserialize(data)
        print(str(data_array))
        return data_array
    except socket.error as e:
        print("Connection error 1: " + str(e))
    finally:
        msocket.close()
Esempio n. 7
0
 def pickfork(self, philosopher_request, connection):
     if self.is_dirty:
         sent_data = values.serialize([
             self.id, philosopher_request[0], values.ACTION_PICK_FORK,
             values.ACTION_FAIL
         ])
         connection.send(sent_data)
         print("data received at philosopher : ",
               str(values.unserialize(sent_data)))
         return False
     else:  # If fork is clean/not being used
         self.is_dirty = True
         self.current_philosopher = philosopher_request[0]
         sent_data = self.serialize([
             self.id, self.current_philosopher, values.ACTION_PICK_FORK,
             values.ACTION_SUCCESS
         ])
         connection.send(sent_data)
         print("data received at philosopher : ",
               str(values.unserialize(sent_data)))
         return True
 def get_fork1(self,forkl):
     while self.is_running:
         try:
             time.sleep(1)
             try:
                 lfork = TCPClient(forkl[0],forkl[1])
                 lfork.connect()
             except socket.error as e:
                 print("socekt error lf =",e)
             lfork.send(values.serialize([self.philosopher_id, values.ACTION_PICK_FORK]))
             data = lfork.receive(1024)
             print(str(values.unserialize(data)))
             if data:
                 data_from_fork = values.unserialize(data)
                 if \
                         data_from_fork[1] == self.philosopher_id \
                         and data_from_fork[2] == values.ACTION_PICK_FORK \
                         and data_from_fork[3] == values.ACTION_SUCCESS:
                     return lfork
             lfork.close()
         except Exception as e:
             pass
Esempio n. 9
0
def timeout(background_forks):
    running = True

    host = "localhost"
    port = 4446
    try:
        msocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        msocket.sendto(values.serialize(["timeout", port]),
                       (values.MONITOR_IP, values.MONITOR_PORT2))
    except socket.error as e:
        print("Connection error 0: " + str(e))
    while running:
        try:
            data, server = msocket.recvfrom(values.DATA_SIZE)
            data_array = values.unserialize(data)
            for p in background_forks:
                print("background forks terminated", p)
                p.terminate()
            running = False
        except socket.error as e:
            print("Connection error 1: " + str(e))
            running = False
        pass
Esempio n. 10
0
 def enable(self):
     self.is_running = True
     while self.is_running:
         self.display(values.serialize([self.philosopher_id, values.STATE_THINKING]))
         time.sleep(random.randint(1, 3))
         self.sitOnTable()