From b41cd940def1002942a7ef3e4d030a328d5ba54d Mon Sep 17 00:00:00 2001 From: pharshil1902 <57032836+pharshil1902@users.noreply.github.com> Date: Tue, 5 May 2020 18:15:32 +0530 Subject: [PATCH 01/10] Added Shortest Job First Algorithm It is in IPYNB format but the dataframes are really looking good. Please, take a look. --- scheduling/Shortest_Job_First_Algorithm.ipynb | 470 ++++++++++++++++++ 1 file changed, 470 insertions(+) create mode 100644 scheduling/Shortest_Job_First_Algorithm.ipynb diff --git a/scheduling/Shortest_Job_First_Algorithm.ipynb b/scheduling/Shortest_Job_First_Algorithm.ipynb new file mode 100644 index 000000000000..80f21e57df35 --- /dev/null +++ b/scheduling/Shortest_Job_First_Algorithm.ipynb @@ -0,0 +1,470 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "PRAC_11.ipynb", + "provenance": [], + "collapsed_sections": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "xU_L3jkJWSgs", + "colab_type": "text" + }, + "source": [ + "### **SRTF**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "yvbOKIzBWQuA", + "colab_type": "code", + "outputId": "0fdda077-e9ce-475b-e613-b94742f113d9", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 384 + } + }, + "source": [ + "# -*- coding: utf-8 -*-\n", + "\"\"\"\n", + "\n", + "@author: Harshil Panchal\n", + "\"\"\"\n", + "\n", + "# Shortest job remainig first\n", + "# PLease note that arriaval time and burst time are inputted as space separated\n", + "\n", + "import pandas as pd\n", + "\n", + " \n", + "\n", + "def calculate_Waitingtime(arrival_time,burst_time,no_of_processes):\n", + " \n", + " \"\"\"\n", + " This function calculates the Waiting Times of each Processes\n", + " Return: list of Waiting Time.\n", + " >>> calculate_Waitingtime([1,2,3,4],[3,3,5,1],4)\n", + " [0, 3, 5, 0]\n", + " >>> calculate_Waitingtime([1,2,3],[2,5,1],3)\n", + " [0, 2, 0]\n", + " >>> calculate_Waitingtime([2,3],[5,1],2)\n", + " [1, 0]\n", + " \"\"\"\n", + " remaining_time=[0]*no_of_processes \n", + " waiting_time=[0]*no_of_processes\n", + "\n", + "\t# Copy the burst time into remaining_time[] \n", + " for i in range(no_of_processes): \n", + " remaining_time[i] =burst_time[i]\n", + " \n", + " complete = 0\n", + " increment_time = 0\n", + " minm = 999999999\n", + " short = 0\n", + " check = False\n", + "\n", + " # Process until all processes gets \n", + " # completed \n", + " while (complete != no_of_processes):\n", + " for j in range(no_of_processes):\n", + " if(arrival_time[j]<=increment_time and(remaining_time[j]>0 and remaining_time[j]>> calculate_TurnAroundTime([3,3,5,1], 4, [0,3,5,0])\n", + " [3, 6, 10, 1]\n", + " >>> calculate_TurnAroundTime([3,3], 2, [0,3])\n", + " [3, 6]\n", + " >>> calculate_TurnAroundTime([8,10,1], 3, [1,0,3])\n", + " [9, 10, 4]\n", + " \"\"\"\n", + " turn_around_time=[0]*no_of_processes\n", + " for i in range(no_of_processes): \n", + " turn_around_time[i] = burst_time[i] + waiting_time[i] \n", + " return turn_around_time\n", + "def calculate_average_times(waiting_time,turn_around_time, no_of_processes): \n", + " \"\"\"\n", + " This function calculates the average of the waiting & turnaround times\n", + " Prints: Average Waiting time & Average Turn Around Time\n", + " >>> calculate_average_times([0,3,5,0],[3,6,10,1],4)\n", + " Average waiting time = 2.00000 \n", + " Average turn around time = 5.0\n", + "\n", + " >>> calculate_average_times([2,3],[3,6],2)\n", + " Average waiting time = 2.50000 \n", + " Average turn around time = 4.5\n", + "\n", + " >>> calculate_average_times([10,4,3],[2,7,6],3)\n", + " Average waiting time = 5.66667 \n", + " Average turn around time = 5.0\n", + " \"\"\"\n", + " total_waiting_time = 0\n", + " total_turn_around_time = 0\n", + " for i in range(no_of_processes): \n", + "\n", + " total_waiting_time = total_waiting_time + waiting_time[i] \n", + " total_turn_around_time = total_turn_around_time + turn_around_time[i] \n", + "\n", + "\n", + " print(\"\\nAverage waiting time = %.5f \"%(total_waiting_time /no_of_processes) ) \n", + " print(\"Average turn around time = \", total_turn_around_time / no_of_processes) \n", + " \n", + "\n", + "print(\"Enter How Many Process You want To Enter\")\n", + "no_of_processes=int(input())\n", + "burst_time=[0]*no_of_processes\n", + "arrival_time=[0]*no_of_processes\n", + "processes=list(range(1,no_of_processes+1))\n", + "\n", + "\n", + "for i in range(no_of_processes):\n", + " print(\"Enter The Arrival time and Brust time for Process:--\"+str(i+1))\n", + " arrival_time[i], burst_time[i]=map(int,input().split())\n", + "waiting_time=calculate_Waitingtime(arrival_time,burst_time,no_of_processes)\n", + "turn_around_time=calculate_TurnAroundTime(burst_time, no_of_processes, waiting_time)\n", + "calculate_average_times(waiting_time,turn_around_time, no_of_processes)\n", + "processes=list(range(1,no_of_processes+1)) \n", + "FCFS=pd.DataFrame(list(zip(processes,burst_time,arrival_time,waiting_time,turn_around_time)),columns=['Processes','Burst Time','Arrival Time','Waiting Time','Turn Around Time'])\n", + "\n", + "# Printing the dataFrame \n", + "FCFS.head(no_of_processes) \n", + " \n", + " " + ], + "execution_count": 4, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Enter How Many Process You want To Enter\n", + "4\n", + "Enter The Arrival time and Brust time for Process:--1\n", + "1 2\n", + "Enter The Arrival time and Brust time for Process:--2\n", + "2 3\n", + "Enter The Arrival time and Brust time for Process:--3\n", + "2 10\n", + "Enter The Arrival time and Brust time for Process:--4\n", + "1 2\n", + "\n", + "Average waiting time = 2.75000 \n", + "Average turn around time = 7.0\n" + ], + "name": "stdout" + }, + { + "output_type": "execute_result", + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
ProcessesBurst TimeArrival TimeWaiting TimeTurn Around Time
012102
123236
23102616
342124
\n", + "
" + ], + "text/plain": [ + " Processes Burst Time Arrival Time Waiting Time Turn Around Time\n", + "0 1 2 1 0 2\n", + "1 2 3 2 3 6\n", + "2 3 10 2 6 16\n", + "3 4 2 1 2 4" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 4 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "-gEvaGPTWcvf", + "colab_type": "text" + }, + "source": [ + "### **FCFS**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "B7dfqP2lWhdb", + "colab_type": "code", + "outputId": "fdc6ce69-c861-4ee9-d0b2-92a0543731b5", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 338 + } + }, + "source": [ + "# -*- coding: utf-8 -*-\n", + "\"\"\"\n", + "\n", + "@author: Harshil Panchal\n", + "\"\"\"\n", + "\n", + "#first come first serve\n", + "import pandas as pd\n", + "def findWaitingTime(processes, n, bt, at):\n", + " service_time = [0] * n\n", + " service_time[0] = at[0]\n", + " wt = [0] * n\n", + " wt[0] =0\n", + " for i in range(1, n):\n", + "\n", + " # Add burst time of previous processes\n", + " service_time[i] = (service_time[i - 1] +\n", + " bt[i - 1])\n", + " # Find waiting time for current\n", + " # process = sum - at[i]\n", + " wt[i] = service_time[i] - at[i]\n", + " if (wt[i] < 0):\n", + " wt[i] = 0\n", + " \n", + " return wt\n", + " \n", + "\n", + "def findTurnAroundTime(processes, n, bt, wt):\n", + " tat = [0] * n\n", + " for i in range(n):\n", + " tat[i] = bt[i] + wt[i]\n", + " return tat\n", + "\n", + "\n", + "\n", + "def findavgTime(processes, n, bt, at,wt,tat):\n", + " total_wt = 0\n", + " total_tat = 0\n", + " compl_time=[0]*n\n", + " for i in range(n):\n", + " total_wt = total_wt + wt[i]\n", + " total_tat = total_tat + tat[i]\n", + " compl_time[i] = tat[i] + at[i]\n", + " print(\"Average waiting time = %.5f \" % (total_wt / n))\n", + " print(\"\\nAverage turn around time = \", total_tat / n)\n", + " return compl_time\n", + "\n", + "print(\"Enter How Many Process You want To Enter\")\n", + "n=int(input())\n", + "burst_time=[0]*n\n", + "arrival_time=[0]*n\n", + "processes=list(range(1,n+1))\n", + "print(processes)\n", + "\n", + "\n", + "for i in range(n):\n", + " print(\"Enter The Brust time and Arrival time for Process:--\"+str(i+1))\n", + " burst_time[i],arrival_time[i]=map(int,input().split())\n", + " \n", + "wt=findWaitingTime(processes, n, burst_time, arrival_time)\n", + "tat= findTurnAroundTime(processes, n, burst_time, wt)\n", + "compl_time=findavgTime(processes, n, burst_time,arrival_time,wt,tat)\n", + "\n", + "FCFS=pd.DataFrame(list(zip(processes,burst_time,arrival_time,wt,tat,compl_time)),columns=['Processes','Burst Time','Arrival Time','Waiting Time','Turn Around Time','complition Time'])\n", + "\n", + "FCFS.head(n)\n", + "\n" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Enter How Many Process You want To Enter\n", + "3\n", + "[1, 2, 3]\n", + "Enter The Brust time and Arrival time for Process:--1\n", + "3 0\n", + "Enter The Brust time and Arrival time for Process:--2\n", + "2 1\n", + "Enter The Brust time and Arrival time for Process:--3\n", + "4 3\n", + "Average waiting time = 1.33333 \n", + "\n", + "Average turn around time = 4.333333333333333\n" + ], + "name": "stdout" + }, + { + "output_type": "execute_result", + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
ProcessesBurst TimeArrival TimeWaiting TimeTurn Around Timecomplition Time
0130033
1221245
2343269
\n", + "
" + ], + "text/plain": [ + " Processes Burst Time ... Turn Around Time complition Time\n", + "0 1 3 ... 3 3\n", + "1 2 2 ... 4 5\n", + "2 3 4 ... 6 9\n", + "\n", + "[3 rows x 6 columns]" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 1 + } + ] + } + ] +} \ No newline at end of file From a2c0a6fe12a8889fb2d0c9e6da5fcebb7f2c9934 Mon Sep 17 00:00:00 2001 From: pharshil1902 <57032836+pharshil1902@users.noreply.github.com> Date: Tue, 5 May 2020 18:27:49 +0530 Subject: [PATCH 02/10] Delete Shortest_Job_First_Algorithm.ipynb --- scheduling/Shortest_Job_First_Algorithm.ipynb | 470 ------------------ 1 file changed, 470 deletions(-) delete mode 100644 scheduling/Shortest_Job_First_Algorithm.ipynb diff --git a/scheduling/Shortest_Job_First_Algorithm.ipynb b/scheduling/Shortest_Job_First_Algorithm.ipynb deleted file mode 100644 index 80f21e57df35..000000000000 --- a/scheduling/Shortest_Job_First_Algorithm.ipynb +++ /dev/null @@ -1,470 +0,0 @@ -{ - "nbformat": 4, - "nbformat_minor": 0, - "metadata": { - "colab": { - "name": "PRAC_11.ipynb", - "provenance": [], - "collapsed_sections": [] - }, - "kernelspec": { - "name": "python3", - "display_name": "Python 3" - } - }, - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "id": "xU_L3jkJWSgs", - "colab_type": "text" - }, - "source": [ - "### **SRTF**" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "yvbOKIzBWQuA", - "colab_type": "code", - "outputId": "0fdda077-e9ce-475b-e613-b94742f113d9", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 384 - } - }, - "source": [ - "# -*- coding: utf-8 -*-\n", - "\"\"\"\n", - "\n", - "@author: Harshil Panchal\n", - "\"\"\"\n", - "\n", - "# Shortest job remainig first\n", - "# PLease note that arriaval time and burst time are inputted as space separated\n", - "\n", - "import pandas as pd\n", - "\n", - " \n", - "\n", - "def calculate_Waitingtime(arrival_time,burst_time,no_of_processes):\n", - " \n", - " \"\"\"\n", - " This function calculates the Waiting Times of each Processes\n", - " Return: list of Waiting Time.\n", - " >>> calculate_Waitingtime([1,2,3,4],[3,3,5,1],4)\n", - " [0, 3, 5, 0]\n", - " >>> calculate_Waitingtime([1,2,3],[2,5,1],3)\n", - " [0, 2, 0]\n", - " >>> calculate_Waitingtime([2,3],[5,1],2)\n", - " [1, 0]\n", - " \"\"\"\n", - " remaining_time=[0]*no_of_processes \n", - " waiting_time=[0]*no_of_processes\n", - "\n", - "\t# Copy the burst time into remaining_time[] \n", - " for i in range(no_of_processes): \n", - " remaining_time[i] =burst_time[i]\n", - " \n", - " complete = 0\n", - " increment_time = 0\n", - " minm = 999999999\n", - " short = 0\n", - " check = False\n", - "\n", - " # Process until all processes gets \n", - " # completed \n", - " while (complete != no_of_processes):\n", - " for j in range(no_of_processes):\n", - " if(arrival_time[j]<=increment_time and(remaining_time[j]>0 and remaining_time[j]>> calculate_TurnAroundTime([3,3,5,1], 4, [0,3,5,0])\n", - " [3, 6, 10, 1]\n", - " >>> calculate_TurnAroundTime([3,3], 2, [0,3])\n", - " [3, 6]\n", - " >>> calculate_TurnAroundTime([8,10,1], 3, [1,0,3])\n", - " [9, 10, 4]\n", - " \"\"\"\n", - " turn_around_time=[0]*no_of_processes\n", - " for i in range(no_of_processes): \n", - " turn_around_time[i] = burst_time[i] + waiting_time[i] \n", - " return turn_around_time\n", - "def calculate_average_times(waiting_time,turn_around_time, no_of_processes): \n", - " \"\"\"\n", - " This function calculates the average of the waiting & turnaround times\n", - " Prints: Average Waiting time & Average Turn Around Time\n", - " >>> calculate_average_times([0,3,5,0],[3,6,10,1],4)\n", - " Average waiting time = 2.00000 \n", - " Average turn around time = 5.0\n", - "\n", - " >>> calculate_average_times([2,3],[3,6],2)\n", - " Average waiting time = 2.50000 \n", - " Average turn around time = 4.5\n", - "\n", - " >>> calculate_average_times([10,4,3],[2,7,6],3)\n", - " Average waiting time = 5.66667 \n", - " Average turn around time = 5.0\n", - " \"\"\"\n", - " total_waiting_time = 0\n", - " total_turn_around_time = 0\n", - " for i in range(no_of_processes): \n", - "\n", - " total_waiting_time = total_waiting_time + waiting_time[i] \n", - " total_turn_around_time = total_turn_around_time + turn_around_time[i] \n", - "\n", - "\n", - " print(\"\\nAverage waiting time = %.5f \"%(total_waiting_time /no_of_processes) ) \n", - " print(\"Average turn around time = \", total_turn_around_time / no_of_processes) \n", - " \n", - "\n", - "print(\"Enter How Many Process You want To Enter\")\n", - "no_of_processes=int(input())\n", - "burst_time=[0]*no_of_processes\n", - "arrival_time=[0]*no_of_processes\n", - "processes=list(range(1,no_of_processes+1))\n", - "\n", - "\n", - "for i in range(no_of_processes):\n", - " print(\"Enter The Arrival time and Brust time for Process:--\"+str(i+1))\n", - " arrival_time[i], burst_time[i]=map(int,input().split())\n", - "waiting_time=calculate_Waitingtime(arrival_time,burst_time,no_of_processes)\n", - "turn_around_time=calculate_TurnAroundTime(burst_time, no_of_processes, waiting_time)\n", - "calculate_average_times(waiting_time,turn_around_time, no_of_processes)\n", - "processes=list(range(1,no_of_processes+1)) \n", - "FCFS=pd.DataFrame(list(zip(processes,burst_time,arrival_time,waiting_time,turn_around_time)),columns=['Processes','Burst Time','Arrival Time','Waiting Time','Turn Around Time'])\n", - "\n", - "# Printing the dataFrame \n", - "FCFS.head(no_of_processes) \n", - " \n", - " " - ], - "execution_count": 4, - "outputs": [ - { - "output_type": "stream", - "text": [ - "Enter How Many Process You want To Enter\n", - "4\n", - "Enter The Arrival time and Brust time for Process:--1\n", - "1 2\n", - "Enter The Arrival time and Brust time for Process:--2\n", - "2 3\n", - "Enter The Arrival time and Brust time for Process:--3\n", - "2 10\n", - "Enter The Arrival time and Brust time for Process:--4\n", - "1 2\n", - "\n", - "Average waiting time = 2.75000 \n", - "Average turn around time = 7.0\n" - ], - "name": "stdout" - }, - { - "output_type": "execute_result", - "data": { - "text/html": [ - "
\n", - "\n", - "\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "
ProcessesBurst TimeArrival TimeWaiting TimeTurn Around Time
012102
123236
23102616
342124
\n", - "
" - ], - "text/plain": [ - " Processes Burst Time Arrival Time Waiting Time Turn Around Time\n", - "0 1 2 1 0 2\n", - "1 2 3 2 3 6\n", - "2 3 10 2 6 16\n", - "3 4 2 1 2 4" - ] - }, - "metadata": { - "tags": [] - }, - "execution_count": 4 - } - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "-gEvaGPTWcvf", - "colab_type": "text" - }, - "source": [ - "### **FCFS**" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "B7dfqP2lWhdb", - "colab_type": "code", - "outputId": "fdc6ce69-c861-4ee9-d0b2-92a0543731b5", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 338 - } - }, - "source": [ - "# -*- coding: utf-8 -*-\n", - "\"\"\"\n", - "\n", - "@author: Harshil Panchal\n", - "\"\"\"\n", - "\n", - "#first come first serve\n", - "import pandas as pd\n", - "def findWaitingTime(processes, n, bt, at):\n", - " service_time = [0] * n\n", - " service_time[0] = at[0]\n", - " wt = [0] * n\n", - " wt[0] =0\n", - " for i in range(1, n):\n", - "\n", - " # Add burst time of previous processes\n", - " service_time[i] = (service_time[i - 1] +\n", - " bt[i - 1])\n", - " # Find waiting time for current\n", - " # process = sum - at[i]\n", - " wt[i] = service_time[i] - at[i]\n", - " if (wt[i] < 0):\n", - " wt[i] = 0\n", - " \n", - " return wt\n", - " \n", - "\n", - "def findTurnAroundTime(processes, n, bt, wt):\n", - " tat = [0] * n\n", - " for i in range(n):\n", - " tat[i] = bt[i] + wt[i]\n", - " return tat\n", - "\n", - "\n", - "\n", - "def findavgTime(processes, n, bt, at,wt,tat):\n", - " total_wt = 0\n", - " total_tat = 0\n", - " compl_time=[0]*n\n", - " for i in range(n):\n", - " total_wt = total_wt + wt[i]\n", - " total_tat = total_tat + tat[i]\n", - " compl_time[i] = tat[i] + at[i]\n", - " print(\"Average waiting time = %.5f \" % (total_wt / n))\n", - " print(\"\\nAverage turn around time = \", total_tat / n)\n", - " return compl_time\n", - "\n", - "print(\"Enter How Many Process You want To Enter\")\n", - "n=int(input())\n", - "burst_time=[0]*n\n", - "arrival_time=[0]*n\n", - "processes=list(range(1,n+1))\n", - "print(processes)\n", - "\n", - "\n", - "for i in range(n):\n", - " print(\"Enter The Brust time and Arrival time for Process:--\"+str(i+1))\n", - " burst_time[i],arrival_time[i]=map(int,input().split())\n", - " \n", - "wt=findWaitingTime(processes, n, burst_time, arrival_time)\n", - "tat= findTurnAroundTime(processes, n, burst_time, wt)\n", - "compl_time=findavgTime(processes, n, burst_time,arrival_time,wt,tat)\n", - "\n", - "FCFS=pd.DataFrame(list(zip(processes,burst_time,arrival_time,wt,tat,compl_time)),columns=['Processes','Burst Time','Arrival Time','Waiting Time','Turn Around Time','complition Time'])\n", - "\n", - "FCFS.head(n)\n", - "\n" - ], - "execution_count": 0, - "outputs": [ - { - "output_type": "stream", - "text": [ - "Enter How Many Process You want To Enter\n", - "3\n", - "[1, 2, 3]\n", - "Enter The Brust time and Arrival time for Process:--1\n", - "3 0\n", - "Enter The Brust time and Arrival time for Process:--2\n", - "2 1\n", - "Enter The Brust time and Arrival time for Process:--3\n", - "4 3\n", - "Average waiting time = 1.33333 \n", - "\n", - "Average turn around time = 4.333333333333333\n" - ], - "name": "stdout" - }, - { - "output_type": "execute_result", - "data": { - "text/html": [ - "
\n", - "\n", - "\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "
ProcessesBurst TimeArrival TimeWaiting TimeTurn Around Timecomplition Time
0130033
1221245
2343269
\n", - "
" - ], - "text/plain": [ - " Processes Burst Time ... Turn Around Time complition Time\n", - "0 1 3 ... 3 3\n", - "1 2 2 ... 4 5\n", - "2 3 4 ... 6 9\n", - "\n", - "[3 rows x 6 columns]" - ] - }, - "metadata": { - "tags": [] - }, - "execution_count": 1 - } - ] - } - ] -} \ No newline at end of file From 76d50daf9890a7a999b30a8f7325be54a02a65af Mon Sep 17 00:00:00 2001 From: pharshil1902 <57032836+pharshil1902@users.noreply.github.com> Date: Tue, 5 May 2020 18:28:20 +0530 Subject: [PATCH 03/10] Added Shortest Job First Algorithm --- scheduling/Shortest_Job_First Algorithm.py | 218 +++++++++++++++++++++ 1 file changed, 218 insertions(+) create mode 100644 scheduling/Shortest_Job_First Algorithm.py diff --git a/scheduling/Shortest_Job_First Algorithm.py b/scheduling/Shortest_Job_First Algorithm.py new file mode 100644 index 000000000000..ed7974d3919e --- /dev/null +++ b/scheduling/Shortest_Job_First Algorithm.py @@ -0,0 +1,218 @@ +# -*- coding: utf-8 -*- +"""PRAC_11.ipynb + +Automatically generated by Colaboratory. + +Original file is located at + https://colab.research.google.com/drive/1TxowpbN8JhayJ9T2k25U5sf_NPVxdM6p + +### **SRTF** +""" + +# -*- coding: utf-8 -*- +""" + +@author: Harshil Panchal +""" + +# Shortest job remainig first +# PLease note that arriaval time and burst time are inputted as space separated + +import pandas as pd + + + +def calculate_Waitingtime(arrival_time,burst_time,no_of_processes): + + """ + This function calculates the Waiting Times of each Processes + Return: list of Waiting Time. + >>> calculate_Waitingtime([1,2,3,4],[3,3,5,1],4) + [0, 3, 5, 0] + >>> calculate_Waitingtime([1,2,3],[2,5,1],3) + [0, 2, 0] + >>> calculate_Waitingtime([2,3],[5,1],2) + [1, 0] + """ + remaining_time=[0]*no_of_processes + waiting_time=[0]*no_of_processes + + # Copy the burst time into remaining_time[] + for i in range(no_of_processes): + remaining_time[i] =burst_time[i] + + complete = 0 + increment_time = 0 + minm = 999999999 + short = 0 + check = False + + # Process until all processes gets + # completed + while (complete != no_of_processes): + for j in range(no_of_processes): + if(arrival_time[j]<=increment_time and(remaining_time[j]>0 and remaining_time[j]>> calculate_TurnAroundTime([3,3,5,1], 4, [0,3,5,0]) + [3, 6, 10, 1] + >>> calculate_TurnAroundTime([3,3], 2, [0,3]) + [3, 6] + >>> calculate_TurnAroundTime([8,10,1], 3, [1,0,3]) + [9, 10, 4] + """ + turn_around_time=[0]*no_of_processes + for i in range(no_of_processes): + turn_around_time[i] = burst_time[i] + waiting_time[i] + return turn_around_time +def calculate_average_times(waiting_time,turn_around_time, no_of_processes): + """ + This function calculates the average of the waiting & turnaround times + Prints: Average Waiting time & Average Turn Around Time + >>> calculate_average_times([0,3,5,0],[3,6,10,1],4) + Average waiting time = 2.00000 + Average turn around time = 5.0 + + >>> calculate_average_times([2,3],[3,6],2) + Average waiting time = 2.50000 + Average turn around time = 4.5 + + >>> calculate_average_times([10,4,3],[2,7,6],3) + Average waiting time = 5.66667 + Average turn around time = 5.0 + """ + total_waiting_time = 0 + total_turn_around_time = 0 + for i in range(no_of_processes): + + total_waiting_time = total_waiting_time + waiting_time[i] + total_turn_around_time = total_turn_around_time + turn_around_time[i] + + + print("\nAverage waiting time = %.5f "%(total_waiting_time /no_of_processes) ) + print("Average turn around time = ", total_turn_around_time / no_of_processes) + + +print("Enter How Many Process You want To Enter") +no_of_processes=int(input()) +burst_time=[0]*no_of_processes +arrival_time=[0]*no_of_processes +processes=list(range(1,no_of_processes+1)) + + +for i in range(no_of_processes): + print("Enter The Arrival time and Brust time for Process:--"+str(i+1)) + arrival_time[i], burst_time[i]=map(int,input().split()) +waiting_time=calculate_Waitingtime(arrival_time,burst_time,no_of_processes) +turn_around_time=calculate_TurnAroundTime(burst_time, no_of_processes, waiting_time) +calculate_average_times(waiting_time,turn_around_time, no_of_processes) +processes=list(range(1,no_of_processes+1)) +FCFS=pd.DataFrame(list(zip(processes,burst_time,arrival_time,waiting_time,turn_around_time)),columns=['Processes','Burst Time','Arrival Time','Waiting Time','Turn Around Time']) + +# Printing the dataFrame +FCFS.head(no_of_processes) + +"""### **FCFS**""" + +# -*- coding: utf-8 -*- +""" + +@author: Harshil Panchal +""" + +#first come first serve +import pandas as pd +def findWaitingTime(processes, n, bt, at): + service_time = [0] * n + service_time[0] = at[0] + wt = [0] * n + wt[0] =0 + for i in range(1, n): + + # Add burst time of previous processes + service_time[i] = (service_time[i - 1] + + bt[i - 1]) + # Find waiting time for current + # process = sum - at[i] + wt[i] = service_time[i] - at[i] + if (wt[i] < 0): + wt[i] = 0 + + return wt + + +def findTurnAroundTime(processes, n, bt, wt): + tat = [0] * n + for i in range(n): + tat[i] = bt[i] + wt[i] + return tat + + + +def findavgTime(processes, n, bt, at,wt,tat): + total_wt = 0 + total_tat = 0 + compl_time=[0]*n + for i in range(n): + total_wt = total_wt + wt[i] + total_tat = total_tat + tat[i] + compl_time[i] = tat[i] + at[i] + print("Average waiting time = %.5f " % (total_wt / n)) + print("\nAverage turn around time = ", total_tat / n) + return compl_time + +print("Enter How Many Process You want To Enter") +n=int(input()) +burst_time=[0]*n +arrival_time=[0]*n +processes=list(range(1,n+1)) +print(processes) + + +for i in range(n): + print("Enter The Brust time and Arrival time for Process:--"+str(i+1)) + burst_time[i],arrival_time[i]=map(int,input().split()) + +wt=findWaitingTime(processes, n, burst_time, arrival_time) +tat= findTurnAroundTime(processes, n, burst_time, wt) +compl_time=findavgTime(processes, n, burst_time,arrival_time,wt,tat) + +FCFS=pd.DataFrame(list(zip(processes,burst_time,arrival_time,wt,tat,compl_time)),columns=['Processes','Burst Time','Arrival Time','Waiting Time','Turn Around Time','complition Time']) + +FCFS.head(n) \ No newline at end of file From 3e17e7e577b60fe34c1a9b33a38b64ef39b4c995 Mon Sep 17 00:00:00 2001 From: pharshil1902 <57032836+pharshil1902@users.noreply.github.com> Date: Tue, 5 May 2020 18:28:59 +0530 Subject: [PATCH 04/10] Update Shortest_Job_First Algorithm.py --- scheduling/Shortest_Job_First Algorithm.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/scheduling/Shortest_Job_First Algorithm.py b/scheduling/Shortest_Job_First Algorithm.py index ed7974d3919e..a0fc2d2e268d 100644 --- a/scheduling/Shortest_Job_First Algorithm.py +++ b/scheduling/Shortest_Job_First Algorithm.py @@ -1,10 +1,5 @@ # -*- coding: utf-8 -*- -"""PRAC_11.ipynb -Automatically generated by Colaboratory. - -Original file is located at - https://colab.research.google.com/drive/1TxowpbN8JhayJ9T2k25U5sf_NPVxdM6p ### **SRTF** """ @@ -215,4 +210,4 @@ def findavgTime(processes, n, bt, at,wt,tat): FCFS=pd.DataFrame(list(zip(processes,burst_time,arrival_time,wt,tat,compl_time)),columns=['Processes','Burst Time','Arrival Time','Waiting Time','Turn Around Time','complition Time']) -FCFS.head(n) \ No newline at end of file +FCFS.head(n) From b34bb48671d5b1bfaf23748d0ab3bf2444934841 Mon Sep 17 00:00:00 2001 From: pharshil1902 <57032836+pharshil1902@users.noreply.github.com> Date: Tue, 5 May 2020 18:34:48 +0530 Subject: [PATCH 05/10] Update Shortest_Job_First Algorithm.py --- scheduling/Shortest_Job_First Algorithm.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/scheduling/Shortest_Job_First Algorithm.py b/scheduling/Shortest_Job_First Algorithm.py index a0fc2d2e268d..226fda0ae08b 100644 --- a/scheduling/Shortest_Job_First Algorithm.py +++ b/scheduling/Shortest_Job_First Algorithm.py @@ -1,14 +1,4 @@ -# -*- coding: utf-8 -*- - -### **SRTF** -""" - -# -*- coding: utf-8 -*- -""" - -@author: Harshil Panchal -""" # Shortest job remainig first # PLease note that arriaval time and burst time are inputted as space separated From a3905958374981ad3fbb603d4a80888e01f0e2cf Mon Sep 17 00:00:00 2001 From: pharshil1902 <57032836+pharshil1902@users.noreply.github.com> Date: Thu, 7 May 2020 12:35:19 +0530 Subject: [PATCH 06/10] Update Shortest_Job_first Algorithm --- scheduling/Shortest_Job_First Algorithm.py | 124 +++++---------------- 1 file changed, 29 insertions(+), 95 deletions(-) diff --git a/scheduling/Shortest_Job_First Algorithm.py b/scheduling/Shortest_Job_First Algorithm.py index 226fda0ae08b..1c2be96d083a 100644 --- a/scheduling/Shortest_Job_First Algorithm.py +++ b/scheduling/Shortest_Job_First Algorithm.py @@ -1,7 +1,8 @@ # Shortest job remainig first -# PLease note that arriaval time and burst time are inputted as space separated +# PLease note that arriaval time and burst +#time are inputted as space separated import pandas as pd @@ -11,7 +12,7 @@ def calculate_Waitingtime(arrival_time,burst_time,no_of_processes): """ This function calculates the Waiting Times of each Processes - Return: list of Waiting Time. + Return: list of Waiting Time. >>> calculate_Waitingtime([1,2,3,4],[3,3,5,1],4) [0, 3, 5, 0] >>> calculate_Waitingtime([1,2,3],[2,5,1],3) @@ -21,8 +22,7 @@ def calculate_Waitingtime(arrival_time,burst_time,no_of_processes): """ remaining_time=[0]*no_of_processes waiting_time=[0]*no_of_processes - - # Copy the burst time into remaining_time[] + # Copy the burst time into remaining_time[] for i in range(no_of_processes): remaining_time[i] =burst_time[i] @@ -36,12 +36,13 @@ def calculate_Waitingtime(arrival_time,burst_time,no_of_processes): # completed while (complete != no_of_processes): for j in range(no_of_processes): - if(arrival_time[j]<=increment_time and(remaining_time[j]>0 and remaining_time[j]0): + if(remaining_time[j]>> calculate_TurnAroundTime([3,3,5,1], 4, [0,3,5,0]) [3, 6, 10, 1] >>> calculate_TurnAroundTime([3,3], 2, [0,3]) @@ -89,15 +88,13 @@ def calculate_TurnAroundTime(burst_time, no_of_processes, waiting_time): def calculate_average_times(waiting_time,turn_around_time, no_of_processes): """ This function calculates the average of the waiting & turnaround times - Prints: Average Waiting time & Average Turn Around Time + Prints: Average Waiting time & Average Turn Around Time >>> calculate_average_times([0,3,5,0],[3,6,10,1],4) Average waiting time = 2.00000 Average turn around time = 5.0 - >>> calculate_average_times([2,3],[3,6],2) Average waiting time = 2.50000 Average turn around time = 4.5 - >>> calculate_average_times([10,4,3],[2,7,6],3) Average waiting time = 5.66667 Average turn around time = 5.0 @@ -105,13 +102,12 @@ def calculate_average_times(waiting_time,turn_around_time, no_of_processes): total_waiting_time = 0 total_turn_around_time = 0 for i in range(no_of_processes): - total_waiting_time = total_waiting_time + waiting_time[i] total_turn_around_time = total_turn_around_time + turn_around_time[i] - - - print("\nAverage waiting time = %.5f "%(total_waiting_time /no_of_processes) ) - print("Average turn around time = ", total_turn_around_time / no_of_processes) + print("\nAverage waiting time = %.5f "%( + total_waiting_time /no_of_processes) ) + print("Average turn around time = ", + total_turn_around_time / no_of_processes) print("Enter How Many Process You want To Enter") @@ -120,84 +116,22 @@ def calculate_average_times(waiting_time,turn_around_time, no_of_processes): arrival_time=[0]*no_of_processes processes=list(range(1,no_of_processes+1)) - for i in range(no_of_processes): print("Enter The Arrival time and Brust time for Process:--"+str(i+1)) arrival_time[i], burst_time[i]=map(int,input().split()) waiting_time=calculate_Waitingtime(arrival_time,burst_time,no_of_processes) -turn_around_time=calculate_TurnAroundTime(burst_time, no_of_processes, waiting_time) +bt=burst_time +n=no_of_processes +wt=waiting_time +turn_around_time=calculate_TurnAroundTime(bt,n,wt) calculate_average_times(waiting_time,turn_around_time, no_of_processes) processes=list(range(1,no_of_processes+1)) -FCFS=pd.DataFrame(list(zip(processes,burst_time,arrival_time,waiting_time,turn_around_time)),columns=['Processes','Burst Time','Arrival Time','Waiting Time','Turn Around Time']) +FCFS=pd.DataFrame(list(zip(processes,burst_time, +arrival_time,waiting_time,turn_around_time)), +columns=['Process','BurstTime','ArrivalTime','WaitingTime','TurnAroundTime']) # Printing the dataFrame -FCFS.head(no_of_processes) - -"""### **FCFS**""" - -# -*- coding: utf-8 -*- -""" - -@author: Harshil Panchal -""" - -#first come first serve -import pandas as pd -def findWaitingTime(processes, n, bt, at): - service_time = [0] * n - service_time[0] = at[0] - wt = [0] * n - wt[0] =0 - for i in range(1, n): - - # Add burst time of previous processes - service_time[i] = (service_time[i - 1] + - bt[i - 1]) - # Find waiting time for current - # process = sum - at[i] - wt[i] = service_time[i] - at[i] - if (wt[i] < 0): - wt[i] = 0 - - return wt - - -def findTurnAroundTime(processes, n, bt, wt): - tat = [0] * n - for i in range(n): - tat[i] = bt[i] + wt[i] - return tat - - - -def findavgTime(processes, n, bt, at,wt,tat): - total_wt = 0 - total_tat = 0 - compl_time=[0]*n - for i in range(n): - total_wt = total_wt + wt[i] - total_tat = total_tat + tat[i] - compl_time[i] = tat[i] + at[i] - print("Average waiting time = %.5f " % (total_wt / n)) - print("\nAverage turn around time = ", total_tat / n) - return compl_time - -print("Enter How Many Process You want To Enter") -n=int(input()) -burst_time=[0]*n -arrival_time=[0]*n -processes=list(range(1,n+1)) -print(processes) - - -for i in range(n): - print("Enter The Brust time and Arrival time for Process:--"+str(i+1)) - burst_time[i],arrival_time[i]=map(int,input().split()) +pd.set_option('display.max_rows', FCFS.shape[0]+1) +print(FCFS) + -wt=findWaitingTime(processes, n, burst_time, arrival_time) -tat= findTurnAroundTime(processes, n, burst_time, wt) -compl_time=findavgTime(processes, n, burst_time,arrival_time,wt,tat) - -FCFS=pd.DataFrame(list(zip(processes,burst_time,arrival_time,wt,tat,compl_time)),columns=['Processes','Burst Time','Arrival Time','Waiting Time','Turn Around Time','complition Time']) - -FCFS.head(n) From 84977e326fc7f7b70257a48fdbc37f7495b0dbd3 Mon Sep 17 00:00:00 2001 From: pharshil1902 <57032836+pharshil1902@users.noreply.github.com> Date: Thu, 7 May 2020 15:37:50 +0530 Subject: [PATCH 07/10] Added Shortest_Job_First Algorithm --- scheduling/Shortest_Job_First Algorithm.py | 45 +++++++++++----------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/scheduling/Shortest_Job_First Algorithm.py b/scheduling/Shortest_Job_First Algorithm.py index 1c2be96d083a..c5c74e20201b 100644 --- a/scheduling/Shortest_Job_First Algorithm.py +++ b/scheduling/Shortest_Job_First Algorithm.py @@ -109,29 +109,30 @@ def calculate_average_times(waiting_time,turn_around_time, no_of_processes): print("Average turn around time = ", total_turn_around_time / no_of_processes) +if __name__ == "__main__": + + print("Enter How Many Process You want To Enter") + no_of_processes=int(input()) + burst_time=[0]*no_of_processes + arrival_time=[0]*no_of_processes + processes=list(range(1,no_of_processes+1)) -print("Enter How Many Process You want To Enter") -no_of_processes=int(input()) -burst_time=[0]*no_of_processes -arrival_time=[0]*no_of_processes -processes=list(range(1,no_of_processes+1)) - -for i in range(no_of_processes): - print("Enter The Arrival time and Brust time for Process:--"+str(i+1)) - arrival_time[i], burst_time[i]=map(int,input().split()) -waiting_time=calculate_Waitingtime(arrival_time,burst_time,no_of_processes) -bt=burst_time -n=no_of_processes -wt=waiting_time -turn_around_time=calculate_TurnAroundTime(bt,n,wt) -calculate_average_times(waiting_time,turn_around_time, no_of_processes) -processes=list(range(1,no_of_processes+1)) -FCFS=pd.DataFrame(list(zip(processes,burst_time, -arrival_time,waiting_time,turn_around_time)), -columns=['Process','BurstTime','ArrivalTime','WaitingTime','TurnAroundTime']) + for i in range(no_of_processes): + print("Enter The Arrival time and Brust time for Process:--"+str(i+1)) + arrival_time[i], burst_time[i]=map(int,input().split()) + waiting_time=calculate_Waitingtime(arrival_time,burst_time,no_of_processes) + bt=burst_time + n=no_of_processes + wt=waiting_time + turn_around_time=calculate_TurnAroundTime(bt,n,wt) + calculate_average_times(waiting_time,turn_around_time, no_of_processes) + processes=list(range(1,no_of_processes+1)) + FCFS=pd.DataFrame(list(zip(processes,burst_time, + arrival_time,waiting_time,turn_around_time)), + columns=['Process','BurstTime','ArrivalTime','WaitingTime','TurnAroundTime']) -# Printing the dataFrame -pd.set_option('display.max_rows', FCFS.shape[0]+1) -print(FCFS) + # Printing the dataFrame + pd.set_option('display.max_rows', FCFS.shape[0]+1) + print(FCFS) From cb8eb74067ca19a15a9e980cf74b5bb0a1be5c07 Mon Sep 17 00:00:00 2001 From: pharshil1902 <57032836+pharshil1902@users.noreply.github.com> Date: Thu, 7 May 2020 17:53:54 +0530 Subject: [PATCH 08/10] Added Shortest Job First Algorithm --- ...thm.py => shortest_job_first_algorithm.py} | 27 +++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-) rename scheduling/{Shortest_Job_First Algorithm.py => shortest_job_first_algorithm.py} (85%) diff --git a/scheduling/Shortest_Job_First Algorithm.py b/scheduling/shortest_job_first_algorithm.py similarity index 85% rename from scheduling/Shortest_Job_First Algorithm.py rename to scheduling/shortest_job_first_algorithm.py index c5c74e20201b..d0cfdc5601f5 100644 --- a/scheduling/Shortest_Job_First Algorithm.py +++ b/scheduling/shortest_job_first_algorithm.py @@ -8,16 +8,16 @@ -def calculate_Waitingtime(arrival_time,burst_time,no_of_processes): +def calculate_waitingtime(arrival_time,burst_time,no_of_processes): """ This function calculates the Waiting Times of each Processes Return: list of Waiting Time. - >>> calculate_Waitingtime([1,2,3,4],[3,3,5,1],4) + >>> calculate_waitingtime([1,2,3,4],[3,3,5,1],4) [0, 3, 5, 0] - >>> calculate_Waitingtime([1,2,3],[2,5,1],3) + >>> calculate_waitingtime([1,2,3],[2,5,1],3) [0, 2, 0] - >>> calculate_Waitingtime([2,3],[5,1],2) + >>> calculate_waitingtime([2,3],[5,1],2) [1, 0] """ remaining_time=[0]*no_of_processes @@ -70,15 +70,15 @@ def calculate_Waitingtime(arrival_time,burst_time,no_of_processes): # Increment time increment_time += 1 return waiting_time -def calculate_TurnAroundTime(burst_time, no_of_processes, waiting_time): +def calculate_turnaroundtime(burst_time, no_of_processes, waiting_time): """ This function calculates the Turn Around Times of each Processes Return: list of Turn Around Time. - >>> calculate_TurnAroundTime([3,3,5,1], 4, [0,3,5,0]) + >>> calculate_turnaroundtime([3,3,5,1], 4, [0,3,5,0]) [3, 6, 10, 1] - >>> calculate_TurnAroundTime([3,3], 2, [0,3]) + >>> calculate_turnaroundtime([3,3], 2, [0,3]) [3, 6] - >>> calculate_TurnAroundTime([8,10,1], 3, [1,0,3]) + >>> calculate_turnaroundtime([8,10,1], 3, [1,0,3]) [9, 10, 4] """ turn_around_time=[0]*no_of_processes @@ -120,19 +120,18 @@ def calculate_average_times(waiting_time,turn_around_time, no_of_processes): for i in range(no_of_processes): print("Enter The Arrival time and Brust time for Process:--"+str(i+1)) arrival_time[i], burst_time[i]=map(int,input().split()) - waiting_time=calculate_Waitingtime(arrival_time,burst_time,no_of_processes) + waiting_time=calculate_waitingtime(arrival_time,burst_time,no_of_processes) bt=burst_time n=no_of_processes wt=waiting_time - turn_around_time=calculate_TurnAroundTime(bt,n,wt) + turn_around_time=calculate_turnaroundtime(bt,n,wt) calculate_average_times(waiting_time,turn_around_time, no_of_processes) processes=list(range(1,no_of_processes+1)) - FCFS=pd.DataFrame(list(zip(processes,burst_time, + fcfs=pd.DataFrame(list(zip(processes,burst_time, arrival_time,waiting_time,turn_around_time)), columns=['Process','BurstTime','ArrivalTime','WaitingTime','TurnAroundTime']) # Printing the dataFrame - pd.set_option('display.max_rows', FCFS.shape[0]+1) - print(FCFS) - + pd.set_option('display.max_rows', fcfs.shape[0]+1) + print(fcfs) From f35650b2ef98b1fd5f65f918134577391e6f893f Mon Sep 17 00:00:00 2001 From: pharshil1902 <57032836+pharshil1902@users.noreply.github.com> Date: Thu, 7 May 2020 22:29:59 +0530 Subject: [PATCH 09/10] Update shortest_job_first_algorithm.py --- scheduling/shortest_job_first_algorithm.py | 33 +++++++++++++--------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/scheduling/shortest_job_first_algorithm.py b/scheduling/shortest_job_first_algorithm.py index d0cfdc5601f5..4b716336d6f3 100644 --- a/scheduling/shortest_job_first_algorithm.py +++ b/scheduling/shortest_job_first_algorithm.py @@ -5,10 +5,12 @@ #time are inputted as space separated import pandas as pd - +from typing import List -def calculate_waitingtime(arrival_time,burst_time,no_of_processes): +def calculate_waitingtime( + arrival_time: List[int],burst_time: List[int],no_of_processes: int +) -> List[int]: """ This function calculates the Waiting Times of each Processes @@ -34,25 +36,25 @@ def calculate_waitingtime(arrival_time,burst_time,no_of_processes): # Process until all processes gets # completed - while (complete != no_of_processes): + while complete != no_of_processes: for j in range(no_of_processes): - if(arrival_time[j]<=increment_time): - if(remaining_time[j]>0): - if(remaining_time[j]0: + if remaining_time[j] List[int]: """ This function calculates the Turn Around Times of each Processes Return: list of Turn Around Time. @@ -85,7 +89,9 @@ def calculate_turnaroundtime(burst_time, no_of_processes, waiting_time): for i in range(no_of_processes): turn_around_time[i] = burst_time[i] + waiting_time[i] return turn_around_time -def calculate_average_times(waiting_time,turn_around_time, no_of_processes): +def calculate_average_times( + waiting_time: List[int],turn_around_time: List[int], no_of_processes: int +): """ This function calculates the average of the waiting & turnaround times Prints: Average Waiting time & Average Turn Around Time @@ -104,7 +110,7 @@ def calculate_average_times(waiting_time,turn_around_time, no_of_processes): for i in range(no_of_processes): total_waiting_time = total_waiting_time + waiting_time[i] total_turn_around_time = total_turn_around_time + turn_around_time[i] - print("\nAverage waiting time = %.5f "%( + print("Average waiting time = %.5f "%( total_waiting_time /no_of_processes) ) print("Average turn around time = ", total_turn_around_time / no_of_processes) @@ -134,4 +140,3 @@ def calculate_average_times(waiting_time,turn_around_time, no_of_processes): # Printing the dataFrame pd.set_option('display.max_rows', fcfs.shape[0]+1) print(fcfs) - From 121abce8522451c935d9cced63e65eb2f061cc5f Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 7 May 2020 19:27:29 +0200 Subject: [PATCH 10/10] Format code with psf/black --- scheduling/shortest_job_first_algorithm.py | 175 +++++++++++---------- 1 file changed, 91 insertions(+), 84 deletions(-) diff --git a/scheduling/shortest_job_first_algorithm.py b/scheduling/shortest_job_first_algorithm.py index 4b716336d6f3..18aadca0032f 100644 --- a/scheduling/shortest_job_first_algorithm.py +++ b/scheduling/shortest_job_first_algorithm.py @@ -1,20 +1,20 @@ - - -# Shortest job remainig first -# PLease note that arriaval time and burst -#time are inputted as space separated +""" +Shortest job remainig first +Please note arrival time and burst +Please use spaces to separate times entered. +""" import pandas as pd from typing import List - + def calculate_waitingtime( - arrival_time: List[int],burst_time: List[int],no_of_processes: int + arrival_time: List[int], burst_time: List[int], no_of_processes: int ) -> List[int]: - + """ - This function calculates the Waiting Times of each Processes - Return: list of Waiting Time. + Calculate the waiting time of each processes + Return: list of waiting times. >>> calculate_waitingtime([1,2,3,4],[3,3,5,1],4) [0, 3, 5, 0] >>> calculate_waitingtime([1,2,3],[2,5,1],3) @@ -22,62 +22,62 @@ def calculate_waitingtime( >>> calculate_waitingtime([2,3],[5,1],2) [1, 0] """ - remaining_time=[0]*no_of_processes - waiting_time=[0]*no_of_processes - # Copy the burst time into remaining_time[] - for i in range(no_of_processes): - remaining_time[i] =burst_time[i] - + remaining_time = [0] * no_of_processes + waiting_time = [0] * no_of_processes + # Copy the burst time into remaining_time[] + for i in range(no_of_processes): + remaining_time[i] = burst_time[i] + complete = 0 increment_time = 0 minm = 999999999 short = 0 check = False - # Process until all processes gets - # completed + # Process until all processes are completed while complete != no_of_processes: for j in range(no_of_processes): - if arrival_time[j]<=increment_time: - if remaining_time[j]>0: - if remaining_time[j] 0: + if remaining_time[j] < minm: + minm = remaining_time[j] + short = j + check = True + + if not check: increment_time += 1 continue - remaining_time[short]-=1 - - minm = remaining_time[short] - if minm == 0: + remaining_time[short] -= 1 + + minm = remaining_time[short] + if minm == 0: minm = 999999999 - - if remaining_time[short] == 0: + + if remaining_time[short] == 0: complete += 1 check = False - # Find finish time of current - # process + # Find finish time of current process finish_time = increment_time + 1 - # Calculate waiting time - finar=finish_time - arrival_time[short] - waiting_time[short] = (finar - burst_time[short]) + # Calculate waiting time + finar = finish_time - arrival_time[short] + waiting_time[short] = finar - burst_time[short] - if waiting_time[short] < 0: + if waiting_time[short] < 0: waiting_time[short] = 0 - - # Increment time + + # Increment time increment_time += 1 return waiting_time + + def calculate_turnaroundtime( burst_time: List[int], no_of_processes: int, waiting_time: List[int] -) -> List[int]: +) -> List[int]: """ - This function calculates the Turn Around Times of each Processes - Return: list of Turn Around Time. + Calculate the turn around time of each Processes + Return: list of turn around times. >>> calculate_turnaroundtime([3,3,5,1], 4, [0,3,5,0]) [3, 6, 10, 1] >>> calculate_turnaroundtime([3,3], 2, [0,3]) @@ -85,58 +85,65 @@ def calculate_turnaroundtime( >>> calculate_turnaroundtime([8,10,1], 3, [1,0,3]) [9, 10, 4] """ - turn_around_time=[0]*no_of_processes - for i in range(no_of_processes): - turn_around_time[i] = burst_time[i] + waiting_time[i] + turn_around_time = [0] * no_of_processes + for i in range(no_of_processes): + turn_around_time[i] = burst_time[i] + waiting_time[i] return turn_around_time + + def calculate_average_times( - waiting_time: List[int],turn_around_time: List[int], no_of_processes: int -): + waiting_time: List[int], turn_around_time: List[int], no_of_processes: int +): """ This function calculates the average of the waiting & turnaround times Prints: Average Waiting time & Average Turn Around Time >>> calculate_average_times([0,3,5,0],[3,6,10,1],4) - Average waiting time = 2.00000 - Average turn around time = 5.0 + Average waiting time = 2.00000 + Average turn around time = 5.0 >>> calculate_average_times([2,3],[3,6],2) - Average waiting time = 2.50000 - Average turn around time = 4.5 + Average waiting time = 2.50000 + Average turn around time = 4.5 >>> calculate_average_times([10,4,3],[2,7,6],3) - Average waiting time = 5.66667 - Average turn around time = 5.0 + Average waiting time = 5.66667 + Average turn around time = 5.0 """ total_waiting_time = 0 total_turn_around_time = 0 - for i in range(no_of_processes): - total_waiting_time = total_waiting_time + waiting_time[i] - total_turn_around_time = total_turn_around_time + turn_around_time[i] - print("Average waiting time = %.5f "%( - total_waiting_time /no_of_processes) ) - print("Average turn around time = ", - total_turn_around_time / no_of_processes) - + for i in range(no_of_processes): + total_waiting_time = total_waiting_time + waiting_time[i] + total_turn_around_time = total_turn_around_time + turn_around_time[i] + print("Average waiting time = %.5f" % (total_waiting_time / no_of_processes)) + print("Average turn around time =", total_turn_around_time / no_of_processes) + + if __name__ == "__main__": - - print("Enter How Many Process You want To Enter") - no_of_processes=int(input()) - burst_time=[0]*no_of_processes - arrival_time=[0]*no_of_processes - processes=list(range(1,no_of_processes+1)) + print("Enter how many process you want to analyze") + no_of_processes = int(input()) + burst_time = [0] * no_of_processes + arrival_time = [0] * no_of_processes + processes = list(range(1, no_of_processes + 1)) for i in range(no_of_processes): - print("Enter The Arrival time and Brust time for Process:--"+str(i+1)) - arrival_time[i], burst_time[i]=map(int,input().split()) - waiting_time=calculate_waitingtime(arrival_time,burst_time,no_of_processes) - bt=burst_time - n=no_of_processes - wt=waiting_time - turn_around_time=calculate_turnaroundtime(bt,n,wt) - calculate_average_times(waiting_time,turn_around_time, no_of_processes) - processes=list(range(1,no_of_processes+1)) - fcfs=pd.DataFrame(list(zip(processes,burst_time, - arrival_time,waiting_time,turn_around_time)), - columns=['Process','BurstTime','ArrivalTime','WaitingTime','TurnAroundTime']) - - # Printing the dataFrame - pd.set_option('display.max_rows', fcfs.shape[0]+1) - print(fcfs) + print("Enter the arrival time and brust time for process:--" + str(i + 1)) + arrival_time[i], burst_time[i] = map(int, input().split()) + waiting_time = calculate_waitingtime(arrival_time, burst_time, no_of_processes) + bt = burst_time + n = no_of_processes + wt = waiting_time + turn_around_time = calculate_turnaroundtime(bt, n, wt) + calculate_average_times(waiting_time, turn_around_time, no_of_processes) + processes = list(range(1, no_of_processes + 1)) + fcfs = pd.DataFrame( + list(zip(processes, burst_time, arrival_time, waiting_time, turn_around_time)), + columns=[ + "Process", + "BurstTime", + "ArrivalTime", + "WaitingTime", + "TurnAroundTime", + ], + ) + + # Printing the dataFrame + pd.set_option("display.max_rows", fcfs.shape[0] + 1) + print(fcfs)