|
| 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 | + "\n", |
| 12 | + "# Declare three semaphore variables: mutex, full, and empty.\n", |
| 13 | + "# mutex is used to ensure mutual exclusion when accessing shared variables.\n", |
| 14 | + "# full represents the number of full slots in the buffer.\n", |
| 15 | + "# empty represents the number of empty slots in the buffer.\n", |
| 16 | + "mutex = threading.Semaphore(1)\n", |
| 17 | + "full = threading.Semaphore(0)\n", |
| 18 | + "empty = threading.Semaphore(3)\n", |
| 19 | + "\n", |
| 20 | + "# Variable to keep track of the item count.\n", |
| 21 | + "x = 0\n", |
| 22 | + "\n", |
| 23 | + "# The producer function, responsible for producing items.\n", |
| 24 | + "def producer():\n", |
| 25 | + " global mutex, full, empty, x\n", |
| 26 | + "\n", |
| 27 | + " # Acquire an empty slot in the buffer.\n", |
| 28 | + " empty.acquire()\n", |
| 29 | + "\n", |
| 30 | + " # Acquire the mutex to ensure mutual exclusion when accessing shared variables.\n", |
| 31 | + " mutex.acquire()\n", |
| 32 | + "\n", |
| 33 | + " # Increment the item count.\n", |
| 34 | + " x += 1\n", |
| 35 | + "\n", |
| 36 | + " # Print a message indicating the item is produced.\n", |
| 37 | + " print(f\"Producer produces item {x}\\n\")\n", |
| 38 | + "\n", |
| 39 | + " # Release the mutex to allow other threads to access shared variables.\n", |
| 40 | + " mutex.release()\n", |
| 41 | + "\n", |
| 42 | + " # Release a full slot in the buffer.\n", |
| 43 | + " full.release()\n", |
| 44 | + "\n", |
| 45 | + "# The consumer function, responsible for consuming items.\n", |
| 46 | + "def consumer():\n", |
| 47 | + " global mutex, full, empty, x\n", |
| 48 | + "\n", |
| 49 | + " # Acquire a full slot in the buffer.\n", |
| 50 | + " full.acquire()\n", |
| 51 | + "\n", |
| 52 | + " # Acquire the mutex to ensure mutual exclusion when accessing shared variables.\n", |
| 53 | + " mutex.acquire()\n", |
| 54 | + "\n", |
| 55 | + " # Print a message indicating the item is consumed.\n", |
| 56 | + " print(f\"Consumer consumes item {x}\\n\")\n", |
| 57 | + "\n", |
| 58 | + " # Decrement the item count.\n", |
| 59 | + " x -= 1\n", |
| 60 | + "\n", |
| 61 | + " # Release the mutex to allow other threads to access shared variables.\n", |
| 62 | + " mutex.release()\n", |
| 63 | + "\n", |
| 64 | + " # Release an empty slot in the buffer.\n", |
| 65 | + " empty.release()\n", |
| 66 | + "\n", |
| 67 | + "# The main function to start the producer-consumer simulation.\n", |
| 68 | + "def main():\n", |
| 69 | + " while True:\n", |
| 70 | + " # Display the menu for user selection.\n", |
| 71 | + " print(\"1. PRODUCER\\n2. CONSUMER\\n3. EXIT\")\n", |
| 72 | + " n = int(input(\"ENTER YOUR CHOICE: \\n\"))\n", |
| 73 | + "\n", |
| 74 | + " if n == 1:\n", |
| 75 | + " # If the buffer is not full, start a new producer thread.\n", |
| 76 | + " if empty._value != 0:\n", |
| 77 | + " producer_thread = threading.Thread(target=producer)\n", |
| 78 | + " producer_thread.start()\n", |
| 79 | + " else:\n", |
| 80 | + " print(\"BUFFER IS FULL\")\n", |
| 81 | + "\n", |
| 82 | + " elif n == 2:\n", |
| 83 | + " # If the buffer is not empty, start a new consumer thread.\n", |
| 84 | + " if full._value != 0:\n", |
| 85 | + " consumer_thread = threading.Thread(target=consumer)\n", |
| 86 | + " consumer_thread.start()\n", |
| 87 | + " else:\n", |
| 88 | + " print(\"BUFFER IS EMPTY\")\n", |
| 89 | + "\n", |
| 90 | + " elif n == 3:\n", |
| 91 | + " # Exit the program.\n", |
| 92 | + " break\n", |
| 93 | + "\n", |
| 94 | + " else:\n", |
| 95 | + " print(\"Invalid choice. Please try again.\")\n", |
| 96 | + "\n", |
| 97 | + "if __name__ == \"__main__\":\n", |
| 98 | + " main()\n" |
| 99 | + ] |
| 100 | + }, |
| 101 | + { |
| 102 | + "cell_type": "markdown", |
| 103 | + "metadata": {}, |
| 104 | + "source": [ |
| 105 | + "#### Altenative" |
| 106 | + ] |
| 107 | + }, |
| 108 | + { |
| 109 | + "cell_type": "code", |
| 110 | + "execution_count": null, |
| 111 | + "metadata": {}, |
| 112 | + "outputs": [], |
| 113 | + "source": [ |
| 114 | + "import threading\n", |
| 115 | + "import time\n", |
| 116 | + "import random\n", |
| 117 | + "\n", |
| 118 | + "# Buffer size\n", |
| 119 | + "BUFFER_SIZE = 5\n", |
| 120 | + "\n", |
| 121 | + "# Semaphore to control access to the buffer\n", |
| 122 | + "mutex = threading.Semaphore(1)\n", |
| 123 | + "\n", |
| 124 | + "# Semaphore to count the empty slots in the buffer\n", |
| 125 | + "empty = threading.Semaphore(BUFFER_SIZE)\n", |
| 126 | + "\n", |
| 127 | + "# Semaphore to count the number of items in the buffer\n", |
| 128 | + "full = threading.Semaphore(0)\n", |
| 129 | + "\n", |
| 130 | + "# Buffer to store items\n", |
| 131 | + "buffer = []\n", |
| 132 | + "\n", |
| 133 | + "# The producer function, responsible for producing items\n", |
| 134 | + "def producer():\n", |
| 135 | + " for _ in range(10):\n", |
| 136 | + " # Generate a random item to be produced\n", |
| 137 | + " item = random.randint(1, 100)\n", |
| 138 | + "\n", |
| 139 | + " # Acquire an empty slot in the buffer\n", |
| 140 | + " empty.acquire()\n", |
| 141 | + "\n", |
| 142 | + " # Acquire the mutex to ensure mutual exclusion when accessing the buffer\n", |
| 143 | + " mutex.acquire()\n", |
| 144 | + "\n", |
| 145 | + " # Add the item to the buffer\n", |
| 146 | + " buffer.append(item)\n", |
| 147 | + "\n", |
| 148 | + " # Print a message indicating the item is produced and the current buffer contents\n", |
| 149 | + " print(f\"Producer: Produced item {item}. Buffer: {buffer}\")\n", |
| 150 | + "\n", |
| 151 | + " # Release the mutex to allow other threads to access the buffer\n", |
| 152 | + " mutex.release()\n", |
| 153 | + "\n", |
| 154 | + " # Release a full slot in the buffer to signal that an item is available\n", |
| 155 | + " full.release()\n", |
| 156 | + "\n", |
| 157 | + " # Introduce a random delay to simulate variable production time\n", |
| 158 | + " time.sleep(random.uniform(0.1, 0.5))\n", |
| 159 | + "\n", |
| 160 | + "# The consumer function, responsible for consuming items\n", |
| 161 | + "def consumer():\n", |
| 162 | + " for _ in range(10):\n", |
| 163 | + " # Acquire a full slot in the buffer to check if an item is available for consumption\n", |
| 164 | + " full.acquire()\n", |
| 165 | + "\n", |
| 166 | + " # Acquire the mutex to ensure mutual exclusion when accessing the buffer\n", |
| 167 | + " mutex.acquire()\n", |
| 168 | + "\n", |
| 169 | + " # Remove the first item from the buffer\n", |
| 170 | + " item = buffer.pop(0)\n", |
| 171 | + "\n", |
| 172 | + " # Print a message indicating the item is consumed and the current buffer contents\n", |
| 173 | + " print(f\"Consumer: Consumed item {item}. Buffer: {buffer}\")\n", |
| 174 | + "\n", |
| 175 | + " # Release the mutex to allow other threads to access the buffer\n", |
| 176 | + " mutex.release()\n", |
| 177 | + "\n", |
| 178 | + " # Release an empty slot in the buffer to signal that a slot is available for production\n", |
| 179 | + " empty.release()\n", |
| 180 | + "\n", |
| 181 | + " # Introduce a random delay to simulate variable consumption time\n", |
| 182 | + " time.sleep(random.uniform(0.1, 0.5))\n", |
| 183 | + "\n", |
| 184 | + "if __name__ == \"__main__\":\n", |
| 185 | + " # Create producer and consumer threads\n", |
| 186 | + " producer_thread = threading.Thread(target=producer)\n", |
| 187 | + " consumer_thread = threading.Thread(target=consumer)\n", |
| 188 | + "\n", |
| 189 | + " # Start the threads\n", |
| 190 | + " producer_thread.start()\n", |
| 191 | + " consumer_thread.start()\n", |
| 192 | + "\n", |
| 193 | + " # Wait for the threads to complete\n", |
| 194 | + " producer_thread.join()\n", |
| 195 | + " consumer_thread.join()\n", |
| 196 | + "\n", |
| 197 | + " # Print a message indicating that the simulation is completed\n", |
| 198 | + " print(\"Producer-Consumer simulation completed.\")\n" |
| 199 | + ] |
| 200 | + } |
| 201 | + ], |
| 202 | + "metadata": { |
| 203 | + "kernelspec": { |
| 204 | + "display_name": "Python 3", |
| 205 | + "language": "python", |
| 206 | + "name": "python3" |
| 207 | + }, |
| 208 | + "language_info": { |
| 209 | + "codemirror_mode": { |
| 210 | + "name": "ipython", |
| 211 | + "version": 3 |
| 212 | + }, |
| 213 | + "file_extension": ".py", |
| 214 | + "mimetype": "text/x-python", |
| 215 | + "name": "python", |
| 216 | + "nbconvert_exporter": "python", |
| 217 | + "pygments_lexer": "ipython3", |
| 218 | + "version": "3.11.0" |
| 219 | + }, |
| 220 | + "orig_nbformat": 4 |
| 221 | + }, |
| 222 | + "nbformat": 4, |
| 223 | + "nbformat_minor": 2 |
| 224 | +} |
0 commit comments