Skip to content

Commit 64f93bb

Browse files
committed
added semaphore solution for dining philosopher
1 parent bead3d2 commit 64f93bb

File tree

3 files changed

+126
-2
lines changed

3 files changed

+126
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ py -3.11 -m pip install numpy
3434
[Python File](./same_python_file/semaphore_producer_consumer.py)    /    [Python File Alternative](./same_python_file/semaphore_producer_consumer_alternative.py)
3535

3636
### Lab - 4 == Semaphore Solution in Process-Synchronization (Dining Philosopher Problem)
37-
[jupyter File](./exponential_distribution.ipynb) <br/>
38-
[Python File](./same_python_file/exponential_distribution.py)
37+
[jupyter File](./semaphore_dining_philosopher.ipynb) <br/>
38+
[Python File](./same_python_file/semaphore_dining_philosopher.py)
3939

4040
### Lab - 5 == Banker’s Algorithm (Deadlock Avoidance)
4141
[jupyter File](./bankers_algorithm.ipynb) <br/>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import threading
2+
import time
3+
import random
4+
5+
NUM_PHILOSOPHERS = 5
6+
chopsticks = [threading.Semaphore(1) for _ in range(NUM_PHILOSOPHERS)]
7+
8+
def philosopher(philosopher_id):
9+
# Get the left and right chopsticks for the philosopher
10+
left_chopstick = chopsticks[philosopher_id]
11+
right_chopstick = chopsticks[(philosopher_id + 1) % NUM_PHILOSOPHERS]
12+
13+
while True:
14+
# Thinking
15+
print(f"Philosopher {philosopher_id} is thinking.")
16+
time.sleep(random.random())
17+
18+
# Pick up left chopstick
19+
left_chopstick.acquire()
20+
print(f"Philosopher {philosopher_id} picked up the left chopstick.")
21+
22+
# Try to pick up right chopstick without blocking
23+
if right_chopstick.acquire(blocking=False):
24+
# Pick up right chopstick and start eating
25+
print(f"Philosopher {philosopher_id} picked up the right chopstick.")
26+
print(f"Philosopher {philosopher_id} is eating.")
27+
time.sleep(random.random())
28+
29+
# Release right chopstick after eating
30+
right_chopstick.release()
31+
print(f"Philosopher {philosopher_id} put down the right chopstick.")
32+
33+
# Release left chopstick after eating or if right chopstick is not available
34+
left_chopstick.release()
35+
print(f"Philosopher {philosopher_id} put down the left chopstick.")
36+
37+
if __name__ == "__main__":
38+
# Create philosopher threads and start them
39+
philosophers = [threading.Thread(target=philosopher, args=(i,)) for i in range(NUM_PHILOSOPHERS)]
40+
for p in philosophers:
41+
p.start()
42+
43+
# Wait for all philosopher threads to complete
44+
for p in philosophers:
45+
p.join()

semaphore_dining_philosopher.ipynb

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"import threading\n",
10+
"import time\n",
11+
"import random\n",
12+
"\n",
13+
"NUM_PHILOSOPHERS = 5\n",
14+
"chopsticks = [threading.Semaphore(1) for _ in range(NUM_PHILOSOPHERS)]\n",
15+
"\n",
16+
"def philosopher(philosopher_id):\n",
17+
" # Get the left and right chopsticks for the philosopher\n",
18+
" left_chopstick = chopsticks[philosopher_id]\n",
19+
" right_chopstick = chopsticks[(philosopher_id + 1) % NUM_PHILOSOPHERS]\n",
20+
"\n",
21+
" while True:\n",
22+
" # Thinking\n",
23+
" print(f\"Philosopher {philosopher_id} is thinking.\")\n",
24+
" time.sleep(random.random())\n",
25+
"\n",
26+
" # Pick up left chopstick\n",
27+
" left_chopstick.acquire()\n",
28+
" print(f\"Philosopher {philosopher_id} picked up the left chopstick.\")\n",
29+
"\n",
30+
" # Try to pick up right chopstick without blocking\n",
31+
" if right_chopstick.acquire(blocking=False):\n",
32+
" # Pick up right chopstick and start eating\n",
33+
" print(f\"Philosopher {philosopher_id} picked up the right chopstick.\")\n",
34+
" print(f\"Philosopher {philosopher_id} is eating.\")\n",
35+
" time.sleep(random.random())\n",
36+
"\n",
37+
" # Release right chopstick after eating\n",
38+
" right_chopstick.release()\n",
39+
" print(f\"Philosopher {philosopher_id} put down the right chopstick.\")\n",
40+
" \n",
41+
" # Release left chopstick after eating or if right chopstick is not available\n",
42+
" left_chopstick.release()\n",
43+
" print(f\"Philosopher {philosopher_id} put down the left chopstick.\")\n",
44+
"\n",
45+
"if __name__ == \"__main__\":\n",
46+
" # Create philosopher threads and start them\n",
47+
" philosophers = [threading.Thread(target=philosopher, args=(i,)) for i in range(NUM_PHILOSOPHERS)]\n",
48+
" for p in philosophers:\n",
49+
" p.start()\n",
50+
"\n",
51+
" # Wait for all philosopher threads to complete\n",
52+
" for p in philosophers:\n",
53+
" p.join()\n"
54+
]
55+
}
56+
],
57+
"metadata": {
58+
"kernelspec": {
59+
"display_name": "Python 3",
60+
"language": "python",
61+
"name": "python3"
62+
},
63+
"language_info": {
64+
"codemirror_mode": {
65+
"name": "ipython",
66+
"version": 3
67+
},
68+
"file_extension": ".py",
69+
"mimetype": "text/x-python",
70+
"name": "python",
71+
"nbconvert_exporter": "python",
72+
"pygments_lexer": "ipython3",
73+
"version": "3.11.0"
74+
},
75+
"orig_nbformat": 4
76+
},
77+
"nbformat": 4,
78+
"nbformat_minor": 2
79+
}

0 commit comments

Comments
 (0)