 ### Python Priority Scheduling (Preemeptive) Algorithm with Different Arrival Time

`'''The task is to find the Average Waiting Time and Average Turnaround Time of the given processes with their Burst Time using Priority Scheduling Algorithm.Priority is a scheduling policy that selects the waiting process with the highest priority to execute next.Priority Scheduling is a Non Pre-emptive and Pre-emptive Algorithm, hence the process which has the Highest Priority is selected first.Here we are considering Pre-emptive version of Priority Scheduling, hence the process which has the Highest Priority will be served first and will be continued to be served till there is any other process with higher priority.If there is any process with higher priority, then switch the process.Start Time: Time at which the execution of the process startsCompletion Time: Time at which the process completes its executionTurnaround Time: Completion Time - Arrival TimeWaiting Time: Turnaround Time - Burst TimeI have made use of 2 queues in the code:Ready Queue: It stores all the processes which have already arrived.Normal Queue: It stores all the processes which have not arrived yet.'''`
` `
`class Priority:    def processData(self, no_of_processes):        process_data = []        for i in range(no_of_processes):            temporary = []            process_id = int(input("Enter Process ID: "))`
`            arrival_time = int(input(f"Enter Arrival Time for Process {process_id}: "))`
`            burst_time = int(input(f"Enter Burst Time for Process {process_id}: "))`
`            priority = int(input(f"Enter Priority for Process {process_id}: "))`
`            temporary.extend([process_id, arrival_time, burst_time, priority, 0, burst_time])            '''            '0' is the state of the process. 0 means not executed and 1 means execution complete            '''            process_data.append(temporary)        Priority.schedulingProcess(self, process_data)    def schedulingProcess(self, process_data):        start_time = []        exit_time = []        s_time = 0        sequence_of_process = []        process_data.sort(key=lambda x: x)        '''        Sort processes according to the Arrival Time        '''        while 1:            ready_queue = []            normal_queue = []            temp = []            for i in range(len(process_data)):                if process_data[i] <= s_time and process_data[i] == 0:                    temp.extend([process_data[i], process_data[i], process_data[i], process_data[i],                                 process_data[i]])                    ready_queue.append(temp)                    temp = []                elif process_data[i] == 0:                    temp.extend([process_data[i], process_data[i], process_data[i], process_data[i],                                 process_data[i]])                    normal_queue.append(temp)                    temp = []            if len(ready_queue) == 0 and len(normal_queue) == 0:                break            if len(ready_queue) != 0:                ready_queue.sort(key=lambda x: x, reverse=True)                start_time.append(s_time)                s_time = s_time + 1                e_time = s_time                exit_time.append(e_time)                sequence_of_process.append(ready_queue)                for k in range(len(process_data)):                    if process_data[k] == ready_queue:                        break                process_data[k] = process_data[k] - 1                if process_data[k] == 0:       #if burst time is zero, it means process is completed                    process_data[k] = 1                    process_data[k].append(e_time)            if len(ready_queue) == 0:                normal_queue.sort(key=lambda x: x)                if s_time < normal_queue:                    s_time = normal_queue                start_time.append(s_time)                s_time = s_time + 1`
`           e_time = s_time                exit_time.append(e_time)                sequence_of_process.append(normal_queue)                for k in range(len(process_data)):                    if process_data[k] == normal_queue:                        break                process_data[k] = process_data[k] - 1                if process_data[k] == 0:        #if burst time is zero, it means process is completed                    process_data[k] = 1                    process_data[k].append(e_time)        t_time = Priority.calculateTurnaroundTime(self, process_data)        w_time = Priority.calculateWaitingTime(self, process_data)        Priority.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] - process_data[i]            '''            turnaround_time = completion_time - arrival_time            '''            total_turnaround_time = total_turnaround_time + turnaround_time            process_data[i].append(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] - process_data[i]            '''            waiting_time = turnaround_time - burst_time            '''            total_waiting_time = total_waiting_time + waiting_time            process_data[i].append(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)        '''        Sort processes according to the Process ID        '''        print("Process_ID  Arrival_Time  Rem_Burst_Time   Priority        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="				")            print()`
`        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: "))    priority = Priority()    priority.processData(no_of_processes)`

`Output:`

` ` 