Tutorial Playlist
"First come, first serve" (FCFS) is a scheduling algorithm used to manage and prioritize tasks or processes. It is a fundamental method that all students must be aware of in order to learn the core principles of CPU Process Scheduling Algorithms. Stay hooked to the end of this tutorial to know more.
This tutorial is all about the characteristics of FCFS, its advantages, and disadvantages along with several examples that will help you grasp a deeper understanding of this basic concept in computer science.
First Come First Serve (FCFS) is a simple CPU scheduling algorithm used in operating systems. In this approach, processes are executed in the order they arrive in the ready queue. The first process that enters the queue is the first one to be executed by the CPU. FCFS is a non-preemptive scheduling algorithm, meaning that upon execution, processes continue to run until they complete or voluntarily release the CPUs.
Let's understand how FCFS scheduling works with an example:
Suppose we have three processes A, B, and C with their respective burst times:
Assuming they arrive in the order A -> B -> C, the order of execution under FCFS would be:
In this scenario, Process A, being the first to arrive, gets executed first, followed by B and then C.
Now, here's another example with different process burst times:
In this example, we again have three processes X, Y, and Z with their respective burst times:
Assuming they arrive in the order X -> Y -> Z, the order of execution under FCFS would be:
In this example, Process X starts execution first as it arrived first. After Process X completes, Process Y follows, and finally, Process Z runs.
FCFS is a non-preemptive scheduling algorithm, which means that a process will continue to run until it finishes its burst time upon execution. Preemption is not supported in FCFS, so the currently executing process cannot be interrupted by a higher-priority process.
In a preemptive scheduling approach, the operating system can interrupt a running process and allocate the CPU to another process if a higher-priority process becomes available. This means that a running process may be temporarily paused to allow another process with higher priority to execute.
Suppose we have two processes, A and B, with the following burst times and priorities:
In preemptive scheduling, if Process B becomes ready while Process A is running, the operating system might decide to preempt (interrupt) Process A to allow Process B, with a higher priority, to execute. After a while, the operating system could resume executing Process A.
In FCFS, once a process starts execution, it continues until it completes or releases the CPU voluntarily. Other processes waiting in the ready queue have to wait for the currently executing process to finish before they can start execution.
In a non-preemptive scheduling approach, once processes start executing, they run until they complete their burst times or voluntarily release the CPUs. No process can be interrupted by the operating system until it finishes or enters a blocking state.
Let's use the same processes, A and B, with the same burst times and priorities:
In a non-preemptive scheduling scenario, if Process B becomes ready while Process A is running, the operating system will let Process A continue until it completes its execution. Only after Process A finishes or enters a blocking state will Process B get a chance to execute.
The convoy effect is a phenomenon that can occur in FCFS scheduling. It refers to a situation where long processes occupy the CPUs, making the shorter processes wait behind them. This can lead to poor CPU utilization and increased response times for other processes.
For example, consider a scenario where a long I/O-bound process occupies the CPU under FCFS. Other shorter processes that are CPU-bound might queue up behind this process, resulting in longer waiting times for those processes.
The characteristics of FCFS CPU Process Scheduling are as follows:
Here are the benefits of FCFS CPU Process Scheduling:
Here are some of the downsides of FCFS CPU Process Scheduling:
Dijkstra proposed a solution to the problem of wasting wake-up signals by introducing Semaphores in operating systems. Semaphores are variables used to store wake-up calls from producers to consumers, preventing direct signaling and potential wastage. The producer stores wake-up calls in the semaphore variable, which can be read and updated automatically in kernel mode by any consumer when needed.
The implementation of Semaphores in user mode is not feasible due to the possibility of race conditions when multiple processes try to access the variable simultaneously. Thus, Semaphores always require support from the operating system for proper functioning.
There are two categories of Semaphores based on their functionality:
AT: Arrival Time - The time at which a process arrives in the ready queue or becomes ready for execution.
BT: Burst Time - The amount of time a process requires to complete its execution, also known as the CPU time.
CPU: Central Processing Unit - The main processing component of a computer where calculations and data processing take place.
CT: Completion Time - The time at which a process finishes its execution.
FIFO: First In First Out - A queueing principle where the first item to enter the queue is the first one to be removed.
OS: Operating System - Software that manages computer hardware, software resources, and provides services for computer programs.
TAT: Turn Around Time - The total time taken for a process to complete its execution, from arrival to completion.
WT: Waiting Time - The total time a process spends waiting in the ready queue before it starts execution.
Here's an example of implementing the First Come First Serve CPU scheduling algorithm in Java:
Code:
import java.util.*;
class Process {
String name;
int arrivalTime;
int burstTime;
public Process(String name, int arrivalTime, int burstTime) {
this.name = name;
this.arrivalTime = arrivalTime;
this.burstTime = burstTime;
}
}
public class FCFSExample {
public static void main(String[] args) {
List<Process> processes = new ArrayList<>();
processes.add(new Process("Process A", 0, 8));
processes.add(new Process("Process B", 1, 4));
processes.add(new Process("Process C", 2, 6));
int currentTime = 0;
for (Process process : processes) {
if (process.arrivalTime > currentTime) {
currentTime = process.arrivalTime;
}
System.out.println("Executing " + process.name + " from time " + currentTime + " to " + (currentTime + process.burstTime));
currentTime += process.burstTime;
}
}
}
In the above code, we define a Process class with attributes for the process name, arrival time, and burst time. We then create a list of processes and simulate their execution using the FCFS scheduling algorithm. The processes are executed in the order they arrive in the list, and their execution times are printed.
The First Come, First Served (FCFS) scheduling algorithm is beneficial due to its fairness, simplicity, and predictability in managing tasks or processes based on their arrival time. It ensures all tasks are eventually processed, preventing starvation. With low overhead and ease of implementation, FCFS is an efficient choice for scenarios where task priorities and execution times are not critical.
Through this tutorial, we aimed to give you a preliminary understanding of what First Come, First Served is. However, if you want a more nuanced understanding, you should enroll in the specially curated computer science courses by upGrad. With the help of these courses by upGrad you will not only be able to get a better understanding of the concepts but also bag lucrative jobs in leading corporations.
Avoid FCFS in scenarios where some processes are time-sensitive or have higher priority. If your system experiences a mix of long and short-running processes, more advanced scheduling algorithms like Shortest Job First (SJF) or Round Robin might be more suitable.
Yes, FCFS can be part of hybrid scheduling approaches. For example, in multi-level feedback queues, FCFS might be used in one of the priority queues while other algorithms handle different queues with varying priorities and time quantum.
The average waiting time in FCFS depends on the order of process arrival. If shorter processes arrive first, the average waiting time may be lower. However, if longer processes arrive first, the average waiting time can be significantly higher.
PAVAN VADAPALLI
Popular
Talk to our experts. We’re available 24/7.
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enrolling. upGrad does not make any representations regarding the recognition or equivalence of the credits or credentials awarded, unless otherwise expressly stated. Success depends on individual qualifications, experience, and efforts in seeking employment.
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...