To calculate the average waiting time using the First-Come, First-Served (FCFS) scheduling algorithm, we need to simulate the execution of the processes, taking into account both CPU and I/O operations. We assume a single CPU and a single I/O device (disk). Processes are scheduled on the CPU in FCFS order, and similarly for the I/O device. Waiting time is accumulated when a process is ready to use the CPU but the CPU is busy with another process.
Let's track the state of the system and each process:
cpu_free_at: The time when the CPU becomes available.
io_free_at: The time when the I/O device becomes available.
process_ready_for_cpu_at[P_i]: The time when process P_i finishes its I/O or arrives, and is ready for its next CPU burst.
waiting_time[P_i]: The total time P_i spends waiting for the CPU.
Initially, all processes arrive at time 0 and are ready for their first CPU burst.
cpu_free_at = 0
io_free_at = 0
process_ready_for_cpu_at = {P1: 0, P2: 0, P3: 0, P4: 0}
waiting_time = {P1: 0, P2: 0, P3: 0, P4: 0}
We process the tasks in FCFS order (P1, P2, P3, P4).
1. Process P1: (3 CPU, 7 I/O, 2 CPU, 1 I/O, 1 CPU)
- 1st CPU Burst (3 units):
- P1 is ready for CPU at
process_ready_for_cpu_at[P1] = 0.
- CPU becomes free at
cpu_free_at = 0.
- P1 starts CPU at
max(0, 0) = 0.
- Waiting time for P1:
0 - 0 = 0.
- P1 finishes CPU burst at
0 + 3 = 3.
cpu_free_at = 3.
- P1 then requests I/O.
- 1st I/O Burst (7 units):
- I/O becomes free at
io_free_at = 0.
- P1 requests I/O at
3.
- P1 starts I/O at
max(0, 3) = 3.
- P1 finishes I/O burst at
3 + 7 = 10.
io_free_at = 10.
- P1 is ready for its next CPU burst at
10. process_ready_for_cpu_at[P1] = 10.
2. Process P2: (4 CPU, 2 I/O, 3 CPU, 1 I/O, 1 CPU) - Next in FCFS for CPU
- 1st CPU Burst (4 units):
- P2 is ready for CPU at
process_ready_for_cpu_at[P2] = 0.
- CPU becomes free at
cpu_free_at = 3.
- P2 starts CPU at
max(3, 0) = 3.
- Waiting time for P2:
3 - 0 = 3. (waiting_time[P2] = 3)
- P2 finishes CPU burst at
3 + 4 = 7.
cpu_free_at = 7.
- P2 then requests I/O.
- 1st I/O Burst (2 units):
- I/O becomes free at
io_free_at = 10 (from P1).
- P2 requests I/O at
7.
- P2 starts I/O at
max(10, 7) = 10. (P2 waits 3 units for I/O).
- P2 finishes I/O burst at
10 + 2 = 12.
io_free_at = 12.
- P2 is ready for its next CPU burst at
12. process_ready_for_cpu_at[P2] = 12.
3. Process P3: (2 CPU, 3 I/O, 2 CPU) - Next in FCFS for CPU
- 1st CPU Burst (2 units):
- P3 is ready for CPU at
process_ready_for_cpu_at[P3] = 0.
- CPU becomes free at
cpu_free_at = 7.
- P3 starts CPU at
max(7, 0) = 7.
- Waiting time for P3:
7 - 0 = 7. (waiting_time[P3] = 7)
- P3 finishes CPU burst at
7 + 2 = 9.
cpu_free_at = 9.
- P3 then requests I/O.
- 1st I/O Burst (3 units):
- I/O becomes free at
io_free_at = 12 (from P2).
- P3 requests I/O at
9.
- P3 starts I/O at
max(12, 9) = 12. (P3 waits 3 units for I/O).
- P3 finishes I/O burst at
12 + 3 = 15.
io_free_at = 15.
- P3 is ready for its next CPU burst at
15. process_ready_for_cpu_at[P3] = 15.
4. Process P4: (7 CPU) - Next in FCFS for CPU
- 1st CPU Burst (7 units):
- P4 is ready for CPU at
process_ready_for_cpu_at[P4] = 0.
- CPU becomes free at
cpu_free_at = 9.
- P4 starts CPU at
max(9, 0) = 9.
- Waiting time for P4:
9 - 0 = 9. (waiting_time[P4] = 9)
- P4 finishes CPU burst at
9 + 7 = 16.
cpu_free_at = 16.
- P4 is done.
Processes returning from I/O:
At this point, the CPU is free at 16.
P1 is ready for CPU at 10.
P2 is ready for CPU at 12.
P3 is ready for CPU at 15.
The FCFS order for the CPU from this point is P1, then P2, then P3 (based on when they became ready).
5. Process P1 (continued): (2 CPU, 1 I/O, 1 CPU) - P1 became ready for CPU earliest (at 10)
- 2nd CPU Burst (2 units):
- P1 is ready for CPU at
process_ready_for_cpu_at[P1] = 10.
- CPU becomes free at
cpu_free_at = 16.
- P1 starts CPU at
max(16, 10) = 16.
- Waiting time for P1:
16 - 10 = 6. (waiting_time[P1] = 0 + 6 = 6)
- P1 finishes CPU burst at
16 + 2 = 18.
cpu_free_at = 18.
- P1 then requests I/O.
- 2nd I/O Burst (1 unit):
- I/O becomes free at
io_free_at = 15 (from P3).
- P1 requests I/O at
18.
- P1 starts I/O at
max(15, 18) = 18.
- P1 finishes I/O burst at
18 + 1 = 19.
io_free_at = 19.
- P1 is ready for its next CPU burst at
19. process_ready_for_cpu_at[P1] = 19.
6. Process P2 (continued): (3 CPU, 1 I/O, 1 CPU) - P2 became ready for CPU next (at 12)
- 2nd CPU Burst (3 units):
- P2 is ready for CPU at
process_ready_for_cpu_at[P2] = 12.
- CPU becomes free at
cpu_free_at = 18.
- P2 starts CPU at
max(18, 12) = 18.
- Waiting time for P2:
18 - 12 = 6. (waiting_time[P2] = 3 + 6 = 9)
- P2 finishes CPU burst at
18 + 3 = 21.
cpu_free_at = 21.
- P2 then requests I/O.
- 2nd I/O Burst (1 unit):
- I/O becomes free at
io_free_at = 19 (from P1).
- P2 requests I/O at
21.
- P2 starts I/O at
max(19, 21) = 21.
- P2 finishes I/O burst at
21 + 1 = 22.
io_free_at = 22.
- P2 is ready for its next CPU burst at
22. process_ready_for_cpu_at[P2] = 22.
7. Process P3 (continued): (2 CPU) - P3 became ready for CPU last (at 15)
- 2nd CPU Burst (2 units):
- P3 is ready for CPU at
process_ready_for_cpu_at[P3] = 15.
- CPU becomes free at
cpu_free_at = 21.
- P3 starts CPU at
max(21, 15) = 21.
- Waiting time for P3:
21 - 15 = 6. (waiting_time[P3] = 7 + 6 = 13)
- P3 finishes CPU burst at
21 + 2 = 23.
cpu_free_at = 23.
- P3 is done.
8. Process P1 (continued): (1 CPU) - P1 became ready for CPU earliest (at 19) among remaining
- 3rd CPU Burst (1 unit):
- P1 is ready for CPU at
process_ready_for_cpu_at[P1] = 19.
- CPU becomes free at
cpu_free_at = 23.
- P1 starts CPU at
max(23, 19) = 23.
- Waiting time for P1:
23 - 19 = 4. (waiting_time[P1] = 6 + 4 = 10)
- P1 finishes CPU burst at
23 + 1 = 24.
cpu_free_at = 24.
- P1 is done.
9. Process P2 (continued): (1 CPU) - P2 became ready for CPU (at 22) among remaining
- 3rd CPU Burst (1 unit):
- P2 is ready for CPU at
process_ready_for_cpu_at[P2] = 22.
- CPU becomes free at
cpu_free_at = 24.
- P2 starts CPU at
max(24, 22) = 24.
- Waiting time for P2:
24 - 22 = 2. (waiting_time[P2] = 9 + 2 = 11)
- P2 finishes CPU burst at
24 + 1 = 25.
cpu_free_at = 25.
- P2 is done.
Total Waiting Times:
- P1: 10 units
- P2: 11 units
- P3: 13 units
- P4: 9 units
Sum of Waiting Times:
10+11+13+9=43 units
Average Waiting Time:
Average Waiting Time = (Sum of Waiting Times) / (Number of Processes)
Average Waiting Time = 43/4=10.75 units
The average waiting time using FCFS is 10.75 units.