|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "### Works only if arrival time and burst time is in integer\n", |
| 8 | + "### highest priority == higher number " |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "code", |
| 13 | + "execution_count": null, |
| 14 | + "metadata": {}, |
| 15 | + "outputs": [], |
| 16 | + "source": [ |
| 17 | + "# https://cppsecrets.com/users/1108979711510497121461151049710464115111109971051219746101100117/Python-Priority-Scheduling-Non-Pre-emptive-Algorithm-with-Different-Arrival-Time.php\n", |
| 18 | + "\n", |
| 19 | + "\n", |
| 20 | + "class Priority:\n", |
| 21 | + "\n", |
| 22 | + " def processData(self, no_of_processes):\n", |
| 23 | + " process_data = []\n", |
| 24 | + " for i in range(no_of_processes):\n", |
| 25 | + " temporary = []\n", |
| 26 | + " process_id = int(input(\"Enter Process ID: \"))\n", |
| 27 | + "\n", |
| 28 | + " arrival_time = int(input(f\"Enter Arrival Time for Process {process_id}: \"))\n", |
| 29 | + "\n", |
| 30 | + " burst_time = int(input(f\"Enter Burst Time for Process {process_id}: \"))\n", |
| 31 | + "\n", |
| 32 | + " priority = int(input(f\"Enter Priority for Process {process_id}: \"))\n", |
| 33 | + "\n", |
| 34 | + " temporary.extend([process_id, arrival_time, burst_time, priority, 0])\n", |
| 35 | + " '''\n", |
| 36 | + " '0' is the state of the process. 0 means not executed and 1 means execution complete\n", |
| 37 | + " '''\n", |
| 38 | + " process_data.append(temporary)\n", |
| 39 | + " Priority.schedulingProcess(self, process_data)\n", |
| 40 | + "\n", |
| 41 | + "\n", |
| 42 | + " def schedulingProcess(self, process_data):\n", |
| 43 | + " start_time = []\n", |
| 44 | + " exit_time = []\n", |
| 45 | + " s_time = 0\n", |
| 46 | + " process_data.sort(key=lambda x: x[1])\n", |
| 47 | + " '''\n", |
| 48 | + " Sort processes according to the Arrival Time\n", |
| 49 | + " '''\n", |
| 50 | + " for i in range(len(process_data)):\n", |
| 51 | + " ready_queue = []\n", |
| 52 | + " temp = []\n", |
| 53 | + " normal_queue = []\n", |
| 54 | + " for j in range(len(process_data)):\n", |
| 55 | + " if (process_data[j][1] <= s_time) and (process_data[j][4] == 0):\n", |
| 56 | + " temp.extend([process_data[j][0], process_data[j][1], process_data[j][2], process_data[j][3]])\n", |
| 57 | + " ready_queue.append(temp)\n", |
| 58 | + " temp = []\n", |
| 59 | + " elif process_data[j][4] == 0:\n", |
| 60 | + " temp.extend([process_data[j][0], process_data[j][1], process_data[j][2], process_data[j][3]])\n", |
| 61 | + " normal_queue.append(temp)\n", |
| 62 | + " temp = []\n", |
| 63 | + " if len(ready_queue) != 0:\n", |
| 64 | + " ready_queue.sort(key=lambda x: x[3], reverse=True)\n", |
| 65 | + " '''\n", |
| 66 | + " Sort the processes according to the Priority, considering Higher the Value, Higher the Priority\n", |
| 67 | + " '''\n", |
| 68 | + " start_time.append(s_time)\n", |
| 69 | + " s_time = s_time + ready_queue[0][2]\n", |
| 70 | + " e_time = s_time\n", |
| 71 | + " exit_time.append(e_time)\n", |
| 72 | + " for k in range(len(process_data)):\n", |
| 73 | + " if process_data[k][0] == ready_queue[0][0]:\n", |
| 74 | + " break\n", |
| 75 | + " process_data[k][4] = 1\n", |
| 76 | + " process_data[k].append(e_time)\n", |
| 77 | + " elif len(ready_queue) == 0:\n", |
| 78 | + " if s_time < normal_queue[0][1]:\n", |
| 79 | + " s_time = normal_queue[0][1]\n", |
| 80 | + " start_time.append(s_time)\n", |
| 81 | + " s_time = s_time + normal_queue[0][2]\n", |
| 82 | + " e_time = s_time\n", |
| 83 | + " exit_time.append(e_time)\n", |
| 84 | + " for k in range(len(process_data)):\n", |
| 85 | + " if process_data[k][0] == normal_queue[0][0]:\n", |
| 86 | + " break\n", |
| 87 | + " process_data[k][4] = 1\n", |
| 88 | + " process_data[k].append(e_time)\n", |
| 89 | + " t_time = Priority.calculateTurnaroundTime(self, process_data)\n", |
| 90 | + " w_time = Priority.calculateWaitingTime(self, process_data)\n", |
| 91 | + " Priority.printData(self, process_data, t_time, w_time)\n", |
| 92 | + "\n", |
| 93 | + "\n", |
| 94 | + " def calculateTurnaroundTime(self, process_data):\n", |
| 95 | + " total_turnaround_time = 0\n", |
| 96 | + " for i in range(len(process_data)):\n", |
| 97 | + " turnaround_time = process_data[i][5] - process_data[i][1]\n", |
| 98 | + " '''\n", |
| 99 | + " turnaround_time = completion_time - arrival_time\n", |
| 100 | + " '''\n", |
| 101 | + " total_turnaround_time = total_turnaround_time + turnaround_time\n", |
| 102 | + " process_data[i].append(turnaround_time)\n", |
| 103 | + " average_turnaround_time = total_turnaround_time / len(process_data)\n", |
| 104 | + " '''\n", |
| 105 | + " average_turnaround_time = total_turnaround_time / no_of_processes\n", |
| 106 | + " '''\n", |
| 107 | + " return average_turnaround_time\n", |
| 108 | + "\n", |
| 109 | + "\n", |
| 110 | + " def calculateWaitingTime(self, process_data):\n", |
| 111 | + " total_waiting_time = 0\n", |
| 112 | + " for i in range(len(process_data)):\n", |
| 113 | + " waiting_time = process_data[i][6] - process_data[i][2]\n", |
| 114 | + " '''\n", |
| 115 | + " waiting_time = turnaround_time - burst_time\n", |
| 116 | + " '''\n", |
| 117 | + " total_waiting_time = total_waiting_time + waiting_time\n", |
| 118 | + " process_data[i].append(waiting_time)\n", |
| 119 | + " average_waiting_time = total_waiting_time / len(process_data)\n", |
| 120 | + " '''\n", |
| 121 | + " average_waiting_time = total_waiting_time / no_of_processes\n", |
| 122 | + " '''\n", |
| 123 | + " return average_waiting_time\n", |
| 124 | + "\n", |
| 125 | + "\n", |
| 126 | + " def printData(self, process_data, average_turnaround_time, average_waiting_time):\n", |
| 127 | + " process_data.sort(key=lambda x: x[0])\n", |
| 128 | + " '''\n", |
| 129 | + " Sort processes according to the Process ID\n", |
| 130 | + " '''\n", |
| 131 | + " print(\"Process_ID Arrival_Time Burst_Time Priority Completed Completion_Time Turnaround_Time Waiting_Time\")\n", |
| 132 | + " for i in range(len(process_data)):\n", |
| 133 | + " for j in range(len(process_data[i])):\n", |
| 134 | + " print(process_data[i][j], end=\"\\t\\t\")\n", |
| 135 | + " print()\n", |
| 136 | + " print(f'Average Turnaround Time: {average_turnaround_time}')\n", |
| 137 | + "\n", |
| 138 | + " print(f'Average Waiting Time: {average_waiting_time}')\n", |
| 139 | + "\n", |
| 140 | + "\n", |
| 141 | + "if __name__ == \"__main__\":\n", |
| 142 | + " no_of_processes = int(input(\"Enter number of processes: \"))\n", |
| 143 | + " priority = Priority()\n", |
| 144 | + " priority.processData(no_of_processes)\n", |
| 145 | + "\n", |
| 146 | + "### highest priority == higher number \n", |
| 147 | + "\n", |
| 148 | + "\n", |
| 149 | + "'''\n", |
| 150 | + "5\n", |
| 151 | + "1\n", |
| 152 | + "0\n", |
| 153 | + "2\n", |
| 154 | + "2\n", |
| 155 | + "2\n", |
| 156 | + "0\n", |
| 157 | + "1\n", |
| 158 | + "1\n", |
| 159 | + "3\n", |
| 160 | + "0\n", |
| 161 | + "8\n", |
| 162 | + "4\n", |
| 163 | + "4\n", |
| 164 | + "0\n", |
| 165 | + "4\n", |
| 166 | + "2\n", |
| 167 | + "5\n", |
| 168 | + "0\n", |
| 169 | + "5\n", |
| 170 | + "3\n", |
| 171 | + "\n", |
| 172 | + "\n", |
| 173 | + "\n", |
| 174 | + "\n", |
| 175 | + "'''\n", |
| 176 | + "\n" |
| 177 | + ] |
| 178 | + } |
| 179 | + ], |
| 180 | + "metadata": { |
| 181 | + "kernelspec": { |
| 182 | + "display_name": "Python 3", |
| 183 | + "language": "python", |
| 184 | + "name": "python3" |
| 185 | + }, |
| 186 | + "language_info": { |
| 187 | + "codemirror_mode": { |
| 188 | + "name": "ipython", |
| 189 | + "version": 3 |
| 190 | + }, |
| 191 | + "file_extension": ".py", |
| 192 | + "mimetype": "text/x-python", |
| 193 | + "name": "python", |
| 194 | + "nbconvert_exporter": "python", |
| 195 | + "pygments_lexer": "ipython3", |
| 196 | + "version": "3.11.0" |
| 197 | + }, |
| 198 | + "orig_nbformat": 4 |
| 199 | + }, |
| 200 | + "nbformat": 4, |
| 201 | + "nbformat_minor": 2 |
| 202 | +} |
0 commit comments