Here is a very simple Simulation of First Come First Serve (FCFS) Scheduling algorithm in java . . . .Tell me if any changes are required... :-)
import java.util.Scanner;
public class Fcfs
{
public static void main(String[] args)
{
int N;
Timer timer = new Timer(0);
System.out.println("Enter the number of processes :");
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
Process process[] = new Process[N];
// Initialize all the processes
for(int i=0;i<N;i++)
{
System.out.println("Enter the Process no :");
int x = sc.nextInt();
System.out.println("Enter the Burst for process : "+x+" :");
int newburst = sc.nextInt();
process[i] = new Process(x,timer,newburst);
}
// Schedule the processes
for(int i=0; i<N; i++)
{
process[i].execute();
}
// Now displaying the Statistics
System.out.println("\n*************************************************\n");
System.out.println("--Process--Waiting Time--Turnaround Time--");
for(int i=0;i<N;i++)
{
System.out.println(" "+process[i].pno+" "+process[i].waitingTime+" "+process[i].turnaroundTime+" ");
}
System.out.println("\n*************************************************\n");
}
}
class Process
{
int pno;
int burst;
int submissionTime;
int completionTime;
int waitingTime;
int turnaroundTime;
Timer timer;
Process(int pno, Timer timer,int burst)
{
this.pno = pno;
this.timer = timer;
this.burst = burst;
}
void execute()
{
this.submissionTime = timer.time;
for(int i=0;i<this.burst;i++)
{
timer.tick();
}
this.completionTime = timer.time;
System.out.println("Process "+this.pno+" is executing and finishing at : "+timer.time);
this.turnaroundTime = this.completionTime;
this.waitingTime = this.turnaroundTime - this.burst;
}
}
class Timer
{
int time;
Timer(int time)
{
this.time = time;
}
void tick()
{
this.time =this.time + 1;
}
}
FCFS.java
import java.util.Scanner;
public class Fcfs
{
public static void main(String[] args)
{
int N;
Timer timer = new Timer(0);
System.out.println("Enter the number of processes :");
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
Process process[] = new Process[N];
// Initialize all the processes
for(int i=0;i<N;i++)
{
System.out.println("Enter the Process no :");
int x = sc.nextInt();
System.out.println("Enter the Burst for process : "+x+" :");
int newburst = sc.nextInt();
process[i] = new Process(x,timer,newburst);
}
// Schedule the processes
for(int i=0; i<N; i++)
{
process[i].execute();
}
// Now displaying the Statistics
System.out.println("\n*************************************************\n");
System.out.println("--Process--Waiting Time--Turnaround Time--");
for(int i=0;i<N;i++)
{
System.out.println(" "+process[i].pno+" "+process[i].waitingTime+" "+process[i].turnaroundTime+" ");
}
System.out.println("\n*************************************************\n");
}
}
class Process
{
int pno;
int burst;
int submissionTime;
int completionTime;
int waitingTime;
int turnaroundTime;
Timer timer;
Process(int pno, Timer timer,int burst)
{
this.pno = pno;
this.timer = timer;
this.burst = burst;
}
void execute()
{
this.submissionTime = timer.time;
for(int i=0;i<this.burst;i++)
{
timer.tick();
}
this.completionTime = timer.time;
System.out.println("Process "+this.pno+" is executing and finishing at : "+timer.time);
this.turnaroundTime = this.completionTime;
this.waitingTime = this.turnaroundTime - this.burst;
}
}
class Timer
{
int time;
Timer(int time)
{
this.time = time;
}
void tick()
{
this.time =this.time + 1;
}
}
naughty giyoooooooooooooo
ReplyDelete