|
| 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