First Come, First Serve ñ CPU Scheduling | (Non-preemptive)

FCFS CPU Scheduling (First Come, First Serve) is a fundamental CPU scheduling mechanism that executes programs in the order they are added to the ready queue. In other words, the first process to come will be carried out first, and so on. Since it uses a non−preemptive scheduling technique, a process that has been allocated to the CPU will keep running until it is finished or enters a waiting state.

Scenario 1

Let's take a look at an example to understand FCFS CPU scheduling in more detail. Suppose we have three processes with the following arrival times and burst times:

Process

Arrival Time

Burst Time

P1

0

5

P2

1

3

P3

2

4

The Gantt chart for the FCFS scheduling would look like this:

0-----5-----8-----12
|     |     |     |
P1    P2   P3   Idle


As we can see from the Gantt chart, P1 arrives at time 0 and gets the CPU first. It runs for 5 units of time and completes at time 5. Then P2 arrives at time 1, but it has to wait for P1 to complete before it can start executing. P2 runs for 3 units of time and completes at time 8. Finally, P3 arrives at time 2, but it has to wait for P2 to complete before it can start executing. P3 runs for 4 units of time and completes at time 12.

Overall, FCFS scheduling is simple to implement and ensures that every process gets a fair share of CPU time. However, it may lead to long waiting times for processes with higher burst times. Also, it is not suitable for real−time systems where response time is critical.

FCFS CPU scheduling is a basic scheduling algorithm that executes processes in the order in which they arrive in the ready queue. While it is easy to implement and fair to all processes, it may not be the most efficient or suitable scheduling algorithm in all cases.

Scenario 2

Let’s look into another example, suppose we have four processes with the following arrival times and burst times:

Process

Arrival Time

Burst Time

P1

0

6

P2

2

4

P3

4

2

P4

6

5

The Gantt chart for FCFS scheduling would look like this:

0-----6-----10-----12-----17
|     |     |      |      |
P1   P2     P3     P4    Idle


As we can see from the Gantt chart, P1 arrives at time 0 and gets the CPU first. It runs for 6 units of time and completes at time 6. Then P2 arrives at time 2, but it has to wait for P1 to complete before it can start executing.

• P2 runs for 4 units of time and completes at time 10. Then P3 arrives at time 4, but it has to wait for P2 to complete before it can start executing. P3 runs for 2 units of time and completes at time 12. Finally, P4 arrives at time 6, but it has to wait for P3 to complete before it can start executing. P4 runs for 5 units of time and completes at time 17.

• In this example, we can see that the process with the highest burst time (P1) had to execute first, leading to a longer waiting time for other processes. This is one of the drawbacks of FCFS scheduling, as it may not prioritize processes with shorter burst times, which can lead to longer waiting times and potentially affect the overall performance of the system.

Overall, FCFS scheduling is a simple scheduling algorithm that can be useful in certain scenarios where simplicity and fairness are more important than efficiency. However, other scheduling algorithms such as Round Robin or Shortest Job First may be more suitable for systems with varying process requirements and priorities.

Pseudocode for FCFS

Here's the pseudocode for FCFS CPU Scheduling algorithm:

queue = empty queue
clock = 0
while (processes not finished)
if (new process arrives at clock time)
add the process to the end of the queue
if (CPU is idle and queue is not empty)
remove the first process from the queue
set the start time of the process to the current clock time
run the process for its burst time
set the finish time of the process to the current clock time
increment the clock by 1


Java implementation

Here is the Java implementation of the above pseudocode:

Example

import java.util.*;
class Process {
public int pid;
public int arrivalTime;
public int burstTime;
public int startTime;
public int finishTime;

public Process(int pid, int arrivalTime, int burstTime) {
this.pid = pid;
this.arrivalTime = arrivalTime;
this.burstTime = burstTime;
}
}

public class FCFSScheduling {
public static void main(String[] args) {
// Create a list of processes
List<Process> processes = new ArrayList<>();

// Sort processes by arrival time
processes.sort(Comparator.comparing(p -> p.arrivalTime));

// Initialize variables
int clock = 0;
List<Process> completedProcesses = new ArrayList<>();

// Execute processes in FCFS order
while (!processes.isEmpty() || !queue.isEmpty()) {
// Add new arrivals to the queue
while (!processes.isEmpty() && processes.get(0).arrivalTime == clock) {
processes.remove(0);
}

// Execute the first process in the queue
if (!queue.isEmpty()) {
Process currentProcess = queue.remove();
currentProcess.startTime = clock;
clock += currentProcess.burstTime;
currentProcess.finishTime = clock;
} else {
// CPU is idle
clock++;
}
}

// Print results
for (Process p : completedProcesses) {
System.out.println("Process " + p.pid + ": start time = " + p.startTime
+ ", finish time = " + p.finishTime);
}
}
}


Output

Process 1: start time = 0, finish time = 6
Process 2: start time = 2, finish time = 6
Process 3: start time = 4, finish time = 6
Process 4: start time = 6, finish time = 11


In this approach, each process is represented by a Process class, and all of the processes are stored in a list. The list is then sorted by arrival time. The ready queue is represented by a queue, and tasks are carried out in FCFS order. Each process' start and end times are recorded, and the finished processes are kept in a separate list. As a last step, we output the start time and end time of each process.

Remember that this implementation is merely an illustration and that it can be enhanced or altered in accordance with the particular needs of the system.

Conclusion

In conclusion, First−Come−First−Serve (FCFS) CPU Scheduling is a straightforward and understandable scheduling technique that executes processes in the order they appear in the ready queue. Despite being simple to use, it may not always be the best effective scheduling algorithm because it cannot manage priority tasks or short assignments.

FCFS is appropriate for systems where turnaround time is not a top priority, like a single−user system with a limited number of processes. However, it might not be appropriate for real−time applications or systems that must respond quickly.

Due to its simplicity and ease of use, FCFS is a widely used scheduling algorithm overall, although depending on the particular requirements and limitations of the system, it might not always be the best option. In some circumstances, different scheduling algorithms like Round Robin, Priority Scheduling, and Shortest Job First may perform better.

Updated on: 22-Aug-2023

116 Views