Calculate the waiting time of each processes Return: List of waiting times. >>> calculate_waitingtime([1,2,3,4],[3,3,5,1],4) [0, 3, 5, 0] >>> calculate_waitingtime([1,2,3],[2,5,1],3) [0, 2, 0] >>> calculate_waitingtime([2,3],[5,1],2) [1, 0]
(
arrival_time: list[int], burst_time: list[int], no_of_processes: int
)
| 10 | |
| 11 | |
| 12 | def calculate_waitingtime( |
| 13 | arrival_time: list[int], burst_time: list[int], no_of_processes: int |
| 14 | ) -> list[int]: |
| 15 | """ |
| 16 | Calculate the waiting time of each processes |
| 17 | Return: List of waiting times. |
| 18 | >>> calculate_waitingtime([1,2,3,4],[3,3,5,1],4) |
| 19 | [0, 3, 5, 0] |
| 20 | >>> calculate_waitingtime([1,2,3],[2,5,1],3) |
| 21 | [0, 2, 0] |
| 22 | >>> calculate_waitingtime([2,3],[5,1],2) |
| 23 | [1, 0] |
| 24 | """ |
| 25 | remaining_time = [0] * no_of_processes |
| 26 | waiting_time = [0] * no_of_processes |
| 27 | # Copy the burst time into remaining_time[] |
| 28 | for i in range(no_of_processes): |
| 29 | remaining_time[i] = burst_time[i] |
| 30 | |
| 31 | complete = 0 |
| 32 | increment_time = 0 |
| 33 | minm = 999999999 |
| 34 | short = 0 |
| 35 | check = False |
| 36 | |
| 37 | # Process until all processes are completed |
| 38 | while complete != no_of_processes: |
| 39 | for j in range(no_of_processes): |
| 40 | if ( |
| 41 | arrival_time[j] <= increment_time |
| 42 | and remaining_time[j] > 0 |
| 43 | and remaining_time[j] < minm |
| 44 | ): |
| 45 | minm = remaining_time[j] |
| 46 | short = j |
| 47 | check = True |
| 48 | |
| 49 | if not check: |
| 50 | increment_time += 1 |
| 51 | continue |
| 52 | remaining_time[short] -= 1 |
| 53 | |
| 54 | minm = remaining_time[short] |
| 55 | if minm == 0: |
| 56 | minm = 999999999 |
| 57 | |
| 58 | if remaining_time[short] == 0: |
| 59 | complete += 1 |
| 60 | check = False |
| 61 | |
| 62 | # Find finish time of current process |
| 63 | finish_time = increment_time + 1 |
| 64 | |
| 65 | # Calculate waiting time |
| 66 | finar = finish_time - arrival_time[short] |
| 67 | waiting_time[short] = finar - burst_time[short] |
| 68 | |
| 69 | waiting_time[short] = max(waiting_time[short], 0) |