Quantcast
Channel: Active questions tagged operating-system+scheduling - Stack Overflow
Viewing all articles
Browse latest Browse all 211

Issue with same priority input in Non-Pre-Emptive Priority Scheduling in python

$
0
0

My code is running successfully when the input priorities are unique but when we enter same priority then the output gets messed up.

As I am implementing using lists and checked and compared using the priority but in case of same priority i wish to compare their arrival time or burst time...

How should i do it ...

i approached by making a customsort function but it is giving no output...

class Priority:"""def custom_sort(ready_queue):        temp1 = []        temp2 = []        myans = []        ready_queuxe.sort(key=lambda x: x[3])        for i in range(len(ready_queue)):            for j in range(len(ready_queue)-i):                if ((i+j+1)<len(ready_queue) and (ready_queue[i][3]==ready_queue[i+j+1][3]) ):                    temp1.append(ready_queue[i:i+j+1])                    temp2.append(ready_queue[:i])                    temp1.sort(key=lambda x: x[2])                    temp2.append(temp1)                    myans.append(temp2)                    j=j+1                else:                    break        if(len(temp2)>0):            return myans        else:            return ready_queue"""    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}: "))            priority_id = int(input(f"Enter Priority for Process {process_id}: "))            arrival_time = int(input(f"Enter Arrival Time for Process {process_id}: "))            temporary.extend([process_id, arrival_time, burst_time, priority_id,0])            process_data.append(temporary)        Priority.schedulingProcess(self, process_data)    def schedulingProcess(self, process_data):        start_time = []        exit_time = []        s_time = 0        process_data.sort(key=lambda x: x[1])        for i in range(len(process_data)):            ready_queue = []            temp = []            normal_queue = []            for j in range(len(process_data)):                if (process_data[j][1] <= s_time) and (process_data[j][4] == 0):                    temp.extend([process_data[j][0], process_data[j][1], process_data[j][2], process_data[j][3]])                    ready_queue.append(temp)                    temp = []                elif process_data[j][4] == 0:                    temp.extend([process_data[j][0], process_data[j][1], process_data[j][2], process_data[j][3]])                    normal_queue.append(temp)                    temp = []            if len(ready_queue) != 0:"""ready_queue = Priority.custom_sort(ready_queue)"""                ready_queue.sort(key=lambda x: x[3])                start_time.append(s_time)                s_time = s_time + ready_queue[0][2]                e_time = s_time                exit_time.append(e_time)                for k in range(len(process_data)):                    if process_data[k][0] == ready_queue[0][0]:                        break                process_data[k][4] = 1                process_data[k].append(e_time)            elif len(ready_queue) == 0:                if s_time < normal_queue[0][1]:                    s_time = normal_queue[0][1]                start_time.append(s_time)                s_time = s_time + normal_queue[0][2]                e_time = s_time                exit_time.append(e_time)                for k in range(len(process_data)):                    if process_data[k][0] == normal_queue[0][0]:                        break                process_data[k][4] = 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)    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]            total_turnaround_time = total_turnaround_time + turnaround_time            process_data[i].append(turnaround_time)        average_turnaround_time = total_turnaround_time / len(process_data)        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][5] - process_data[i][1] - process_data[i][2]            total_waiting_time = total_waiting_time + waiting_time            process_data[i].append(waiting_time)        average_waiting_time = total_waiting_time / len(process_data)        return average_waiting_time    def printData(self, process_data, average_turnaround_time, average_waiting_time):        process_data.sort(key=lambda x: x[0])        print("P_ID    Arrival_Time    Burst_Time     Priority    Completed      Completion_Time    Turnaround_Time    Waiting ")        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}')if __name__ == "__main__":    no_of_processes = int(input("Enter number of processes: "))    prit = Priority()    prit.processData(no_of_processes)

`


Viewing all articles
Browse latest Browse all 211

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>