First come first serve scheduling algorithm (FCFS) is the process that request the CPU to
allocate the resources on the first come priority basis.the Implementation of the first come first
serve scheduling algorithm (FCFS) policy is easily managed with fifo(First in First out)
queue.when a process enters into the ready queue ,its address is linked onto the tail of the queue.
When the CPU is free it is allocated to the process at the head of the queue.
The running process is then removed from the queue.the code for first come first serve
scheduling algorithm (FCFS) is simple to write and Understand.The First come first serve
scheduling algorithm (FCFS) in non preemptive.The first come first serve scheduling algorithm
(FCFS) have a drawback for time sharing systems.
first come first serve scheduling algorithm Program [Non-Preemptive]:
import java.io.*;
class fcfs {
public static void main(String args[]) throws Exception {
int n, AT[], BT[], WT[], TAT[];//Burst Time,Average Time,Wait Time
float AWT = 0;
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
System.out.println(“Enter no of process”);
n = Integer.parseInt(br.readLine());
BT = new int[n];
WT = new int[n];
TAT = new int[n];
AT = new int[n];
System.out.println(“Enter Burst time for each process\ ******************************”);
for (int i = 0; i < n; i++) {
System.out.println(“Enter BT for process ” + (i + 1));
BT[i] = Integer.parseInt(br.readLine());
}
System.out.println(“***********************************************”);
for (int i = 0; i < n; i++) {
System.out.println(“Enter AT for process” + i);
AT[i] = Integer.parseInt(br.readLine());
}
System.out.println(“***********************************************”);
WT[0] = 0;
for (int i = 1; i < n; i++) {
WT[i] = WT[i – 1] + BT[i – 1];
WT[i] = WT[i] – AT[i];
}
for (int i = 0; i < n; i++) {
TAT[i] = WT[i] + BT[i];
AWT = AWT + WT[i];
}
System.out.println(” PROCESS BT WT TAT “);
for (int i = 0; i < n; i++) {
System.out.println(” ” + i + ” ” + BT[i] + ” ” + WT[i] + ” ” + TAT[i]);
}
AWT = AWT / n;
System.out.println(“***********************************************”);
System.out.println(“Avg waiting time=” + AWT + “\
***********************************************”);
}
}
Shortest job first -
A different approach to CPU scheduling is SJF shortest job first scheduling algorithm.This
associates with each process the length of the latter next CPU burst.When the CPU is available it
is assigned to the process that has the smallest next CPU burst.if two processes have same length
next CPU burst,FCFS scheduling is used to break the tie.Note that a more appropriate term
would be the shortest next CPU burst ,because the scheduling is done by examining the length of
the next CPU burst because the scheduling is done by examining the length of the next CPU
burst of a process,rather than its total length.The real difficulty in SJF shortest job first
scheduling algorithm is knowing the length of the next proce.
First come first serve scheduling algorithm (FCFS) is the process th.pdf
1. First come first serve scheduling algorithm (FCFS) is the process that request the CPU to
allocate the resources on the first come priority basis.the Implementation of the first come first
serve scheduling algorithm (FCFS) policy is easily managed with fifo(First in First out)
queue.when a process enters into the ready queue ,its address is linked onto the tail of the queue.
When the CPU is free it is allocated to the process at the head of the queue.
The running process is then removed from the queue.the code for first come first serve
scheduling algorithm (FCFS) is simple to write and Understand.The First come first serve
scheduling algorithm (FCFS) in non preemptive.The first come first serve scheduling algorithm
(FCFS) have a drawback for time sharing systems.
first come first serve scheduling algorithm Program [Non-Preemptive]:
import java.io.*;
class fcfs {
public static void main(String args[]) throws Exception {
int n, AT[], BT[], WT[], TAT[];//Burst Time,Average Time,Wait Time
float AWT = 0;
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
System.out.println(“Enter no of process”);
n = Integer.parseInt(br.readLine());
BT = new int[n];
WT = new int[n];
TAT = new int[n];
AT = new int[n];
System.out.println(“Enter Burst time for each process ******************************”);
for (int i = 0; i < n; i++) {
System.out.println(“Enter BT for process ” + (i + 1));
BT[i] = Integer.parseInt(br.readLine());
}
System.out.println(“***********************************************”);
for (int i = 0; i < n; i++) {
System.out.println(“Enter AT for process” + i);
AT[i] = Integer.parseInt(br.readLine());
}
System.out.println(“***********************************************”);
WT[0] = 0;
2. for (int i = 1; i < n; i++) {
WT[i] = WT[i – 1] + BT[i – 1];
WT[i] = WT[i] – AT[i];
}
for (int i = 0; i < n; i++) {
TAT[i] = WT[i] + BT[i];
AWT = AWT + WT[i];
}
System.out.println(” PROCESS BT WT TAT “);
for (int i = 0; i < n; i++) {
System.out.println(” ” + i + ” ” + BT[i] + ” ” + WT[i] + ” ” + TAT[i]);
}
AWT = AWT / n;
System.out.println(“***********************************************”);
System.out.println(“Avg waiting time=” + AWT + “
***********************************************”);
}
}
Shortest job first -
A different approach to CPU scheduling is SJF shortest job first scheduling algorithm.This
associates with each process the length of the latter next CPU burst.When the CPU is available it
is assigned to the process that has the smallest next CPU burst.if two processes have same length
next CPU burst,FCFS scheduling is used to break the tie.Note that a more appropriate term
would be the shortest next CPU burst ,because the scheduling is done by examining the length of
the next CPU burst because the scheduling is done by examining the length of the next CPU
burst of a process,rather than its total length.The real difficulty in SJF shortest job first
scheduling algorithm is knowing the length of the next process.SJF shortest job first scheduling
algorithm scheduling algorithm is optimal which gives the minimum average waiting time for a
given set of processes.
Program-
import java.util.*;
class SJF {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n, BT[], WT[], TAT[];
System.out.println("Enter no of process");
3. n = sc.nextInt();
BT = new int[n + 1];
WT = new int[n + 1];
TAT = new int[n + 1];
float AWT = 0;
System.out.println("Enter Burst time for each process");
for (int i = 0; i < n; i++) {
System.out.println("Enter BT for process " + (i + 1));
BT[i] = sc.nextInt();
}
for (int i = 0; i < n; i++) {
WT[i] = 0;
TAT[i] = 0;
}
int temp;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - 1; j++) {
if (BT[j] > BT[j + 1]) {
temp = BT[j];
BT[j] = BT[j + 1];
BT[j + 1] = temp;
temp = WT[j];
WT[j] = WT[j + 1];
WT[j + 1] = temp;
}
}
}
for (int i = 0; i < n; i++) {
TAT[i] = BT[i] + WT[i];
WT[i + 1] = TAT[i];
}
TAT[n] = WT[n] + BT[n];
System.out.println(" PROCESS BT WT TAT ");
for (int i = 0; i < n; i++)
System.out.println(" " + i + " " + BT[i] + " " + WT[i] + " " + TAT[i]);
for (int j = 0; j < n; j++)
4. AWT += WT[j];
AWT = AWT / n;
System.out.println("***********************************************");
System.out.println("Avg waiting time=" + AWT + "
***********************************************");
}
}
Solution
First come first serve scheduling algorithm (FCFS) is the process that request the CPU to
allocate the resources on the first come priority basis.the Implementation of the first come first
serve scheduling algorithm (FCFS) policy is easily managed with fifo(First in First out)
queue.when a process enters into the ready queue ,its address is linked onto the tail of the queue.
When the CPU is free it is allocated to the process at the head of the queue.
The running process is then removed from the queue.the code for first come first serve
scheduling algorithm (FCFS) is simple to write and Understand.The First come first serve
scheduling algorithm (FCFS) in non preemptive.The first come first serve scheduling algorithm
(FCFS) have a drawback for time sharing systems.
first come first serve scheduling algorithm Program [Non-Preemptive]:
import java.io.*;
class fcfs {
public static void main(String args[]) throws Exception {
int n, AT[], BT[], WT[], TAT[];//Burst Time,Average Time,Wait Time
float AWT = 0;
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
System.out.println(“Enter no of process”);
n = Integer.parseInt(br.readLine());
BT = new int[n];
WT = new int[n];
TAT = new int[n];
AT = new int[n];
System.out.println(“Enter Burst time for each process ******************************”);
for (int i = 0; i < n; i++) {
System.out.println(“Enter BT for process ” + (i + 1));
5. BT[i] = Integer.parseInt(br.readLine());
}
System.out.println(“***********************************************”);
for (int i = 0; i < n; i++) {
System.out.println(“Enter AT for process” + i);
AT[i] = Integer.parseInt(br.readLine());
}
System.out.println(“***********************************************”);
WT[0] = 0;
for (int i = 1; i < n; i++) {
WT[i] = WT[i – 1] + BT[i – 1];
WT[i] = WT[i] – AT[i];
}
for (int i = 0; i < n; i++) {
TAT[i] = WT[i] + BT[i];
AWT = AWT + WT[i];
}
System.out.println(” PROCESS BT WT TAT “);
for (int i = 0; i < n; i++) {
System.out.println(” ” + i + ” ” + BT[i] + ” ” + WT[i] + ” ” + TAT[i]);
}
AWT = AWT / n;
System.out.println(“***********************************************”);
System.out.println(“Avg waiting time=” + AWT + “
***********************************************”);
}
}
Shortest job first -
A different approach to CPU scheduling is SJF shortest job first scheduling algorithm.This
associates with each process the length of the latter next CPU burst.When the CPU is available it
is assigned to the process that has the smallest next CPU burst.if two processes have same length
next CPU burst,FCFS scheduling is used to break the tie.Note that a more appropriate term
would be the shortest next CPU burst ,because the scheduling is done by examining the length of
the next CPU burst because the scheduling is done by examining the length of the next CPU
burst of a process,rather than its total length.The real difficulty in SJF shortest job first
scheduling algorithm is knowing the length of the next process.SJF shortest job first scheduling
6. algorithm scheduling algorithm is optimal which gives the minimum average waiting time for a
given set of processes.
Program-
import java.util.*;
class SJF {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n, BT[], WT[], TAT[];
System.out.println("Enter no of process");
n = sc.nextInt();
BT = new int[n + 1];
WT = new int[n + 1];
TAT = new int[n + 1];
float AWT = 0;
System.out.println("Enter Burst time for each process");
for (int i = 0; i < n; i++) {
System.out.println("Enter BT for process " + (i + 1));
BT[i] = sc.nextInt();
}
for (int i = 0; i < n; i++) {
WT[i] = 0;
TAT[i] = 0;
}
int temp;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - 1; j++) {
if (BT[j] > BT[j + 1]) {
temp = BT[j];
BT[j] = BT[j + 1];
BT[j + 1] = temp;
temp = WT[j];
WT[j] = WT[j + 1];
WT[j + 1] = temp;
}
}
}
7. for (int i = 0; i < n; i++) {
TAT[i] = BT[i] + WT[i];
WT[i + 1] = TAT[i];
}
TAT[n] = WT[n] + BT[n];
System.out.println(" PROCESS BT WT TAT ");
for (int i = 0; i < n; i++)
System.out.println(" " + i + " " + BT[i] + " " + WT[i] + " " + TAT[i]);
for (int j = 0; j < n; j++)
AWT += WT[j];
AWT = AWT / n;
System.out.println("***********************************************");
System.out.println("Avg waiting time=" + AWT + "
***********************************************");
}
}