time.sleep(random.randint(0, 1000) / 1000) def run(self): while True: # wait(self, chopsticks[self.num]) # wait(self, chopsticks[(self.num + 1) % 5]) Swait(self, SemaphoreSet(chopsticks[self.num]), SemaphoreSet(chopsticks[(self.num + 1) % 5])) self.eat() # signal(self, chopsticks[self.num]) # signal(self, chopsticks[(self.num + 1) % 5]) Ssignal(self, SemaphoreSet(chopsticks[self.num]), SemaphoreSet(chopsticks[(self.num + 1) % 5])) self.think() chopsticks = [ Semaphore(1), Semaphore(1), Semaphore(1), Semaphore(1), Semaphore(1) ] all_start(Philosopher(0), Philosopher(1), Philosopher(2), Philosopher(3), Philosopher(4)) print("Done!")
print("Process %s" % num) class P3(Process): def __init__(self): super().__init__() def run(self): while True: print_sems() wait(self, odd) wait(self, mutex) self.print() signal(self, mutex) signal(self, empty) time.sleep(random.randint(0, 10000) / 1000) def print(self): num = buff1.pop(0) print(num) def print_sems(): print(mutex.value, odd.value, even.value, empty.value) all_start(P1(), P2(), P3())
super().__init__() def run(self): global rightcount while True: # print(left.value, right.value) wait(self, right) if rightcount == 0: wait(self, left) rightcount += 1 signal(self, right) self.cross() wait(self, right) rightcount -= 1 if rightcount == 0: signal(self, left) signal(self, right) time.sleep(random.randint(0, 1000) / 1000) def cross(self): print("Car %s →" % self.num) all_start( *[LeftCar(i) for i in range(3)], *[RightCar(i) for i in range(3)], )
signal(self, in_) def shopping(self): customers.append(self.num) print("shopping:%s" % self.num) print("Empty %s" % empty.value) print("shopping_num %s" % len(customers)) time.sleep(random.randint(0, 100)) customers.pop(customers.index(self.num)) def exit(self): wait(self, out) print("out:%s" % self.num) time.sleep(random.randint(0, 1000) / 1000) signal(self, out) signal(self, empty) def run(self): while True: self.walk() self.enter() self.shopping() self.exit() self.walk() all_start(*[Customer(i) for i in range(N)])
def run(self): while True: Swait(self, SemaphoreSet(mx, 1, 1), SemaphoreSet(L, RN, 0)) self.write() Ssignal(self, SemaphoreSet(mx, None, 1)) time.sleep(random.randint(0, 1000) / 1000) def write(self): wlist.append(self.num) print("Writer %s is writing..." % self.num) with open(file, 'w') as f: f.write("I'm writer%02d" % self.num) time.sleep(random.randint(0, 1000) / 1000) print('r:{}, w:{}'.format(rlist, wlist)) wlist.pop(wlist.index(self.num)) RN = 3 rlist = [] # 仅做 debug 之用 wlist = [] # 仅做 debug 之用 rmutex = Semaphore(1) L = Semaphore(RN) mx = Semaphore(1) all_start( *[Writer(i) for i in range(5)], *[Reader(i) for i in range(5)], )
while True: wait(self, full) wait(self, mutex) # 临界区 print("Get {}".format(work_queue.get())) signal(self, mutex) signal(self, empty) print_sem(mutex, full, empty) def print_sem(*sems): print("sems: " + ' '.join([str(sem.value) for sem in sems])) size = 20 work_queue = WorkQueue(size) mutex = Semaphore(1) full = Semaphore(0) empty = Semaphore(size) c1 = Consumer() p1 = Proceducer() p2 = Proceducer() p3 = Proceducer() all_start(c1, p1, p2, p3) print("Done!")