import java.io.DataInputStream;
import java.io.IOException;
import datastruct.LinkedQueue;
import datastruct.Node;
/*
@author athiruban
PROGRAM TO IMPLEMENT ROUND ROBIN SCHEDULING
***/
public class App
{
static String[] process_name=null;
static int[] burst_time=null;
static int[] waiting_time=null;
static int[] turnaroundtime =null;
static int noofproc=0;
static DataInputStream dis = new DataInputStream(System.in);
static LinkedQueue lq;
static int exectime=0;
static boolean processed=false;
static int timeslice=0;
public static void readInfo()
{
processed = false;
System.out.print("\n\tEnter No Of Process : ");
try{
noofproc = Integer.parseInt(dis.readLine() ); } catch(IOException ioe){ }
//allocate memory
process_name = new String[noofproc];
burst_time = new int[noofproc];
waiting_time = new int[noofproc];
turnaroundtime = new int[noofproc];
lq = new LinkedQueue();
for(int i = 0;i<noofproc;i++)
{
System.out.print("\n\tEnter Process Name : ");
try{
process_name[i] = dis.readLine(); } catch(IOException ioe){ }
System.out.print("\n\tEnter Burst time : ");
try{
burst_time[i] = Integer.parseInt(dis.readLine() ); } catch(IOException ioe){ }
lq.addLast(process_name[i],burst_time[i]);
}
}
public static int getBurstByName(String str)
{
for(int i = 0 ;i<noofproc;i++) { if( str.equals(process_name[i]) ) return burst_time[i]; }
return 0;
}
public static int getIdByName(String str)
{
for(int i = 0 ;i<noofproc;i++) { if( str.equals(process_name[i]) ) return i; }
return 0;
}
public static void displayStatistics()
{
double wt=0;
double tt = 0;
if(processed==false){ System.out.println("Locked"); return; }
System.out.println("\nProcess Execution Statistics");
System.out.println("----------------------------");
System.out.println("\nProcessName\t"+"BurstTime\t"+"waitingTime\t"+"Turnaround Time");
for(int i =0;i<noofproc;i++)
{
System.out.println(process_name[i]+"\t\t"+burst_time[i]+"\t\t"+waiting_time[i]+"\t\t"+turnaroundtime[i]);
wt += waiting_time[i];
tt += turnaroundtime[i];
}
System.out.println("\n\tCPU Time Slice is :"+timeslice+" ms");
System.out.println("\n\tAverage Waiting Time is :"+(wt/noofproc)+" ms");
System.out.println("\n\tAverage Turnaround Time is :"+(tt/noofproc)+" ms");
System.out.println("\n\tTotal Execution Time is : "+exectime+" ms");
}
public static void process()
{
Node tnode; // reference
if(process_name == null){System.out.println("Locked");return;}
System.out.println("INITIAL STAGE");
lq.display();
System.out.print("\n\tENTER TIME SLICE > ");
try{
timeslice = Integer.parseInt(dis.readLine());
}catch(IOException ioe){ }
while(lq.getSize()>0)
{
tnode = lq.removeFirst();
if(tnode.p_no >= timeslice) {
exectime += timeslice;
tnode.p_no -= timeslice;
}
else if(tnode.p_no > 0 && tnode.p_no < timeslice) {
exectime += tnode.p_no;
tnode.p_no =0;
}
if(tnode.p_no > 0)
lq.addLast(tnode.name,tnode.p_no);
else {
System.out.println("\tP r o c e s s "+tnode.name+" i s t e r m i n a t e d");
waiting_time[getIdByName(tnode.name)]=exectime-getBurstByName(tnode.name);
turnaroundtime[getIdByName(tnode.name)]
=waiting_time[getIdByName(tnode.name)]+burst_time[getIdByName(tnode.name)];
}
lq.display();
System.out.println("\t=--------------------------------------------------=");
try{ dis.read(); } catch(IOException ioe){ }
}
processed = true;
}
public static void main(String args[])
{
int ichoice=0;
L1:
do{
System.out.println("\n\n");
System.out.println("\tIMPLEMENTING ROUND ROBIN SCHEDULING");
System.out.println("\t-----------------------------------");
System.out.println("\t1 TO READ DETAILS");
System.out.println("\t2 TO PROCESS QUEUE");
System.out.println("\t3 TO DISPLAY STATISTICS");
System.out.println("\t4 TO EXIT");
System.out.print("\n\tYour Choice > ");
try{ ichoice = Integer.parseInt(dis.readLine() ); } catch(IOException ioe){ }
switch(ichoice)
{
case 1: readInfo();
break;
case 2: process();
break;
case 3: displayStatistics();
break;
case 4:
break L1;
default:
break;
}
} while(true);
}
}
No comments:
Post a Comment