Python Shortest Job First (Preemptive) Algorithm with Same Arrival Time

Python Shortest Job First (Preemptive) Algorithm with Same Arrival Time

SJF: Shortest Job First
The task is to find the Average Waiting Time and Average Turnaround Time of the given processes with their Burst Time using SJF Scheduling Algorithm.
SJF is a scheduling policy that selects the waiting process with the smallest execution time to execute next.
Priority Scheduling is a Non Pre-emptive and Pre-emptive Algorithm, hence the process which has the Least Burst Time is selected first.
Here we are considering Pre-emptive version of Priority Scheduling, hence the process which has the Least Burst Time will be served first and will be continued to be served till there is any other process with Lower Burst Time priority.
If there is any process with Lower Burst Time, then switch the process.
We are considering the arrival time of all processes to be 0.
Start Time: Time at which the execution of the process starts
Completion Time: Time at which the process completes its execution
Turnaround Time: Completion Time - Arrival Time
Waiting Time: Turnaround Time - Burst Time

class SJF:

    def processData(self, no_of_processes):
        process_data = []
        for i in range(no_of_processes):
            temporary = []
            process_id = int(input("Enter Process ID: "))
            burst_time = int(input(f"Enter Burst Time for Process {process_id}: "))
            temporary.extend([process_id, 0, burst_time, 0, burst_time])
            '0' is the state of the process. 0 means not executed and 1 means execution complete
        SJF.schedulingProcess(self, process_data)

    def schedulingProcess(self, process_data):
        start_time = []
        exit_time = []
        s_time = 0
        sequence_of_process = []
        while 1:
            ready_queue = []
            temp = []
            for i in range(len(process_data)):
                if process_data[i][1] <= s_time and process_data[i][3] == 0:
                    temp.extend([process_data[i][0], process_data[i][1], process_data[i][2], process_data[i][4]])
                    temp = []
            if len(ready_queue) == 0:
            if len(ready_queue) != 0:
                ready_queue.sort(key=lambda x: x[2])
                Sort processes according to Burst Time
                s_time = s_time + 1
                e_time = s_time
                for k in range(len(process_data)):
                    if process_data[k][0] == ready_queue[0][0]:
                process_data[k][2] = process_data[k][2] - 1
                if process_data[k][2] == 0:        #If Burst Time of a process is 0, it means the process is completed
                    process_data[k][3] = 1
        t_time = SJF.calculateTurnaroundTime(self, process_data)
        w_time = SJF.calculateWaitingTime(self, process_data)
        SJF.printData(self, process_data, t_time, w_time, sequence_of_process)

    def calculateTurnaroundTime(self, process_data):
        total_turnaround_time = 0
        for i in range(len(process_data)):
            turnaround_time = process_data[i][5] - process_data[i][1]
            turnaround_time = completion_time - arrival_time
            total_turnaround_time = total_turnaround_time + turnaround_time
        average_turnaround_time = total_turnaround_time / len(process_data)
        average_turnaround_time = total_turnaround_time / no_of_processes
        return average_turnaround_time

    def calculateWaitingTime(self, process_data):
        total_waiting_time = 0
        for i in range(len(process_data)):
            waiting_time = process_data[i][6] - process_data[i][4]
            waiting_time = turnaround_time - burst_time
            total_waiting_time = total_waiting_time + waiting_time
        average_waiting_time = total_waiting_time / len(process_data)
        average_waiting_time = total_waiting_time / no_of_processes
        return average_waiting_time

    def printData(self, process_data, average_turnaround_time, average_waiting_time, sequence_of_process):
        process_data.sort(key=lambda x: x[0])
        Sort processes according to the Process ID
        print("Process_ID  Arrival_Time  Rem_Burst_Time      Completed  Orig_Burst_Time Completion_Time  Turnaround_Time  Waiting_Time")
        for i in range(len(process_data)):
            for j in range(len(process_data[i])):
                print(process_data[i][j], end="\t\t\t\t")
        print(f'Average Turnaround Time: {average_turnaround_time}')
        print(f'Average Waiting Time: {average_waiting_time}')
        print(f'Sequence of Process: {sequence_of_process}')

if __name__ == "__main__":
    no_of_processes = int(input("Enter number of processes: "))
    sjf = SJF()