0
0
Operating-systemsHow-ToBeginner ยท 3 min read

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 Time is when the process enters the ready queue.
  • Burst Time is the total time required by the process for execution.
  • Completion Time is 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 time with turnaround time.
  • Not accounting for the arrival time properly, especially if processes arrive at different times.
  • Using completion time - burst time directly without subtracting arrival 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

TermDefinition
Arrival TimeTime when process enters the ready queue
Burst TimeTotal CPU time required by the process
Completion TimeTime when process finishes execution
Turnaround TimeCompletion Time - Arrival Time
Waiting TimeTurnaround 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.