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:
Its priority is not functioning well.I dont know if its logical error./* * 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(); }