Thursday, January 29, 2009

Preemptive Priority Scheduling Algorithms

Priority Scheduling
  • A preemptive priority algorithm will preemptive the CPU if the priority of the newly arrival process is higher than the priority of the currently running process.


SAMPLE INPUT:

Process
Burst Time
Arrival Time
Priority
P1
3
0
3
P2
2
1
2
P3
1
2
4
P4
1
3
2
P5
2
4
1
P6
4
5
5


GANTT CHART:


0
1
2
3
4
5
6
7
8
9
10
11
12    

P1
P2
P2
P4
P5
P5
P1
P1
P3
P6
P6
P6
P6


Waiting Time (Waiting time – Arrival Time):
Turning Time ((Waiting time – Arrival Time)+Burst Time):
           
            Waiting                Arrival Time             Waiting Time              Turning Time
                 5                              0                        (5-0)=5                       (5-0)+3=8
                 1                              1                        (1-1)=0                       (1-1)+2=2
                 8                              2                        (8-2)=6                       (8-2)+1=7
                 3                              3                        (3-3)=0                       (3-3)+1=1
                 4                              4                        (4-4)=0                       (4-4)+2=2
                 9                              5                        (9-5)=4                       (9-5)+4=8                                                               Total Waiting Time 15            Total Turning Time 28


Average Waiting Time = Total Waiting Time / 6
       = 15 / 6
       = 2.5

Average Turning Time = Total Turning Time / 6
       = 28 / 6
       = 4.6667





Design:

Codes:

-->
Preemptive Priority Scheduling Algorithm in JAVA
/*~~~~~~~~~~Frame PreemptivePriority.java~~~~~~~~~~~*/
import javax.swing.*;
import java.lang.*;
public class PreemptivePriority extends javax.swing.JFrame {
/** Creates new form PreemptivePriority */
public PreemptivePriority() {
initComponents();
}
PriorityMethod m = new PriorityMethod();
int[] B = new int[10];
int[] A = new int[10];
int[] P = new int[10];
/*~~~~~~~~~~~Button Calculate~~~~~~~~~~~~*/
private void btnCalculateMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
B[1]=Integer.parseInt(txtB1.getText());
B[2]=Integer.parseInt(txtB2.getText());
B[3]=Integer.parseInt(txtB3.getText());
B[4]=Integer.parseInt(txtB4.getText());
B[5]=Integer.parseInt(txtB5.getText());
B[6]=Integer.parseInt(txtB6.getText());
A[1]=Integer.parseInt(txtA1.getText());
A[2]=Integer.parseInt(txtA2.getText());
A[3]=Integer.parseInt(txtA3.getText());
A[4]=Integer.parseInt(txtA4.getText());
A[5]=Integer.parseInt(txtA5.getText());
A[6]=Integer.parseInt(txtA6.getText());
P[1]=Integer.parseInt(txtP1.getText());
P[2]=Integer.parseInt(txtP2.getText());
P[3]=Integer.parseInt(txtP3.getText());
P[4]=Integer.parseInt(txtP4.getText());
P[5]=Integer.parseInt(txtP5.getText());
P[6]=Integer.parseInt(txtP6.getText());
String str = String.valueOf(m.AverageWaitingTime(B,A,P));
Double f = new Double(str);
double d = f.doubleValue();
d=d*100.0;
long dI = (long)d;
double e = ((double)dI)/100.0;
String str2 = String.valueOf(m.AverageTurningTime(B,A,P));
Double f2 = new Double(str2);
double d2 = f2.doubleValue();
d2=d2*100.0;
long dI2 = (long)d2;
double e2 = ((double)dI2)/100.0;
txtAwt.setText(String.valueOf(e));
txtAtt.setText(String.valueOf(e2));
}
/*~~~~~~~~~~~End Button Calculate~~~~~~~~~~~~*/
/*~~~~~~~~~~~Button New~~~~~~~~~~~~*/
private void btnSaveMouseClicked1(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
txtB1.setText("");
txtB2.setText("");
txtB3.setText("");
txtB4.setText("");
txtB5.setText("");
txtB6.setText("");
txtA1.setText("");
txtA2.setText("");
txtA3.setText("");
txtA4.setText("");
txtA5.setText("");
txtA6.setText("");
txtP1.setText("");
txtP2.setText("");
txtP3.setText("");
txtP4.setText("");
txtP5.setText("");
txtP6.setText("");
txtB1.setEnabled(true);
txtB2.setEnabled(true);
txtB3.setEnabled(true);
txtB4.setEnabled(true);
txtB5.setEnabled(true);
txtB6.setEnabled(true);
txtA1.setEnabled(true);
txtA2.setEnabled(true);
txtA3.setEnabled(true);
txtA4.setEnabled(true);
txtA5.setEnabled(true);
txtA6.setEnabled(true);
txtP1.setEnabled(true);
txtP2.setEnabled(true);
txtP3.setEnabled(true);
txtP4.setEnabled(true);
txtP5.setEnabled(true);
txtP6.setEnabled(true);
btnNew.setEnabled(false);
btnSave.setEnabled(true);
txtB1.requestFocus();
txtAwt.setText("");
txtAtt.setText("");
table1.removeAll();
btnCalculate.setEnabled(false);
}
/*~~~~~~~~~~~End Button New~~~~~~~~~~~~*/
/*~~~~~~~~~~~Button Save~~~~~~~~~~~~*/
private void btnSaveMouseClicked(java.awt.event.MouseEvent evt) {
if(txtB1.getText()=="" || txtB2.getText()=="" || txtB3.getText()=="" || txtB4.getText()=="" || txtB5.getText()=="" || txtB6.getText()=="" || txtA1.getText()=="" || txtA2.getText()=="" || txtA3.getText()=="" || txtA4.getText()=="" || txtA5.getText()=="" || txtA6.getText()=="" || txtP1.getText()=="" || txtP2.getText()=="" || txtP3.getText()=="" || txtP4.getText()=="" || txtP5.getText()=="" || txtP6.getText()=="")
JOptionPane.showMessageDialog(null,"Filled-up first all fields to continue...");
else{
table1.setValueAt(lbl1.getText(),0,0);
table1.setValueAt(txtB1.getText(),0,1);
table1.setValueAt(txtA1.getText(),0,2);
table1.setValueAt(txtP1.getText(),0,3);
table1.setValueAt(lbl2.getText(),1,0);
table1.setValueAt(txtB2.getText(),1,1);
table1.setValueAt(txtA2.getText(),1,2);
table1.setValueAt(txtP2.getText(),1,3);
table1.setValueAt(lbl3.getText(),2,0);
table1.setValueAt(txtB3.getText(),2,1);
table1.setValueAt(txtA3.getText(),2,2);
table1.setValueAt(txtP3.getText(),2,3);
table1.setValueAt(lbl4.getText(),3,0);
table1.setValueAt(txtB4.getText(),3,1);
table1.setValueAt(txtA4.getText(),3,2);
table1.setValueAt(txtP4.getText(),3,3);
table1.setValueAt(lbl5.getText(),4,0);
table1.setValueAt(txtB5.getText(),4,1);
table1.setValueAt(txtA5.getText(),4,2);
table1.setValueAt(txtP5.getText(),4,3);
table1.setValueAt(lbl6.getText(),5,0);
table1.setValueAt(txtB6.getText(),5,1);
table1.setValueAt(txtA6.getText(),5,2);
table1.setValueAt(txtP6.getText(),5,3);
txtB1.setEnabled(false);
txtB2.setEnabled(false);
txtB3.setEnabled(false);
txtB4.setEnabled(false);
txtB5.setEnabled(false);
txtB6.setEnabled(false);
txtA1.setEnabled(false);
txtA2.setEnabled(false);
txtA3.setEnabled(false);
txtA4.setEnabled(false);
txtA5.setEnabled(false);
txtA6.setEnabled(false);
txtP1.setEnabled(false);
txtP2.setEnabled(false);
txtP3.setEnabled(false);
txtP4.setEnabled(false);
txtP5.setEnabled(false);
txtP6.setEnabled(false);
btnNew.setEnabled(true);
btnSave.setEnabled(false);
btnCalculate.setEnabled(true);
}
}
/*~~~~~~~~~~~End Button Save~~~~~~~~~~~~*/
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new PreemptivePriority().setVisible(true);
}
});
}
/*~~~~~~~~~~~~~~Method PriorityMethod.java~~~~~~~~~~~~~~~~~~~*/
import javax.swing.*;
public class PriorityMethod {
public double AverageWaitingTime(int[] burst, int[] arrival, int[] priority){
int i, j = 0, Tt=0, Time=0, min, max1=0;
char[] S = new char[10];
char[] start = new char[10];
int[] Wt = new int[10];
int[] B = new int[10];
int[] A = new int[10];
int[] P = new int[10];
double Twt=0.0, Awt;
for(i=1;i<=6;i++)
{
B[i]=burst[i];
A[i]=arrival[i];
P[i]=priority[i];
Wt[i]=0;
S[i]='T';
start[i]='F';
Tt=Tt+B[i];
if(A[i]>Time)
Time=A[i];
if(P[i]>max1)
max1=P[i];
}
int w=0,flag=0,t=0;
i=1;
while(t
{
if(A[i]<=t && B[i]!=0)
{
if(flag==0)
{
Wt[i]=Wt[i]+w;
}
B[i]=B[i]-1;
if(B[i]==0)
S[i]='F';
start[i]='T';
t++;
w=w+1;
if(S[i]!='F')
{
j=1;flag=1;
while(j<=6 && flag!=0)
{
if(S[j]!='F' && P[i]>P[j] && A[j]<=t && i!=j)
{
flag=0;
Wt[i]=Wt[i]-w;
i=j;
}
else
{
flag=1;
}
j++;
}
}
else
{
i++;
j=1;
while(A[j]<=t &&j<=6)
{
if(P[i]>P[j] && S[j]!='F')
{
flag=0;
i=j;
}
j++;
}
}
}
else{
if(flag==0)
i++;
t++;
}
}
while(w
{
min=max1+1;
i=1;
while(i<=6)
{
if(min>P[i] && S[i]=='T')
{
min=P[i];
j=i;
}
i++;
}
i=j;
if(w==Time && start[i]=='T')
{
w=w+B[i];
S[i]='F';
}
else
{
Wt[i]=Wt[i]+w;
w=w+B[i];
S[i]='F';
}
}
for(i=1;i<=6;i++)
Twt=Twt+(Wt[i]-A[i]);
Awt=Twt/6;
JOptionPane.showMessageDialog(null,"Waiting Time Info for every Process\n\tP1 = "+
(Wt[1]-A[1])+"\n\tP2 = "+(Wt[2]-A[2])+"\n\tP3 = "+(Wt[3]-A[3])+"\n\tP4 = "+
(Wt[4]-A[4])+"\n\tP5 = "+(Wt[5]-A[5])+"\n\tP6 = "+(Wt[6]-A[6]));
return Awt;
}
public double AverageTurningTime(int[] burst, int[] arrival, int[] priority){
int i, j = 0, Tt=0, Time=0, min, max1=0;
char[] S = new char[10];
char[] start = new char[10];
int[] Wt = new int[10];
int[] B = new int[10];
int[] A = new int[10];
int[] P = new int[10];
int[] Wtm = new int[10];
double Ttt=0.0, Att;
for(i=1;i<=6;i++)
{
B[i]=burst[i];
Wtm[i]=B[i];
A[i]=arrival[i];
P[i]=priority[i];
Wt[i]=0;
S[i]='T';
start[i]='F';
Tt=Tt+B[i];
if(A[i]>Time)
Time=A[i];
if(P[i]>max1)
max1=P[i];
}
int w=0,flag=0,t=0;
i=1;
while(t
{
if(A[i]<=t && B[i]!=0)
{
if(flag==0)
{
Wt[i]=Wt[i]+w;
}
B[i]=B[i]-1;
if(B[i]==0)
S[i]='F';
start[i]='T';
t++;
w=w+1;
if(S[i]!='F')
{
j=1;flag=1;
while(j<=6 && flag!=0)
{
if(S[j]!='F' && P[i]>P[j] && A[j]<=t && i!=j)
{
flag=0;
Wt[i]=Wt[i]-w;
i=j;
}
else
{
flag=1;
}
j++;
}
}
else
{
i++;
j=1;
while(A[j]<=t &&j<=6)
{
if(P[i]>P[j] && S[j]!='F')
{
flag=0;
i=j;
}
j++;
}
}
}
else{
if(flag==0)
i++;
t++;
}
}
while(w
{
min=max1+1;
i=1;
while(i<=6)
{
if(min>P[i] && S[i]=='T')
{
min=P[i];
j=i;
}
i++;
}
i=j;
if(w==Time && start[i]=='T')
{
w=w+B[i];
S[i]='F';
}
else
{
Wt[i]=Wt[i]+w;
w=w+B[i];
S[i]='F';
}
}
for(i=1;i<=6;i++)
Ttt=Ttt+((Wt[i]-A[i])+Wtm[i]);
Att=Ttt/6;
JOptionPane.showMessageDialog(null,"Turning Time Info for every Process\n\tP1 = "+
((Wt[1]-A[1])+Wtm[1])+"\n\tP2 = "+((Wt[2]-A[2])+Wtm[2])+"\n\tP3 = "+
((Wt[3]-A[3])+Wtm[3])+"\n\tP4 = "+((Wt[4]-A[4])+Wtm[4])+"\n\tP5 = "+
((Wt[5]-A[5])+Wtm[5])+"\n\tP6 = "+((Wt[6]-A[6])+Wtm[6]));
return Att;
}
}



Download link of this project click here.
Note: When clicking the link you directly go to ads site, just wait 5 seconds then click Skip Ads to continue. Thank you.

16 comments:

  1. kuya hinahanap po ung mraming txt
    cannot find variable at cannot find method. meron po bang kulang jan? pa send naman po kung meron
    mr.lodalicious@gmail.com slamat:)

    ReplyDelete
  2. Wala poh kulang ito...kompleto poh ito...try mo lng nsa baba ang method naka-comment n yan...

    ReplyDelete
  3. kuya kahit ilang jobs ba pwede iinput jan?? ang language po gamit nyo?
    eto email ko pki email nman po
    prettyjerk_05@yahoo.com

    ReplyDelete
  4. tinry q po ebuild sa textpad at netbeans unfortunately may error po sa while(t,while w na loops..ano po gagawin jan?

    ReplyDelete
  5. Replies
    1. Sir, nalaman niyo na po ba kung ano ung variable t na nagamit?

      Delete
  6. Netbeans po gamitin mu tiyak gagana yan... at saka configure mulang sa properties ng table s design mu.. this is working project I made I took a lot of research for this because no other shared sites discussing about this topic. Preemptive Priority is the hardest scheduling algorithm specially in java flatform.

    ReplyDelete
  7. @prettyjerk05 -- you must input 6 jobs, but you can edit my sourcecode to make it as many jobs as you can. Declare k lng ng variable para gamitin mu sa mga looping. Kasi 6 jobs lng ang require s professor namin kaya yan lng ang ginawa ko..

    ReplyDelete
    Replies
    1. yea i'm trying for seven jobs but it is giving me error msg at while :"incompatible types
      required: boolean
      found: int

      ')' expected"
      ----pls i desperately need ur help.

      Delete
  8. my error po sa line to
    while(t
    {
    if(A[i]<=t && B[i]!=0)
    {
    `` incompatible types po int cannot be converted to boolean

    ReplyDelete
  9. kuya henlord san po ba dito yong main class? hindi po kasi gumagana, nag hahanap ng main class, pwede po paki sagot.. salamat po :D

    ReplyDelete
  10. HIndi mo nagana kapag 10 jobs ang input

    ReplyDelete
  11. sorry to bother you but i'm trying to get this to work but it throws errors at the while(t, while(w. pls can you help? thanks

    ReplyDelete
  12. the code is not in a package it is the class PriorityMethod
    is not defined in class PreemptivePriority

    ReplyDelete
  13. 1 of 19 results for citizen titanium dive watch
    Download our latest 1-of-19 results for citizen titanium dive watch ford focus titanium hatchback on iTain titanium apple watch today. T&Cs benjamin moore titanium apply. View latest 1-of-19 results with 498 welding titanium ratings. ford escape titanium for sale

    ReplyDelete