Exemple #1
0
 def __init__(self, list_size):
     self.list_size = list_size  # number of items in the singly linked list
     self.append_mutex = Mutex()
     self.no_search = Semaphore(1)
     self.no_append = Semaphore(1)
     self.search_LS = Lightswitch()
     self.append_LS = Lightswitch()
Exemple #2
0
 def __init__(self, n):
     self.n = n  # number of chairs in the waiting room
     self.customers_count = 0  # number of customers in the waiting room
     self.customers_cut = 0  # number of customers that received their haircut
     self.mutex = Mutex(
     )  # protects access to the customer_count shared variable
     self.customer = Semaphore(
         0)  # customer signals that he is ready to get his hair cut
     self.barber_done = Semaphore(
         0)  # barber signals when the haircut is done
     self.customer_done = Semaphore(
         0)  # customer signals when he is satisfied with the haircut
     self.queue = Queue(
     )  # for queueing customers; each customer puts his semaphore into this FIFO queue
Exemple #3
0
 def __init__(self, n):
     self.n = n  # number of chairs in the waiting room
     self.customers_count = 0  # number of customers in the waiting room
     self.customers_cut = 0  # number of customers that received their haircut
     self.mutex = Mutex(
     )  # protects access to the customer_count shared variable
     self.barber = Semaphore(
         0
     )  # barbers signals that a customer can go sit in the barber chair
     self.customer = Semaphore(
         0)  # customer signals that he is ready to get his hair cut
     self.barber_done = Semaphore(
         0)  # barber signals when the haircut is done
     self.customer_done = Semaphore(
         0)  # customer signals when he is satisfied with the haircut
Exemple #4
0
def main():
    n = 5
    lightswitch = Lightswitch()
    semaphore = Semaphore(1)
    threads = []
    for i in range(n):
        threads.append(Thread(lightswitch_test_fnc, lightswitch, semaphore, i))
    for t in threads:
        t.join()
Exemple #5
0
class SimpleBarrier:
    """
    ADT, use only barrier()
    """
    def __init__(self, n):
        self.n = n
        self.count = 0
        self.mutex = Mutex()
        self.turnstile = Semaphore(0)

    def barrier(self):
        self.mutex.lock()
        self.count += 1
        if self.count == self.n:
            self.count = 0
            self.turnstile.signal(self.n)
        self.mutex.unlock()
        self.turnstile.wait()
Exemple #6
0
def main():
    n = 5  # number of chairs in the waiting room
    shared = Shared(n)

    colors = []
    for i in range(8):
        colors.append("\u001b[{}m".format(30 + i))
    for i in range(8):
        colors.append("\u001b[{};1m".format(30 + i))

    Thread(barber, shared)
    i = 0
    while True:
        sleep(randint(1, 8) / 10)
        Thread(customer, shared, Semaphore(0), i, colors[i % len(colors)])
        i += 1
Exemple #7
0
    def __init__(self):
        self.mutex = Mutex()
        self.agent = Semaphore(1)

        # signal smoker that he can smoke
        self.smoker_tobacco = Semaphore(0)
        self.smoker_paper = Semaphore(0)
        self.smoker_match = Semaphore(0)

        # signal dealer that he can deal
        self.dealer_tobacco = Semaphore(0)
        self.dealer_paper = Semaphore(0)
        self.dealer_match = Semaphore(0)

        self.is_tobacco = False
        self.is_paper = False
        self.is_match = False
Exemple #8
0
class SimpleBarrier:
    #### sl 78
    def __init__(self, N):
        self.N = N
        self.count = 0
        self.m = Mutex()
        #self.m = Semaphore(1)   # mutex
        self.b = Semaphore(0)  # barier

    def barrier(self):
        #self.m.wait()
        self.m.lock()
        self.count += 1
        #self.m.signal()
        self.m.unlock()
        if self.count == self.N:
            self.b.signal()
        self.b.wait()
        self.b.signal()
Exemple #9
0
 def __init__(self, N):
     self.N = N
     self.count = 0
     self.m = Mutex()
     #self.m = Semaphore(1)   # mutex
     self.b = Semaphore(0)  # barier
Exemple #10
0
 def __init__(self, n):
     self.n = n
     self.count = 0
     self.mutex = Mutex()
     self.turnstile = Semaphore(0)
Exemple #11
0
 def __init__(self, n, eat_limit, eat_coef):
     self.n = n
     self.eat_coef = eat_coef
     self.forks = [Semaphore(1) for _ in range(n)]
     self.footman = Semaphore(eat_limit)
Exemple #12
0
 def __init__(self):
     self.readLS = Lightswitch()
     self.roomEmpty = Semaphore(1)
Exemple #13
0
 def __init__(self):
     self.readLS = Lightswitch()
     self.writeLS = Lightswitch()
     self.noWriters = Semaphore(1)
     self.noReaders = Semaphore(1)
Exemple #14
0
 def __init__(self, n, eat_coef):
     self.n = n
     self.eat_coef = eat_coef
     self.forks = [Semaphore(1) for _ in range(n)]
Exemple #15
0
 def __init__(self):
     self.agent = Semaphore(1)
     self.tabacco = Semaphore(0)
     self.paper = Semaphore(0)
     self.match = Semaphore(0)