# Implement First Come First Served (FCFS) CPU Scheduling Algorithm using C program

**First Come First Served (FCFS) CPU Scheduling Algorithm implementation**: Here, we are going to **implement FCFS scheduling algorithm using C program**.

Submitted by Vipin Bailwal, on September 24, 2018

CPU scheduling decides which of the available processes in the ready queue is to be allocated the CPU. There are different CPU scheduling algorithms available. In this tutorial, we will learn about the **First Come First Served Algorithm (FCFS)** and see **how we can implement it in C Programming Language**?

## First Come First Serve (FCFS)

**First Come First Serve** is the simplest and easiest scheduling algorithm. In this algorithm, the CPU is allocated to the processes in the order they request it. The **implementation of FCFS** is easily done with a queue (a FIFO structure). When the first process enters the system it starts its execution immediately and runs till it completes its execution. As other processes enter the system, they are put at the end of the queue and wait to get the CPU. When a process finishes executing, it releases the CPU, is removed from the queue and the CPU is allocated to next process at the head of the queue.

**Example:**

The processes arrive in the order **P1, P2, P3** and are served as per the **FCFS algorithm**. The Gantt chart is as shown:

The waiting time for **P1** is **0 milliseconds**, for **P2** it is **25 milliseconds** and **29 milliseconds** for **P3**. Thus, average waiting time is **(0+25+29)/3 = 18 milliseconds**.

**Advantage:**

- It is easy to understand and implement.

**Disadvantage:**

- It is a
**Non-Pre-emptive scheduling algorithm**: Once a process has been allocated the CPU, it will not release the CPU until it finishes executing. Thus, it is not suitable for modern systems which work on the principle of time sharing. - The Average Waiting Time is high.
- It results in
**CONVOY EFFECT**i.e., many processes which require CPU for short duration have to wait for a bigger process to finish thus resulting in low resource utilization.

**C program:**

#include<stdio.h> int main(){ int bt[10]={0},at[10]={0},tat[10]={0},wt[10]={0},ct[10]={0}; int n,sum=0; float totalTAT=0,totalWT=0; printf("Enter number of processes "); scanf("%d",&n); printf("Enter arrival time and burst time for each process\n\n"); for(int i=0;i<n;i++) { printf("Arrival time of process[%d] ",i+1); scanf("%d",&at[i]); printf("Burst time of process[%d] ",i+1); scanf("%d",&bt[i]); printf("\n"); } //calculate completion time of processes for(int j=0;j<n;j++) { sum+=bt[j]; ct[j]+=sum; } //calculate turnaround time and waiting times for(int k=0;k<n;k++) { tat[k]=ct[k]-at[k]; totalTAT+=tat[k]; } for(int k=0;k<n;k++) { wt[k]=tat[k]-bt[k]; totalWT+=wt[k]; } printf("Solution: \n\n"); printf("P#\t AT\t BT\t CT\t TAT\t WT\t\n\n"); for(int i=0;i<n;i++) { printf("P%d\t %d\t %d\t %d\t %d\t %d\n",i+1,at[i],bt[i],ct[i],tat[i],wt[i]); } printf("\n\nAverage Turnaround Time = %f\n",totalTAT/n); printf("Average WT = %f\n\n",totalWT/n); return 0; }

**Output**

Arrival time of process[3] 0 Burst time of process[3] 3 Solution: P# AT BT CT TAT WT P1 0 24 24 24 0 P2 0 3 27 27 24 P3 0 3 30 30 27 Average Turnaround Time = 27.000000 Average WT = 17.000000

Related Tutorials

- Introduction to Algorithms
- Introduction to Greedy Strategy in Algorithms
- Stability in sorting
- External Merge Sorting Algorithm
- Radix Sort and its Algorithm
- Bucket Sort Algorithm
- Bubble sort Algorithm, Flow Chart and C++ Code
- Insertion sort Algorithm, flowchart and C, C++ Code
- Merge Sort | One of the best sorting algorithms used for large inputs
- Binary Search in C, C++
- Randomized Binary Search
- Meta Binary Search | One-sided Binary Search
- Difference between Linear Search and Binary Search
- Binary Search in String
- Variants of Binary Search
- Sieve of Eratosthenes to find prime numbers
- Optimal Merge Pattern (Algorithm and Example)
- Given an array of n numbers, Check whether there is any duplicate or not
- Finding the missing number
- Find the number occurring an odd number of times
- Find the pair whose sum is closest to zero in minimum time complexity
- Find three elements in an array such that their sum is equal to given element K
- Bitonic Search Algorithm
- Check whether a number is Fibonacci or not
- Segregate even and odd numbers in minimum time complexity
- Find trailing zeros in factorial of a number
- Find Nearest Greatest Neighbours of each element in an array
- Interpolation search algorithm
- Floor and ceil of an element in an array using C++
- Two Elements whose sum is closest to zero
- Find a pair with a given difference
- Count number of occurrences (or frequency) in a sorted array
- Find a Fixed Point (Value equal to index) in a given array
- Find the maximum element in an array which is first increasing and then decreasing
- Dynamic Programming (Components, Applications and Elements)
- Algorithm for fractional knapsack problem
- Algorithm and procedure to solve a longest common subsequence problem
- Find the Nth Fibonacci number | C++
- Longest Common Subsequence using Dynamic programming (DP)
- Longest Increasing Subsequence using Dynamic programming (DP)
- Find the maximum sub-array sum using KADANE'S ALGORITHM
- Non-intersecting chords using Dynamic Programming (DP)
- Edit Distance using Dynamic Programming (DP)
- Finding Ugly Number using Dynamic Programming (DP)
- Egg dropping problem using Dynamic Programming (DP)
- Wild card matching problem using Dynamic programming (DP)
- Compute sum of digits in all numbers from 1 to N for a given N
- Minimum jumps required using Dynamic programming (DP)
- Graph coloring problem's solution using backtracking algorithm
- Breadth First Search (BFS) and Depth First Search (DFS) Algorithms
- Travelling Salesman Problem
- Kruskal's (P) and Prim's (K) Algorithms
- Multistage graph problem with forward approach and backward approach algorithms
- Floyd Warshall algorithm with its Pseudo Code

- Backtracking (Types and Algorithms)
- 4 Queen's problem and solution using backtracking algorithm
- N Queen's problem and solution using backtracking algorithm
- Find the GCD (Greatest Common Divisor) of two numbers using EUCLID'S ALGORITHM
- Compute the value of A raise to the power B using Fast Exponentiation
- Implementations of FCFS scheduling algorithm using C++
- Implementation of Shortest Job First (SJF) Non-Preemptive CPU scheduling algorithm using C++
- Implementation of Shortest Job First (SJF) Preemptive CPU scheduling algorithm using C++
- Implementation of Priority scheduling (Pre-emptive) algorithm using C++
- Implementation of Priority scheduling (Non Pre-emptive) algorithm using C++
- Implementation of Round Robin CPU Scheduling algorithm using C++
- Analysis of LRU page replacement algorithm and Belady's anomaly
- Branch and Bound
- Find the roots of a complex polynomial equation using Regula Falsi Method in C
- Sieve of Eratosthenes to find prime numbers
- Implementations of FCFS scheduling algorithm using C++
- Implementation of Shortest Job First (SJF) Non-Preemptive CPU scheduling algorithm using C++
- Implementation of Shortest Job First (SJF) Preemptive CPU scheduling algorithm using C++
- Implementation of Priority scheduling (Pre-emptive) algorithm using C++
- Divide and Conquer Paradigm (What it is, Its Applications, Pros and Cons)
- Implementation of Priority scheduling (Non Pre-emptive) algorithm using C++
- Implementation of Round Robin CPU Scheduling algorithm using C++
- Jump Search Implementation using C++
- Optimal Merge Pattern (Algorithm and Example)
- Introduction to Greedy Strategy in Algorithms
- Strassen's Matrix Multiplication in algorithms
- Huffman Coding (Algorithm, Example and Time complexity)
- Backtracking (Types and Algorithms)
- 4 Queen's problem and solution using backtracking algorithm
- N Queen's problem and solution using backtracking algorithm
- Graph coloring problem's solution using backtracking algorithm
- Tournament Tree and their properties
- Deterministic and Non Deterministic Algorithms
- Lower Bound Theory
- Non Recursive Tree Traversal Algorithm
- Line Drawing Algorithm
- Breadth First Search (BFS) and Depth First Search (DFS) Algorithms
- P and NP problems and solutions | Algorithms
- Travelling Salesman Problem
- 2 – 3 Trees Algorithm
- Kruskal's (P) and Prim's (K) Algorithms
- Algorithm for fractional knapsack problem
- Algorithm and procedure to solve a longest common subsequence problem
- Midpoint Circle Algorithm
- Multistage graph problem with forward approach and backward approach algorithms
- Floyd Warshall algorithm with its Pseudo Code
- Reliability design problem
- Removing consecutive duplicates from a string
- Fast Exponentiation using Bitmasking

Comments and Discussions!