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) );


}
}

Yep. That's code, all right. Was there something you wanted to ask about?

Yep. That's code, all right. Was there something you wanted to ask about?

I need to have the SJF(SHORTEST JOB FIRST) as non preemptive..

It means it will prompt the user for 5 ARRIVAL TIMES and solve
for the waiting time per process and the average waiting time.

For those who are not familiar with
solving the sjf(pre-emption)here
is an example:

Bale nong Example..
Process /Arrival Time/ Burst Time
P1/ 0.0/ 7
P2 /2.0/ 4
P3/ 4.0/ 1
P4 /5.0/ 4

SJF (preemptive)Gantt Chart
| P1| P2| P3| P2| P4| P1|
O__2__4__5__7__11__16

Waiting time for:
P1=(11-2-0)=9
P2=(5-2-2)=1
P3=(4-4)=0
P4=(7-5)=2
Average waiting time = (9 + 1 + 0 +2)/4 = 3
pls Help..tnx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.