algorithm to compute waiting time for FCFS scheduling in python

前端 未结 3 1317
庸人自扰
庸人自扰 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: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)
    

提交回复
热议问题