FCFS Implementation with explaination
FCFS Implementation with explaination
non-preemptive CPU scheduling algorithm used in operating systems. The program calculates and
displays the waiting time (wt) and turnaround time (tt) for each process, and also computes the
average waiting time and average turnaround time for all processes.
Structure Definition
struct process
int bt; // Burst Time (the time the process requires for execution)
int wt; // Waiting Time (time a process has to wait before execution starts)
int tt; // Turnaround Time (total time spent by the process from arrival to completion)
Main Function
int main(){
The program asks the user to input the burst time for each process. The pid is automatically assigned as
the loop index, starting from 1.
p[1].tt = p[1].bt + p[1].wt; // Turnaround time = burst time + waiting time (for the first process,
waiting time = 0)
i = 2;
p[i].wt = p[i-1].bt + p[i-1].wt; // Waiting time for process i = waiting time of previous process +
burst time of previous process
i++;
}
First Process: The waiting time for the first process is 0 because it starts execution
immediately.
Subsequent Processes: For each subsequent process, the waiting time is calculated as the
sum of the burst time of all previous processes. The turnaround time is simply the sum
of burst time and waiting time for each process.
i = 1;
totwt = tottt = 0; // Initialize total waiting time and total turnaround time
printf("\n\t%d \t%d \t%d \t%d", p[i].pid, p[i].bt, p[i].wt, p[i].tt); // Print each process's PID,
burst time, waiting time, and turnaround time
totwt = p[i].wt + totwt; // Add waiting time of the current process to total waiting time
tottt = p[i].tt + tottt; // Add turnaround time of the current process to total turnaround time
i++;
return 0;
The program prints the process ID, burst time, waiting time, and turnaround time for
each process.
It also calculates the total waiting time (totwt) and total turnaround time (tottt).
The averages are calculated by dividing the total waiting and turnaround times by the
number of processes (n), and the averages are printed.