algorithm to compute waiting time for FCFS scheduling in python

前端 未结 3 1318
庸人自扰
庸人自扰 2021-01-29 10:17

Consider the following code that takes input processes and their arrival times and sort them according to FCFS algorithm, so i\'ve been thinking about algorithms to compute avg

相关标签:
3条回答
  • 2021-01-29 10:31
    # Author:Geraldo Braho
    process_queue = []
    total_wtime = 0
    n = int(raw_input('Enter the total numbers of processes: '))
    for i in xrange(n):
        process_queue.append([])#append a list object to the list
        process_queue[i].append(raw_input('Enter process name: '))
        process_queue[i].append(int(raw_input('Enter process arrival Time : ')))
        total_wtime += process_queue[i][1]
        process_queue[i].append(int(raw_input('Enter Process CPU bustTime : ')))
        print ''
    
    process_queue.sort(key = lambda process_queue:process_queue[1])
    
    print 'ProcessName\tArrivalTime\tBurstTime'
    for i in xrange(n):
        print process_queue[i][0],'\t\t',process_queue[i][1],'\t\t',process_queue[i][2]
    
    print 'Total waiting time: ',total_wtime
    print 'Average waiting time: ',(total_wtime/n)
    
    0 讨论(0)
  • 2021-01-29 10:46

    Concept: https://www.geeksforgeeks.org/program-fcfs-scheduling-set-2-processes-different-arrival-time/

    processes = [
        {"pid": "P0", "burst_time": 5, "arrival_time": 0, "service_time": None, "waiting_time": None},
        {"pid": "P1", "burst_time": 3, "arrival_time": 1, "service_time": None, "waiting_time": None},
        {"pid": "P2", "burst_time": 8, "arrival_time": 2, "service_time": None, "waiting_time": None},
        {"pid": "P3", "burst_time": 6, "arrival_time": 3, "service_time": None, "waiting_time": None}
    ]
    
    # No waiting time for first process
    processes[0]["waiting_time"] = 0
    # Service_time of the first process is it's arrival time
    processes[0]["service_time"] = processes[0]["arrival_time"]
    
    for i in range(1, len(processes)):
        # Case where process comes when processor is in idle state
        if processes[i]["arrival_time"] > (processes[i - 1]["service_time"] + processes[i - 1]["burst_time"]):
            processes[i]["service_time"] = processes[i]["arrival_time"]
        else:
            processes[i]["service_time"] = processes[i - 1]["service_time"] + processes[i - 1]["burst_time"]
    
        processes[i]["waiting_time"] = processes[i]["service_time"] - processes[i]["arrival_time"]
    
    total_waiting_time = 0
    
    for process in processes:
        total_waiting_time += process["waiting_time"]
    
    avg_waiting_time = total_waiting_time / len(processes)
    
    print("Average Waiting Time:", avg_waiting_time)
    
    0 讨论(0)
  • 2021-01-29 10:54

    process_queue[i].append(int(list1[i])

    you are appending the i'th element of list1 to the process_queue list , You should note that list1[i] is not a list but an integer . and in next line you are trying to access

    total_wtime += process_queue[i][1]

    1st element of the process_queue[i] but it is not a 2d array

    total_wtime += process_queue[i]

    should work .

    0 讨论(0)
提交回复
热议问题