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 
 | 
  
Turning Time ((Waiting time – Arrival Time)+Burst Time):
            Waiting                Arrival
Time             Waiting Time              Turning Time
                 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.