Here's a simulation of round robin process scheduling algorithm . . . . Tell me if it requires any changes . . . :-)
public class RoundRobin
{
public static void main(String[] args)
{
int N;
int newburst;
int totalburst = 0;
Timer timer = new Timer(0);;
final int timeSlice = 2;
System.out.println("Enter the Number of Processes :");
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
Process process[] = new Process[N];
// First Step : Intializing all the processes
for(int i=0;i<N;i++)
{
System.out.println("Enter the burst for Process : "+i+" :");
newburst = sc.nextInt();
totalburst = totalburst + newburst;
process[i] = new Process(i,timeSlice,newburst,timer);
}
//Second step : Scheduling the processes
int j=0;
while(timer.currentTime != totalburst) // Under construction
{
System.out.println("Time now : "+timer.currentTime);
process[j].execute();
j = (j+1)%N;
}
// Now Displaying all the Turnaround time values neatly:
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].waiting+" "+process[i].turnaround+" ");
}
System.out.println("\n*************************************************\n");
}
}
class Process
{
int pno;
int burst;
int remburst;
int timeSlice;
int turnaround=0;
int waiting=0;
int submissionTime;
int completionTime;
Timer timer;
Process(int pno, int timeSlice, int burst, Timer timer)
{
this.pno = pno;
this.timeSlice = timeSlice;
this.burst = burst;
this.remburst = this.burst;
this.timer = timer;
}
void execute()
{
// set the Arrival time /Submission time
if(this.remburst == this.burst)
this.submissionTime = timer.currentTime;
if(this.remburst > 0)
{
int tempburst = this.remburst;
this.remburst = this.remburst - timeSlice;
if(this.remburst <= 0 )
{
for(int i=0;i<tempburst;i++)
{
timer.tick();
}
this.remburst = 0;
System.out.println("Process "+this.pno+" has Finished executing");
this.completionTime = timer.currentTime;
this.turnaround = this.completionTime - this.submissionTime;
this.waiting = this.turnaround - this.burst;
}
else
{
System.out.println("Process "+this.pno+" is executing");
System.out.println("Remaining Time for process "+this.pno+" is :"+this.remburst);
for(int i=0;i<timeSlice;i++)
{
timer.tick();
}
}
}
}
}
class Timer
{
int currentTime;
Timer(int time)
{
this.currentTime = time;
}
void tick()
{
this.currentTime = this.currentTime + 1;
}
}
RoundRobin.java
import java.util.Scanner;public class RoundRobin
{
public static void main(String[] args)
{
int N;
int newburst;
int totalburst = 0;
Timer timer = new Timer(0);;
final int timeSlice = 2;
System.out.println("Enter the Number of Processes :");
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
Process process[] = new Process[N];
// First Step : Intializing all the processes
for(int i=0;i<N;i++)
{
System.out.println("Enter the burst for Process : "+i+" :");
newburst = sc.nextInt();
totalburst = totalburst + newburst;
process[i] = new Process(i,timeSlice,newburst,timer);
}
//Second step : Scheduling the processes
int j=0;
while(timer.currentTime != totalburst) // Under construction
{
System.out.println("Time now : "+timer.currentTime);
process[j].execute();
j = (j+1)%N;
}
// Now Displaying all the Turnaround time values neatly:
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].waiting+" "+process[i].turnaround+" ");
}
System.out.println("\n*************************************************\n");
}
}
class Process
{
int pno;
int burst;
int remburst;
int timeSlice;
int turnaround=0;
int waiting=0;
int submissionTime;
int completionTime;
Timer timer;
Process(int pno, int timeSlice, int burst, Timer timer)
{
this.pno = pno;
this.timeSlice = timeSlice;
this.burst = burst;
this.remburst = this.burst;
this.timer = timer;
}
void execute()
{
// set the Arrival time /Submission time
if(this.remburst == this.burst)
this.submissionTime = timer.currentTime;
if(this.remburst > 0)
{
int tempburst = this.remburst;
this.remburst = this.remburst - timeSlice;
if(this.remburst <= 0 )
{
for(int i=0;i<tempburst;i++)
{
timer.tick();
}
this.remburst = 0;
System.out.println("Process "+this.pno+" has Finished executing");
this.completionTime = timer.currentTime;
this.turnaround = this.completionTime - this.submissionTime;
this.waiting = this.turnaround - this.burst;
}
else
{
System.out.println("Process "+this.pno+" is executing");
System.out.println("Remaining Time for process "+this.pno+" is :"+this.remburst);
for(int i=0;i<timeSlice;i++)
{
timer.tick();
}
}
}
}
}
class Timer
{
int currentTime;
Timer(int time)
{
this.currentTime = time;
}
void tick()
{
this.currentTime = this.currentTime + 1;
}
}
No comments:
Post a Comment