-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiningPhilosophers.py
More file actions
95 lines (75 loc) · 3.22 KB
/
DiningPhilosophers.py
File metadata and controls
95 lines (75 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# This program simulates the dining philosophers problem using threads and locks.
import threading
import time
class Fork:
def __init__(self, id):
self.id = id
self.lock = threading.Lock()
def pick_up(self):
self.lock.acquire()
def put_down(self):
self.lock.release()
class Philosopher:
def __init__(self, id, left_fork, right_fork):
self.id = id
self.left_fork = left_fork
self.right_fork = right_fork
def eat(self):
print(f"Philosopher {self.id} is eating.")
time.sleep(1) # Simulating eating
def think(self):
print(f"Philosopher {self.id} is thinking.")
time.sleep(1)
def dine(self):
# Each philosopher will eat 3 times
for _ in range(3):
self.think()
# Pick up
self.left_fork.pick_up()
print(f"Philosopher {self.id} picked up left fork.")
self.right_fork.pick_up()
print(f"Philosopher {self.id} picked up right fork.")
self.eat()
# Put down
self.right_fork.put_down()
print(f"Philosopher {self.id} put down right fork.")
self.left_fork.put_down()
print(f"Philosopher {self.id} put down left fork.")
# Display Output
def banner():
print("""
###########################################################################################
# Dining Philosophers Problem #
# #
# This program simulates the Dining Philosophers problem using threads and locks. #
# It creates a specified number of philosophers and forks. Each philosopher goes #
# through a cycle of thinking, picking up the left and right forks, eating, and #
# putting down the forks. This process is repeated a specified number of times. #
# #
# Usage: #
# 1. Enter the number of philosophers. #
# 2. The program will run and show the actions performed by each philosopher. #
# #
###########################################################################################
""")
def main():
banner()
philosopher_count = int(input("Enter the number of philosophers: "))
fork_count = philosopher_count
forks = [Fork(i) for i in range(fork_count)]
# Create philosophers with IDs starting from 1
philosophers = [
Philosopher(i + 1, forks[i], forks[(i + 1) % fork_count]) for i in range(philosopher_count)
]
# Create threads for each philosopher
threads = [
threading.Thread(target=philosopher.dine) for philosopher in philosophers
]
# Start threads
for thread in threads:
thread.start()
# Wait for threads to finish
for thread in threads:
thread.join()
if __name__ == "__main__":
main()