鱼C论坛

 找回密码
 立即注册
查看: 629|回复: 1

哲学家就餐问题

[复制链接]
发表于 2018-10-18 22:51:26 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
引入服务生解法之后只有偶数项的进程能够拿到两个叉子
1539874227(1).jpg
有办法能让奇数进程也能拿到叉子吗
  1. # -*- coding:utf-8 -*-
  2. import threading
  3. from time import sleep


  4. numPhilosophers = numForks = 10

  5. class Philosopher(threading.Thread):
  6.     def __init__(self, index):
  7.         threading.Thread.__init__(self)
  8.         self.index = index
  9.         self.leftFork = forks[self.index]
  10.         self.rightFork = forks[(self.index + 1) % numForks]

  11.     def run(self):
  12.         while True:
  13.            if waiter.serve(self):
  14.                self.dining()
  15.                waiter.clean(self)
  16.         self.thinking()

  17.     def dining(self):
  18.         print ("Philosopher", self.index, " starts to eat.")
  19.         sleep(1/1000)
  20.         print ("Philosopher", self.index, " finishes eating and leaves to think.")
  21.     def thinking(self):
  22.         sleep(1/100)

  23. class Fork():
  24.     def __init__(self, index):
  25.         self.index = index
  26.         self._lock = threading.Lock()

  27.     def pickup(self):
  28.         self._lock.acquire() #申请锁定进程

  29.     def putdown(self):
  30.         self._lock.release() #释放进程

  31. class Waiter:
  32.     def __init__(self):
  33.         self.forks = [Fork(idx) for idx in range(numForks)]
  34.             #最开始餐叉还没有被分配给任何人,所以全部 False
  35.         self.forks_using = [False] * numForks

  36.         #如果哲学家的左右餐叉都是空闲状态,就为这位哲学家服务提供餐叉
  37.     def serve(self, philor):
  38.         if not self.forks_using[philor.leftFork.index] and not self.forks_using[philor.rightFork.index]:
  39.             self.forks_using[philor.leftFork.index] = True
  40.             self.forks_using[philor.rightFork.index] = True
  41.             self.forks[philor.leftFork.index].pickup()
  42.             self.forks[philor.rightFork.index].pickup()
  43.             return True
  44.         else:
  45.             return False        

  46.         #哲学家用餐完毕后,清理并回收餐叉
  47.     def clean(self, philor):
  48.         self.forks[philor.leftFork.index].putdown()
  49.         self.forks[philor.rightFork.index].putdown()
  50.         self.forks_using[philor.leftFork.index] = False
  51.         self.forks_using[philor.rightFork.index] = False


  52. if __name__ == '__main__':
  53.         #创建服务生与哲学家实例
  54.     waiter = Waiter()
  55.     forks = [Fork(idx) for idx in range(numForks)]
  56.     philosophers = [Philosopher(idx) for idx in range(numPhilosophers)]

  57.         #开启所有的哲学家线程
  58.     for philosopher in philosophers:
  59.         philosopher.start()

  60.         # 方便 CTRL + C 退出程序
  61.     try:
  62.         while True: sleep(0.1)
  63.     except Exception:
  64.         raise e
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-10-18 22:52:16 | 显示全部楼层
运行环境py3.5
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-10 12:51

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表