Shortest Job First ( SJF) Algorithm in java with graph plot and dynamic process burst time.
This program is an example of how computer arranges the processes while executing different process. This is one of the oldest and the easiest processing technique.
What it does is that it basically checks for the shortest job in the queue and executes it first so that the larger task are pushed at last.
I have demonstrated it using a graph where y axis shows waiting time and x axis shows process id's.
P_ID is Process ID
P_TIME is Process time required.
W_TIME is the wating time calculated by the machine based on the SJF Algorithm.
Note: The program takes dynamic P_TIME, So you will always get a different graph.
The class name defined here is sjf make sure if you use different class names change the sjf to your class name.
CODE:
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Rectangle2D;
class sjf extends JPanel {
static int process1[] = new int[3];
static int ptime1[] = new int[3];
static int wtime1[] = new int[3];
static int n,total1;
static float avg1;
public static void main(String args[])
{
int process[] = new int[3];
int ptime[] = new int[3];
int wtime[] = new int[3];
int temp, total=0;
float avg=0;
long l=System.currentTimeMillis()%10;
n=3;
process[0]=1;
process[1]=2;
process[2]=3;
if(l>=5)
{
ptime[2]=(int)(l-3);
ptime[1]=(int)(l-4);
ptime[0]=(int)(l-5);
}
if(l<5)
{
ptime[2]=(int)(l);
ptime[1]=(int)(l+2);
ptime[0]=(int)(l+5);
}
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++) { if(ptime[i]>ptime[j])
{
temp = ptime[i];
ptime[i] = ptime[j];
ptime[j] = temp;
temp = process[i];
process[i] = process[j];
process[j] = temp;
}
}
}
wtime[0] = 0;
for(int i=1;i<n;i++)
{
wtime[i] = wtime[i-1]+ptime[i-1];
total = total + wtime[i];
}
total1=total;
avg = (float)total/n;
avg1=avg;
for(int i=0;i<n;i++)
{
process1[i]=process[i];
ptime1[i]=ptime[i];
wtime1[i]=wtime[i];
}
JFrame f1 = new JFrame();
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f1.getContentPane().add(new sjf()); //remember sjf() is the name of the class you seen above
import java.awt.*;
import java.awt.geom.Rectangle2D;
class sjf extends JPanel {
static int process1[] = new int[3];
static int ptime1[] = new int[3];
static int wtime1[] = new int[3];
static int n,total1;
static float avg1;
public static void main(String args[])
{
int process[] = new int[3];
int ptime[] = new int[3];
int wtime[] = new int[3];
int temp, total=0;
float avg=0;
long l=System.currentTimeMillis()%10;
n=3;
process[0]=1;
process[1]=2;
process[2]=3;
if(l>=5)
{
ptime[2]=(int)(l-3);
ptime[1]=(int)(l-4);
ptime[0]=(int)(l-5);
}
if(l<5)
{
ptime[2]=(int)(l);
ptime[1]=(int)(l+2);
ptime[0]=(int)(l+5);
}
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++) { if(ptime[i]>ptime[j])
{
temp = ptime[i];
ptime[i] = ptime[j];
ptime[j] = temp;
temp = process[i];
process[i] = process[j];
process[j] = temp;
}
}
}
wtime[0] = 0;
for(int i=1;i<n;i++)
{
wtime[i] = wtime[i-1]+ptime[i-1];
total = total + wtime[i];
}
total1=total;
avg = (float)total/n;
avg1=avg;
for(int i=0;i<n;i++)
{
process1[i]=process[i];
ptime1[i]=ptime[i];
wtime1[i]=wtime[i];
}
JFrame f1 = new JFrame();
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f1.getContentPane().add(new sjf()); //remember sjf() is the name of the class you seen above
//so change it same as your class name.
f1.setSize(400,400);
f1.setLocation(200,200);
f1.setVisible(true);
}
/**
*
*/
final int PAD = 20;
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int w = getWidth();
int h = getHeight();
int v=30;
g2.drawString("P_ID P_TIME W_TIME",200,30);
for(int i=0;i<n;i++)
{
v=v+20;
String s= Integer.toString(process1[i]);
g2.drawString(" "+s+" "+ptime1[i]+" "+wtime1[i],200,v);
}
v=v+20;
g2.drawString("Total Waiting Time: "+total1,200,v);
v=v+20;
g2.drawString("Average Waiting Time: "+avg1,200,v);
g2.drawLine(PAD, PAD, PAD, h-PAD);
g2.drawLine(PAD, h-PAD, w-PAD, h-PAD);
g2.setPaint(Color.red);
g2.drawString("0",15 ,h-10);
g2.drawString("1",35 ,h-10);
g2.drawString("2",65 ,h-10);
g2.drawString("3",95 ,h-10);
g2.drawString("P",65 ,h);
g2.drawString("I",95 ,h);
g2.drawString("D",125 ,h);
g2.drawString("1",10 ,h-40);
g2.drawString("2",10 ,h-70);
g2.drawString("3",10 ,h-100);
g2.drawString("4",10 ,h-130);
g2.drawString("5",10 ,h-160);
g2.drawString("6",10 ,h-190);
g2.drawString("7",10 ,h-220);
g2.drawString("8",10 ,h-250);
g2.drawString("9",10 ,h-280);
g2.drawString("10",5 ,h-310);
g2.drawString("E",2 ,h-30);
g2.drawString("M",2 ,h-50);
g2.drawString("I",2 ,h-70);
g2.drawString("T",2 ,h-90);
g2.drawString("G",2 ,h-130);
g2.drawString("N",2 ,h-150);
g2.drawString("I",2 ,h-170);
g2.drawString("T",2 ,h-190);
g2.drawString("A",2,h-210);
g2.drawString("w",2 ,h-230);
for(int i=0;i<n;i++)
{
int x=process1[i]*30;
int wait=wtime1[i]*30;
g2.draw(new Rectangle2D.Double(x,h-PAD-wait,20,wait));
} } }//END
f1.setSize(400,400);
f1.setLocation(200,200);
f1.setVisible(true);
}
/**
*
*/
final int PAD = 20;
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int w = getWidth();
int h = getHeight();
int v=30;
g2.drawString("P_ID P_TIME W_TIME",200,30);
for(int i=0;i<n;i++)
{
v=v+20;
String s= Integer.toString(process1[i]);
g2.drawString(" "+s+" "+ptime1[i]+" "+wtime1[i],200,v);
}
v=v+20;
g2.drawString("Total Waiting Time: "+total1,200,v);
v=v+20;
g2.drawString("Average Waiting Time: "+avg1,200,v);
g2.drawLine(PAD, PAD, PAD, h-PAD);
g2.drawLine(PAD, h-PAD, w-PAD, h-PAD);
g2.setPaint(Color.red);
g2.drawString("0",15 ,h-10);
g2.drawString("1",35 ,h-10);
g2.drawString("2",65 ,h-10);
g2.drawString("3",95 ,h-10);
g2.drawString("P",65 ,h);
g2.drawString("I",95 ,h);
g2.drawString("D",125 ,h);
g2.drawString("1",10 ,h-40);
g2.drawString("2",10 ,h-70);
g2.drawString("3",10 ,h-100);
g2.drawString("4",10 ,h-130);
g2.drawString("5",10 ,h-160);
g2.drawString("6",10 ,h-190);
g2.drawString("7",10 ,h-220);
g2.drawString("8",10 ,h-250);
g2.drawString("9",10 ,h-280);
g2.drawString("10",5 ,h-310);
g2.drawString("E",2 ,h-30);
g2.drawString("M",2 ,h-50);
g2.drawString("I",2 ,h-70);
g2.drawString("T",2 ,h-90);
g2.drawString("G",2 ,h-130);
g2.drawString("N",2 ,h-150);
g2.drawString("I",2 ,h-170);
g2.drawString("T",2 ,h-190);
g2.drawString("A",2,h-210);
g2.drawString("w",2 ,h-230);
for(int i=0;i<n;i++)
{
int x=process1[i]*30;
int wait=wtime1[i]*30;
g2.draw(new Rectangle2D.Double(x,h-PAD-wait,20,wait));
} } }//END
If you get any problem in running the code please let me know or if you dont understand anything you can leave a comment or mail me.
Dont forget to say thanks :P
Take care, Have a great day ahead :)
By Sirus
No comments:
Post a Comment