Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help w/ my CPU Scheduler!!

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    15
    My Mood
    Nerdy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help w/ my CPU Scheduler!!

    This is my new thread as what some guys told me..
    My CPU Scheduler will be passed two weeks after.So please help me on completing my work.
    Im having difficulties on finding out some logic here in my project.Can you help me with these?It'll help me a lot.This is how it should work:the user will select what kind of Scheduling he/she desires(Multi-level Queue or Multi-level Feedback),then different algorithms will be selected based on what he/she like(RR,SJF-P,SJF-NP and FCFS).
    Here's my Code for Multi-Level Queue:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    /*
     * queueSched.java
     *
     * Created on Oct 6, 2011, 3:40:05 PM
     */
     
    package guimultilevel;
    import java.util.*;
    import java.awt.*;
    /**
     *
     * @author Administrator
     */
    public class queueSched extends javax.swing.JFrame {
     
        /** Creates new form queueSched */
        public queueSched() {
            initComponents();
        }
     
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
             pack();
        }// </editor-fold>                        
     
        private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {                                       
            int ArrivalT = Integer.parseInt(txtAT.getText());
            int BurstT = Integer.parseInt(txtBT.getText());
            int Priority = Integer.parseInt(txtPrior.getText());
            ProcessQueue process = new ProcessQueue(ArrivalT,BurstT,Priority );
            Temp.add(process);
            final String[] strings1 = new String[lstProcess.getModel().getSize()+1];
            for (int i= 0 ;i <lstProcess.getModel().getSize();i++)
            strings1[i] = (String)lstProcess.getModel().getElementAt(i);
            strings1[lstProcess.getModel().getSize()] = "P"+process.ID +"     "+
            process.ArrivalTime + "      "+process.BurstTime + "      "+process.Priority;
            lstProcess.setModel(new javax.swing.AbstractListModel() {
               final String[] strings = strings1;
                public int getSize() { return strings.length; }
                public Object getElementAt(int i) { return strings[i]; }
            });
            lstProcess.repaint();
    }                                      
     
        private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {                                         
            chkboxFCFS.setSelected(false);
            chkboxRR.setSelected(false);
            chkboxSJF.setSelected(false);
            ctr=0;
            lstProcess.setModel(new javax.swing.AbstractListModel() {
               final String[] strings = {};
                public int getSize() { return strings.length; }
                public Object getElementAt(int i) { return strings[i]; }
            });
            lstAlgorithim.setModel(new javax.swing.AbstractListModel() {
               final String[] strings = {};
                public int getSize() { return strings.length; }
                public Object getElementAt(int i) { return strings[i]; }
            });
            alg.removeAllElements();
            Temp.removeAllElements();
            ProcessQueue.countID =0;
            ProcessQueue.SliceOrder = 0;
        }                                        
     
        private void btnStartActionPerformed(java.awt.event.ActionEvent evt) {                                         
            ProcessQueue.countID =0;
            ProcessQueue.SliceOrder = 0;
     
            if(P.isEmpty()){
                for(int x = 0 ;x< Temp.size();x++){
                P.add(new ProcessQueue(((ProcessQueue)Temp.elementAt(x)).ArrivalTime,((ProcessQueue)Temp.elementAt(x)).BurstTime,
                ((ProcessQueue)Temp.elementAt(x)).Priority));
                System.out.println( "P "+((ProcessQueue)Temp.elementAt(x)).ID +"     "+((ProcessQueue)Temp.elementAt(x)).ArrivalTime + "      "+((ProcessQueue)Temp.elementAt(x)).BurstTime +
                   "      "+((ProcessQueue)Temp.elementAt(x)).Priority);
                }
            }
     
        //start applying the algorithms every unit of time
     
            ProcessQueue.setSliceTime(Integer.parseInt(txtTQ.getText()));
        //set RR time slice
            execTime = 0;
            execution:
            for (execTime = 0 ; execTime >-1;execTime++){
            if (index == -1){
                //no process running
                if((index = WhichToRun(CPUSched))==-1)
                // no porcess arrived
                continue execution;
                else{
                //first process arrived
                ((ProcessQueue)P.elementAt(index)).run(execTime);
                continue execution;
                }
            }
            else if(index != WhichToRun(CPUSched) && preempt) {
            //different priority and the algorithm is preemptable
                if( !((ProcessQueue)P.elementAt(index)).stop(execTime) )
                //preempt the current process
                P.removeElementAt(index); //process finished executing
                index = -1;
                    if((index= WhichToRun(CPUSched))== -1 && execTime <=-1)
                        //all process finished execting
                        break execution;
                    else if((index= WhichToRun(CPUSched))== -1)
                        continue execution;
                        ((ProcessQueue)P.elementAt(index)).run(execTime);
            }
            else {
                //same priority
                if (((ProcessQueue)P.elementAt(index)).run(execTime))
                    //finished executing
                    continue execution;
                    ((ProcessQueue)P.elementAt(index)).stop(execTime)  ;
                    P.removeElementAt(index);
                    index = -1;
                    if((index= WhichToRun(CPUSched))== -1 && execTime <= -1)
                        break execution;
                    else if((index= WhichToRun(CPUSched))== -1)
                        continue execution;
                    ((ProcessQueue)P.elementAt(index)).run(execTime);
            }
     
            }
            repaint();
        }                                        
     
        private void btnFCFSActionPerformed(java.awt.event.ActionEvent evt) {                                        
            chkboxFCFS.setSelected(true);
            ctr++;
            ProcessQueue process2 = new ProcessQueue();
            Temp.add(process2);
            final String[] strings1 = new String[lstAlgorithim.getModel().getSize()+1];
            for (int i= 0 ;i <lstAlgorithim.getModel().getSize();i++)
            strings1[i] = (String)lstAlgorithim.getModel().getElementAt(i);
            strings1[lstAlgorithim.getModel().getSize()] = "FCFS";
            lstAlgorithim.setModel(new javax.swing.AbstractListModel() {
               final String[] strings = strings1;
                public int getSize() { return strings.length; }
                public Object getElementAt(int i) { return strings[i]; }
            });
            lstAlgorithim.repaint();
        }                                       
     
        private void btnRRActionPerformed(java.awt.event.ActionEvent evt) {                                      
            chkboxRR.setSelected(true);
            ctr++;
            ProcessQueue process2 = new ProcessQueue();
            Temp.add(process2);
            final String[] strings1 = new String[lstAlgorithim.getModel().getSize()+1];
            for (int i= 0 ;i <lstAlgorithim.getModel().getSize();i++)
            strings1[i] = (String)lstAlgorithim.getModel().getElementAt(i);
            strings1[lstAlgorithim.getModel().getSize()] = "Round Robin";
            lstAlgorithim.setModel(new javax.swing.AbstractListModel() {
               final String[] strings = strings1;
                public int getSize() { return strings.length; }
                public Object getElementAt(int i) { return strings[i]; }
            });
            lstAlgorithim.repaint();
        }                                     
     
        private void radbtnPStateChanged(javax.swing.event.ChangeEvent evt) {                                     
            preempt=radbtnP.isSelected();
    }                                    
     
        private void radbtnNPStateChanged(javax.swing.event.ChangeEvent evt) {                                      
            preempt=radbtnNP.isSelected();
    }                                     
     
        private void btnSJFPActionPerformed(java.awt.event.ActionEvent evt) {                                        
            chkboxSJF.setSelected(true);
            ctr++;
            ProcessQueue process2 = new ProcessQueue();
            Temp.add(process2);
            final String[] strings1 = new String[lstAlgorithim.getModel().getSize()+1];
            for (int i= 0 ;i <lstAlgorithim.getModel().getSize();i++)
            strings1[i] = (String)lstAlgorithim.getModel().getElementAt(i);
            strings1[lstAlgorithim.getModel().getSize()] = "SJF";
            lstAlgorithim.setModel(new javax.swing.AbstractListModel() {
               final String[] strings = strings1;
                public int getSize() { return strings.length; }
                public Object getElementAt(int i) { return strings[i]; }
            });
            lstAlgorithim.repaint();
        }                                       
     
        private void chkboxSJFStateChanged(javax.swing.event.ChangeEvent evt) {                                       
            chkboxSJF.isSelected();
            CPUSched=2;
            if(CPUSched==2){
            radbtnP.setSelected(true);
            radbtnP.setEnabled(true);
            radbtnNP.setEnabled(true);
            }
            else {
            radbtnP.setSelected(false);
            radbtnP.setEnabled(false);
            radbtnNP.setEnabled(false);
            }
        }                                      
     
        private void chkboxFCFSStateChanged(javax.swing.event.ChangeEvent evt) {                                        
            CPUSched=1;
        }                                       
     
        private void chkboxRRStateChanged(javax.swing.event.ChangeEvent evt) {                                      
            CPUSched=4;
            radbtnP.setSelected(true);
        }                                     
    public int WhichToRun(int CPU){
    //    finds what process runs first
        int PriorityID =0;
        PriorityID=index;
            int arrived = 0;
        if (PriorityID == -1)
            PriorityID = 0;
     
            if(P.isEmpty()){
                execTime =-2;
         return -1;
         }
       for (int i=0 ; i<=(P.size()-1);i++){
        if (((ProcessQueue)P.elementAt(0)).ArrivalTime > execTime)
         return -1;
       if (((ProcessQueue)P.elementAt(i)).ArrivalTime <= execTime){
           ++arrived;
           if(CPU == 2)
       ((ProcessQueue)P.elementAt(i)).setInOrder();
       }
       else
           break;
       }
        for (int i=0 ; i<arrived;i++) {
        if (((ProcessQueue)P.elementAt(i)).getPriority(CPU) < ((ProcessQueue)P.get(PriorityID)).getPriority(CPU)){
            PriorityID = i;
        }
      }
      return PriorityID;
      }
    //drawing of Gantt Chart
     @Override
    public void paint(Graphics g){
        super.paint(g);
        Graphics2D g2d = (Graphics2D)g;
        for(int i=0;i<draw.size()-(ctr*2);i++){
        g2d.setColor(Color.BLACK);
        g2d.drawString("|",((Integer)draw.elementAt(i))*20 +50,480);
        g2d.drawString(""+(Integer)draw.elementAt(i),((Integer)draw.elementAt(i))*20 +50,495);
            if ((i%2)==0){
                g2d.setColor(Color.RED);
                g2d.drawString("P"+(Integer)drawP.elementAt(i/2),((Integer)draw.elementAt(i))*20 +55,480);
            }
        }
        g2d.setColor(Color.BLACK);
        g2d.drawRect(50,470,900,12);
        draw.removeAllElements();
        drawP.removeAllElements();
    }
     
        /**
        * @param args the command line arguments
        */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new queueSched().setVisible(true);
                }
            });
        }
        Vector Temp = new Vector();
        Vector P = new Vector();
        Vector alg = new Vector();
        int execTime =0;
        int RID=0,ctr=0;
        int index = -1;
        boolean  preempt =false;
        int algo = 1;
        int CPUSched=1;
        public static Vector draw = new Vector();
        public static Vector drawP = new Vector();
    }
    Its priority is not functioning well.I dont know if its logical error.
    Last edited by JavaPF; October 10th, 2011 at 07:47 AM. Reason: Please use highlight tags!! see my signature


  2. #2
    Junior Member
    Join Date
    Oct 2011
    Posts
    15
    My Mood
    Nerdy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help w/ my CPU Scheduler!!

    And here is how it computes some sort of BT and AT.
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package guimultilevel;
    import java.util.Queue.*;
    /**
     *
     * @author Administrator
     */
    public class ProcessQueue {
        static int countID =0;
        int ID ;
         int BurstTime;
         int Priority;
         int ArrivalTime;
         int RemainingTime =0;
         boolean Running;
         int RunningTime;
         static int SliceOrder =0;
         static int  SliceTime ;
         int Order =0;
     
        /** Creates a new instance of Process */
        public ProcessQueue() {
        }
        public ProcessQueue(int arrived, int burst,int priority){
         ArrivalTime = arrived ;
         BurstTime = burst ;
         Priority = priority ;
         RemainingTime = BurstTime;
         ++countID;
         SliceOrder = countID;
         ID =countID;
        }
     
        public static void setSliceTime(int slice){
         SliceTime = slice;
        }
    public boolean run(int Time){
        if(Running){
     
           -- RemainingTime;
           RunningTime++;
           if(RunningTime >=SliceTime){
            ++SliceOrder;
            Order = SliceOrder;
           }
     
        }
        else{
     
           Running=true;
           RunningTime =1;
          if(RunningTime >=SliceTime){
            ++SliceOrder;
            Order = SliceOrder;
           }
        queueSched.draw.add(Time);
        queueSched.drawP.add(ID);
        System.out.println("P"+ID+ " started at "+ Time);
        }
      if (RemainingTime<= 0)
            return false;
            return true;
     
    }
     
    public boolean stop(int Time){
        queueSched.draw.add(Time);
       Running = false;
       -- RemainingTime;
       SliceOrder++;
       Order =SliceOrder;
       System.out.println("P"+ID + " stopped at "+ Time);
            if(RemainingTime== 0)
                return false;
                return true;
    }
     
    public void setInOrder(){
        if(Order == 0){
            Order = SliceOrder;
            SliceOrder++;}
    }
    public int getPriority(int Algorithm){
      switch (Algorithm){
      case 1:
          return ArrivalTime;
      case 2:
          return RemainingTime;
        case 3:
          return Priority;
        case 4:
          return Order;
            }
      return 0;
    }
     
    }
    Last edited by JavaPF; October 10th, 2011 at 07:49 AM. Reason: Pointless quoting large amounts of code. Use highlight tags also

Similar Threads

  1. Help with my CPU Scheduler
    By HacKofDeatH in forum Object Oriented Programming
    Replies: 6
    Last Post: October 12th, 2011, 01:50 AM
  2. Help with Quartz scheduler
    By ftom2 in forum Java Theory & Questions
    Replies: 0
    Last Post: March 14th, 2011, 12:28 PM