How to Calculate Waiting Time in Operating Systems
To calculate
waiting time in operating systems, subtract the burst time from the turnaround time of a process. The formula is: Waiting Time = Turnaround Time - Burst Time, where Turnaround Time = Completion Time - Arrival Time.Syntax
The waiting time for a process can be calculated using these formulas:
- Turnaround Time = Completion Time - Arrival Time
- Waiting Time = Turnaround Time - Burst Time
Where:
Arrival Timeis when the process enters the ready queue.Burst Timeis the total time required by the process for execution.Completion Timeis when the process finishes execution.
python
waiting_time = (completion_time - arrival_time) - burst_time
Example
This example shows how to calculate waiting time for multiple processes using their arrival, burst, and completion times.
python
processes = [
{'pid': 1, 'arrival_time': 0, 'burst_time': 5, 'completion_time': 8},
{'pid': 2, 'arrival_time': 1, 'burst_time': 3, 'completion_time': 10},
{'pid': 3, 'arrival_time': 2, 'burst_time': 8, 'completion_time': 18}
]
for p in processes:
turnaround_time = p['completion_time'] - p['arrival_time']
waiting_time = turnaround_time - p['burst_time']
print(f"Process {p['pid']} Waiting Time: {waiting_time}")Output
Process 1 Waiting Time: 3
Process 2 Waiting Time: 6
Process 3 Waiting Time: 8
Common Pitfalls
Common mistakes when calculating waiting time include:
- Confusing
waiting timewithturnaround time. - Not accounting for the
arrival timeproperly, especially if processes arrive at different times. - Using
completion time - burst timedirectly without subtractingarrival time.
Always remember to calculate turnaround time first, then subtract burst time to get waiting time.
python
wrong_waiting_time = completion_time - burst_time # Incorrect if arrival_time > 0 # Correct calculation: waiting_time = (completion_time - arrival_time) - burst_time
Quick Reference
| Term | Definition |
|---|---|
| Arrival Time | Time when process enters the ready queue |
| Burst Time | Total CPU time required by the process |
| Completion Time | Time when process finishes execution |
| Turnaround Time | Completion Time - Arrival Time |
| Waiting Time | Turnaround Time - Burst Time |
Key Takeaways
Calculate waiting time by subtracting burst time from turnaround time.
Turnaround time is completion time minus arrival time.
Always consider arrival time to avoid incorrect waiting time.
Waiting time shows how long a process waits before execution.
Use clear formulas to avoid confusion between related times.