|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "import multiprocessing\n", |
| 10 | + "import time\n", |
| 11 | + "\n", |
| 12 | + "def process_function(process_id, arrival_time, burst_time):\n", |
| 13 | + " print(f\"Process {process_id} started execution at time {time.time()}\")\n", |
| 14 | + " time.sleep(burst_time)\n", |
| 15 | + " print(f\"Process {process_id} completed execution at time {time.time()}\")\n", |
| 16 | + "\n", |
| 17 | + "\n", |
| 18 | + "def fcfs_scheduling(processes):\n", |
| 19 | + " # Sort the processes based on their arrival time (ascending order)\n", |
| 20 | + " # This ensures that processes with earlier arrival times are scheduled first.\n", |
| 21 | + " processes.sort(key=lambda p: p[1])\n", |
| 22 | + "\n", |
| 23 | + " # Initialize the starting time of the first process as its arrival time\n", |
| 24 | + " current_time = processes[0][1]\n", |
| 25 | + "\n", |
| 26 | + " # Initialize variables to keep track of total waiting time and turnaround time\n", |
| 27 | + " waiting_time_total = 0\n", |
| 28 | + " turnaround_time_total = 0\n", |
| 29 | + "\n", |
| 30 | + " # Print the table header for the process details\n", |
| 31 | + " print(\"Process ID\\tArrival Time\\tBurst Time\\tWaiting Time\\tTurnaround Time\")\n", |
| 32 | + "\n", |
| 33 | + " # Iterate through each process in the sorted order\n", |
| 34 | + " for process in processes:\n", |
| 35 | + " # Unpack the process details (process ID, arrival time, and burst time)\n", |
| 36 | + " pid, arrival_time, burst_time = process\n", |
| 37 | + "\n", |
| 38 | + " # Calculate the waiting time for the current process, ensuring it is non-negative\n", |
| 39 | + " waiting_time = max(0, current_time - arrival_time)\n", |
| 40 | + "\n", |
| 41 | + " # Calculate the turnaround time for the current process\n", |
| 42 | + " turnaround_time = waiting_time + burst_time\n", |
| 43 | + "\n", |
| 44 | + " # Update the total waiting time and turnaround time\n", |
| 45 | + " waiting_time_total += waiting_time\n", |
| 46 | + " turnaround_time_total += turnaround_time\n", |
| 47 | + "\n", |
| 48 | + " # Print the details for the current process\n", |
| 49 | + " print(f\"{pid}\\t\\t{arrival_time}\\t\\t{burst_time}\\t\\t{waiting_time}\\t\\t{turnaround_time}\")\n", |
| 50 | + "\n", |
| 51 | + "\n", |
| 52 | + " \n", |
| 53 | + " # Add the print statements for the process execution\n", |
| 54 | + " # print(f\"Process {pid} started execution at time {current_time}\")\n", |
| 55 | + " # Update the current time to the completion time of the current process\n", |
| 56 | + " current_time += burst_time\n", |
| 57 | + " # print(f\"Process {pid} completed execution at time {current_time}\")\n", |
| 58 | + "\n", |
| 59 | + "\n", |
| 60 | + " # Calculate the average waiting time and turnaround time for all processes\n", |
| 61 | + " n = len(processes)\n", |
| 62 | + " avg_waiting_time = waiting_time_total / n\n", |
| 63 | + " avg_turnaround_time = turnaround_time_total / n\n", |
| 64 | + "\n", |
| 65 | + " # Print the average waiting time and average turnaround time\n", |
| 66 | + " print(f\"\\nAverage Waiting Time: {avg_waiting_time:.2f}\")\n", |
| 67 | + " print(f\"Average Turnaround Time: {avg_turnaround_time:.2f}\")\n", |
| 68 | + "\n", |
| 69 | + "if __name__ == \"__main__\":\n", |
| 70 | + " # Example usage with user input for the number of processes\n", |
| 71 | + " n = int(input(\"Enter the number of processes: \"))\n", |
| 72 | + " processes = []\n", |
| 73 | + "\n", |
| 74 | + " # Prompt the user to enter arrival time and burst time for each process\n", |
| 75 | + " print(\"\\nEnter Process Arrival Time and Burst Time\")\n", |
| 76 | + " for i in range(n):\n", |
| 77 | + " arrival_time = float(input(f\"P[{i+1}] Arrival Time: \"))\n", |
| 78 | + " burst_time = float(input(f\"P[{i+1}] Burst Time: \"))\n", |
| 79 | + " processes.append((i+1, arrival_time, burst_time))\n", |
| 80 | + " \n", |
| 81 | + " \n", |
| 82 | + " jobs = []\n", |
| 83 | + " for process in processes:\n", |
| 84 | + " process_id, arrival_time, burst_time = process\n", |
| 85 | + " p = multiprocessing.Process(target=process_function, args=(process_id, arrival_time, burst_time))\n", |
| 86 | + " jobs.append(p)\n", |
| 87 | + " p.start()\n", |
| 88 | + "\n", |
| 89 | + " for job in jobs:\n", |
| 90 | + " job.join()\n", |
| 91 | + "\n", |
| 92 | + " print(\"All processes completed execution.\")\n", |
| 93 | + "\n", |
| 94 | + " # Call the FCFS scheduling function with the list of processes\n", |
| 95 | + " fcfs_scheduling(processes)\n", |
| 96 | + "\n", |
| 97 | + "\n", |
| 98 | + "\n" |
| 99 | + ] |
| 100 | + } |
| 101 | + ], |
| 102 | + "metadata": { |
| 103 | + "language_info": { |
| 104 | + "name": "python" |
| 105 | + }, |
| 106 | + "orig_nbformat": 4 |
| 107 | + }, |
| 108 | + "nbformat": 4, |
| 109 | + "nbformat_minor": 2 |
| 110 | +} |
0 commit comments