Hi programmers I need this code to be completed with
the sjf(shortest job first) to be preemptive, it means
with arrival times with each process..
Here is my code so far:
CpuScheduling1.java
Interface:
Menu
[1]First- Come First-Served
[2]Shortest Job First
[3]Priority
Enter Choice:
Note: I need to have the sjf as preemptive it means with arrival time
for each process..plz help..and if you know how to have it return to the main menu after each selection is made.. thanks
import java.util.*;
import javax.swing.*;
public class CpuScheduling1
{
public static void main(String[ ]args)
{
int m=Integer.parseInt(JOptionPane.showInputDialog( " Menu\n[1]First- Come First-Served\n[2]Shortest Job First\n[3]Priority\n\nEnter Choice:"));
if (m==1)
{
FCFS();
}
else if (m==2)
{
SJF();
}
else if (m==3)
{
P();
}
else
{
JOptionPane.showMessageDialog(null,"Error Message","Error!",JOptionPane.ERROR_MESSAGE);
}
}
public static void FCFS()
{
int bp[]=new int[5],wtp[] =new int[5],twt=0, awt,num;
String output1[]=new String[10];
for (num=0;num<=4;num++)
{
bp[num]=Integer.parseInt(JOptionPane.showInputDialog( "\nEnter Burst time for P"+(num+1)+" : "));
}
for (num=0;num<=4;num++)
{
if (num==0)
{
wtp[num]=0;
}
else
{
wtp[num]=wtp[ num-1]+bp[ num-1];
output1[num]="\nWaiting time for P"+(num+1)+" = "+wtp[num];
}
}
for (num=0;num<=4;num++)
{
twt=twt+wtp[ num];
}
awt=twt/5;
JOptionPane.showMessageDialog(null,output1,"FCFS",JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null,"\nAverage Waiting Time =" + awt,"FCFS",JOptionPane.INFORMATION_MESSAGE);
}
public static void SJF()
{
int bp[]=new int[5],wtp[] =new int[5],twt=0, awt,x,num, temp=0,p[ ]=new int[5];
boolean found=false;
for (num=0;num<=4;num++)
{
bp[num]=Integer.parseInt(JOptionPane.showInputDialog( "\nEnter Burst time for P"+(num+1)+" : "));
}
for (num=0;num<=4;num++)
{
p[num]=bp[num] ;
}
for (x=0;x<=3;x++)
{
for (num=0;num<=3;num++)
{
if (p[num]>p[num+1])
{
temp=p[num];
p[num]=p[num+ 1];
p[num+1]=temp;
}
}
}
for (num=0;num<=4;num++)
{
if (num==0)
{
for (x=0;x<=4;x++)
{
if (p[num]==bp[ x] && found==false)
{
wtp[num]=0;
JOptionPane.showMessageDialog(null, "\nWaiting time for P"+(x+1)+" = "+wtp[num]);
bp[x]=0;
found=true;
}
}
found=false;
}
else
{
for (x=0;x<=4;x++)
{
if (p[num]==bp[ x] && found==false)
{
wtp[num]=wtp[ num-1]+p[ num-1];
JOptionPane.showMessageDialog(null,"\nWaiting time for P"+(x+1)+" = "+wtp[num]);
bp[x]=0;
found=true;
}
}
found=false;
}
}
for (num=0;num<=4;num++)
{
twt=twt+wtp[ num];
}
JOptionPane.showMessageDialog(null,"\n\nAverage waiting time: "+(awt=twt/5) );
}
public static void P()
{
int bp[]=new int[5],wtp[] =new int[6],p[]=new int[5],sp[]= new int[5],twt=0, awt,num,x, temp=0;
boolean found=false;
for (num=0;num<=4;num++)
{
bp[num]=Integer.parseInt(JOptionPane.showInputDialog( "\nEnter Burst time for P"+(num+1)+" : "));
}
for (num=0;num<=4;num++)
{
p[num]=Integer.parseInt(JOptionPane.showInputDialog( "\nEnter Priority for P"+(num+1)+" : "));
}
for (num=0;num<=4;num++)
{
sp[num]=p[num] ;
}
for (x=0;x<=3;x++)
{
for (num=0;num<=3;num++)
{
if (sp[num]>sp[num+1])
{
temp=sp[num] ;
sp[num]=sp[num+ 1];
sp[num+1]=temp;
}
}
}
for (num=0;num<=4;num++)
{
if (num==0)
{
for (x=0;x<=4;x++)
{
if (sp[num]==p[ x] && found==false)
{
wtp[num]=0;
JOptionPane.showMessageDialog(null,"\nWaiting time for P"+(x+1)+" = "+wtp[num]);
temp=x;
p[x]=0;
found=true;
}
}
found=false;
}
else
{
for (x=0;x<=4;x++)
{
if (sp[num]==p[ x] && found==false)
{
wtp[num]=wtp[ num-1]+bp[ temp];
JOptionPane.showMessageDialog(null,"\nWaiting time for P"+(x+1)+" = "+wtp[num]);
temp=x;
p[x]=0;
found=true;
}
}
found=false;
}
}
for (num=0;num<=4;num++)
{
twt=twt+wtp[ num];
}
JOptionPane.showMessageDialog(null,"\n\nAverage waiting time: "+(awt=twt/5) );
}
}