Experiment
Aim: Write a program to implement FCFS CPU scheduling algorithm.
Theory:
First Come, First Served (FCFS) also known as First In, First Out (FIFO) is the CPU scheduling algorithm in which the CPU is allocated to the processes in the order they are queued in the ready queue. FCFS follows non-preemptive scheduling which mean once the CPU is allocated to a process it does not leave the CPU until the process will not get terminated or may get halted due to some I/O interrupt.
Advantages:
➥ Simple and easy to implement.
➥ Non-preemptive; context switching is minimal.
➥ Fair: Processes are serviced in the order of their arrival.
➥ Suitable for batch systems with large workloads.
Disadvantages:
➥ Poor average waiting time; may suffer from the convoy effect.
➥ Non-preemptive: A long process can block all subsequent ones.
➥ Not suitable for interactive or time-sharing systems.
➥ Ignoring process length: Short processes may wait a long time if a large process is first in the queue.
Flowchart
Algorithm
Algorithm:
Step 1: Start
Step 2: Input Arrival Time (AT) and Burst Time (BT) for 4 processes
Step 3: Sort the processes by Arrival Time (if not already sorted)
Step 4: Initialize current time = 0
Step 5: For each process i from 1 to 4:
a. If current time < Arrival Time of process i:
→ Set current time = Arrival Time of process i (CPU waits)
b. Start Time = current time
c. Completion Time (CT) = Start Time + Burst Time
d. Turnaround Time (TAT) = CT - Arrival Time
e. Waiting Time (WT) = TAT - Burst Time
f. Update current time = Completion Time
Step 6: Display the result table:
PID, AT, BT, CT, TAT, WT
Step 7: End
Source Code :
// C++ program for First Come First Serve (FCFS) scheduling for 4 processes, where the input (Process ID, Arrival Time, and Burst // Time) is taken from the user.
#include <iostream>
using namespace std;
int main() {
int arrival[4], burst[4], completion[4], turnaround[4], waiting[4];
int i;
// Input
cout << "Enter Arrival Time and Burst Time for 4 processes:\n";
for (i = 0; i < 4; i++) {
cout << "Process " << i + 1 << ":\n";
cout << "Arrival Time: ";
cin >> arrival[i];
cout << "Burst Time: ";
cin >> burst[i];
}
// First process
completion[0] = arrival[0] + burst[0];
turnaround[0] = completion[0] - arrival[0];
waiting[0] = turnaround[0] - burst[0];
// Next processes
for (i = 1; i < 4; i++) {
if (completion[i - 1] < arrival[i]) {
completion[i] = arrival[i] + burst[i]; // CPU idle time
} else {
completion[i] = completion[i - 1] + burst[i];
}
turnaround[i] = completion[i] - arrival[i];
waiting[i] = turnaround[i] - burst[i];
}
// Output
cout << "\nProcess\tAT\tBT\tCT\tTAT\tWT\n";
for (i = 0; i < 4; i++) {
cout << i + 1 << "\t" << arrival[i] << "\t" << burst[i] << "\t" << completion[i]
<< "\t" << turnaround[i] << "\t" << waiting[i] << "\n";
}
return 0;
}
// Program should be with output in Lab file
Objective (multiple-choice) questions with answers based on the FCFS (First Come First Serve) Scheduling
Q1. In FCFS scheduling, the process that is executed first is the one with:
A) Highest priority
B) Largest burst time
C) Earliest arrival time
D) Lowest process ID
Answer: C) Earliest arrival time
Q2. FCFS is which type of scheduling algorithm?
A) Preemptive
B) Non-preemptive
C) Hybrid
D) Real-time
Answer: B) Non-preemptive
Q3. What is the waiting time of a process?
A) Completion time – Arrival time
B) Turnaround time – Burst time
C) Arrival time – Burst time
D) Burst time – Arrival time
Answer: B) Turnaround time – Burst time
Q4. Which of the following is a disadvantage of FCFS?
A) Easy to implement
B) No starvation
C) Convoy effect
D) Good for real-time systems
Answer: C) Convoy effect
Q5. In FCFS, if two processes have the same arrival time, which runs first?
A) Higher burst time
B) Random selection
C) One entered first (input order)
D) Shortest job first
Answer: C) One entered first (input order)
Q6. FCFS can lead to:
A) Starvation
B) Deadlock
C) Convoy effect
D) Parallel execution
Answer: C) Convoy effect
The term "Convoy OS" likely refers to the Convoy Effect in operating systems, a phenomenon where a long-running process can cause delays for shorter processes waiting to access the same resource. This effect is particularly noticeable in scheduling algorithms like First-Come, First-Served (FCFS), where a long process at the front of the queue can significantly impact the waiting time of shorter processes behind it.