Explanation
Here is an explanation of the FCFS scheduling program:
1. Purpose
This program simulates the First-Come, First-Served (FCFS) CPU scheduling algorithm (assuming all processes arrive at time 0). It calculates the Waiting Time (WT) and Turnaround Time (TAT) for each process, along with their overall averages.
2. Key Concepts
- Input Parsing: Using
split(',')andmap(int, ...)to convert a comma-separated string into a list of integers. - List Initialization: Using
[0] * nto pre-allocate lists of a fixed size. - Cumulative Sums: Iterating with
range()to accumulate burst times sequentially. - Built-in Functions: Using
sum()andlen()/nto calculate averages.
3. Code Walkthrough
-
Input & Initialization (
lines 1–4):- Prompts the user for process count (
n) and their CPU burst times (bt). - Initializes two lists,
wt(waiting time) andtat(turnaround time), with zeros.
- Prompts the user for process count (
-
Calculate Waiting Time (
lines 5–6):- The first process always waits
0units. - Each subsequent process $i$ waits for the time the previous process spent waiting plus its burst time:
$$\text{WT}[i] = \text{WT}[i-1] + \text{BT}[i-1]$$
- The first process always waits
-
Calculate Turnaround Time (
lines 7–8):- Total time spent from arrival to completion:
$$\text{TAT}[i] = \text{WT}[i] + \text{BT}[i]$$
- Total time spent from arrival to completion:
-
Display Results (
lines 9–14):- Prints a tab-delimited (
\t) table listing Process ID, Burst Time, Waiting Time, and Turnaround Time. - Uses
sum(wt)/nandsum(tat)/nto display the average WT and TAT.
- Prints a tab-delimited (
4. Output
Example Run:
Enter number of processes: 3
Enter burst times separated by comma ',': 4,3,2
P bt wt tat
1 4 0 4
2 3 4 7
3 2 7 9
Avg WT = 3.6666666666666665
Avg TAT = 6.666666666666667