FCFS
FCFS
#include <stdio.h>
int main() {
int n, i, j;
scanf("%d", &n);
scanf("%d", &arrival_time[i]);
scanf("%d", &burst_time[i]);
// Sort the processes based on arrival time, maintaining the original order for same arrival times
// Sort by arrival time, and if they are the same, keep the original order
if (arrival_time[i] > arrival_time[j] ||
arrival_time[i] = arrival_time[j];
arrival_time[j] = temp;
temp = burst_time[i];
burst_time[i] = burst_time[j];
burst_time[j] = temp;
temp = process[i];
process[i] = process[j];
process[j] = temp;
int current_time = 0;
// If the current time is less than the arrival time, CPU will be idle
total_wait_time += wait_time[i];
total_turnaround_time += turn_around_time[i];
return 0;
Code Overview
1. Input Data:
o The program starts by asking for the number of processes.
o It then collects the arrival time and burst time for each process.
2. Sorting Processes:
o The processes are sorted based on their arrival times. If two processes have the
same arrival time, they maintain their input order. This is achieved using a
nested loop that compares arrival times and, if necessary, process indices.
3. Time Calculations:
o The program tracks the current time as it processes each job.
o For each process, it calculates:
Completion Time: When the process finishes executing.
Turnaround Time: The total time taken from arrival to completion.
Waiting Time: The time the process spends waiting in the queue (i.e.,
turnaround time minus burst time).
4. Output:
o Finally, it prints the details of each process, including its arrival time, burst
time, waiting time, turnaround time, and completion time.
o It also calculates and prints the average waiting time and average turnaround
time for all processes.
Detailed Breakdown
1. Input Section:
o This loop collects arrival and burst times for each process and assigns a unique
process number.
3. Sorting Logic:
o The sorting mechanism checks if one arrival time is greater than the other. If
they are equal, it uses the process index to maintain the order of input.
o This ensures that processes are sorted first by arrival time and then by their
order of input when arrival times are the same.
4. Calculating Times:
o This section displays a table with all relevant information about each process
and calculates the averages.