ariestamirra

cpu scheduller

May 19th, 2014
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.64 KB | None | 0 0
  1. package Schedulling;
  2.  
  3. import java.util.ArrayList;
  4. import Schedulling.Process;
  5.  
  6. /**
  7.  *
  8.  * @author Mirra Ariesta Amalia Adiba
  9.  */
  10. public abstract class CpuScheduler {
  11.  
  12.     public interface CpuSchedulerEvent {
  13.  
  14.         public abstract void onDetachProcess(Time time);
  15.     }
  16.  
  17.     protected int time = 0;
  18.     private Process current;
  19.     public ArrayList<Time> processTimeLine = new ArrayList<Time>();
  20.  
  21.     public void attach(Process current) {
  22.         this.current = current;
  23.         if (current != null) {
  24.             processTimeLine.add(new Time(current.getNama(), time));
  25.         }
  26.     }
  27.  
  28.     public void detach() {
  29.         if (current != null) {
  30.             processTimeLine.get(processTimeLine.size() - 1).end = time;
  31.             if (event != null) {
  32.                 event.onDetachProcess(processTimeLine.get(processTimeLine.size() - 1));
  33.             }
  34.         }
  35.         current = null;
  36.     }
  37.  
  38.     public Process getCurrent() {
  39.         return current;
  40.     }
  41.  
  42.     public CpuSchedulerEvent event;
  43.  
  44.     protected ArrayList<Schedulling.Process> listProcess;
  45.  
  46.     public CpuScheduler() {
  47.         listProcess = new ArrayList<Schedulling.Process>();
  48.     }
  49.  
  50.     public void addProses(Process process) {
  51.         listProcess.add(process);
  52.     }
  53.  
  54.     public ArrayList<Process> getListProcess() {
  55.         return listProcess;
  56.     }
  57.  
  58.     private double sumWaitingTime() {
  59.         int result = 0;
  60.         for (int i = 0; i < listProcess.size(); i++) {
  61.             result += (listProcess.get(i).getWaitingTime());
  62.         }
  63.         return result;
  64.     }
  65.  
  66.     protected ArrayList<Process> getProcessQueue(Process except) {
  67.         ArrayList<Process> result = new ArrayList<Process>();
  68.         for (int i = 0; i < listProcess.size(); i++) {
  69.             if (listProcess.get(i).getArrivalTime() <= time && listProcess.get(i).getCurrentBurstTime() > 0) {
  70.                 if (listProcess.get(i) != except) {
  71.                     result.add(listProcess.get(i));
  72.                 }
  73.             }
  74.         }
  75.         return result;
  76.     }
  77.  
  78.     protected void queueWait(ArrayList<Process> queue) {
  79.         for (int i = 0; i < queue.size(); i++) {
  80.             queue.get(i).doWait();
  81.         }
  82.     }
  83.  
  84.     protected int getRemainingBurstTime() {
  85.         Process current;
  86.         int result = 0;
  87.         for (int i = 0; i < listProcess.size(); i++) {
  88.             current = listProcess.get(i);
  89.             result += (current.getCurrentBurstTime());
  90.         }
  91.         return result;
  92.     }
  93.  
  94.     public double countAvg() {
  95.         return sumWaitingTime() / listProcess.size();
  96.     }
  97.  
  98.     protected abstract void execute();
  99.  
  100.     @Override
  101.     public String toString() {
  102.         String result = "";
  103.         for (int i = 0; i < listProcess.size(); i++) {
  104.             result += (listProcess.get(i).getNama() + "=" + listProcess.get(i).getWaitingTime() + "\n");
  105.         }
  106.         return result;
  107.     }
  108.  
  109.     public int sumBurstTime() {
  110.         int total = 0;
  111.         for (int i = 0; i < listProcess.size(); i++) {
  112.             total += listProcess.get(i).getBurstTime();
  113.         }
  114.         return total;
  115.     }
  116.  
  117.     private int getLastEnd() {
  118.         Time time = null;
  119.         for (int i = 0; i < processTimeLine.size(); i++) {
  120.             if (time == null) {
  121.                 time = processTimeLine.get(i);
  122.             } else {
  123.                 if (time.end < processTimeLine.get(i).end) {
  124.                     time = processTimeLine.get(i);
  125.                 }
  126.             }
  127.         }
  128.         return time.end;
  129.     }
  130.  
  131.     private Time getTimeByStartValue(int start) {
  132.         Time time = null;
  133.         for (int i = 0; i < processTimeLine.size(); i++) {
  134.  
  135.             time = processTimeLine.get(i);
  136.  
  137.             if (time.start == start) {
  138.                 return time;
  139.             }
  140.         }
  141.         return null;
  142.     }
  143.  
  144.     private Time getTimeByEndValue(int end) {
  145.         Time time = null;
  146.         for (int i = 0; i < processTimeLine.size(); i++) {
  147.  
  148.             time = processTimeLine.get(i);
  149.  
  150.             if (time.end == end) {
  151.                 return time;
  152.             }
  153.         }
  154.         return null;
  155.     }
  156.  
  157.     public String getData() {
  158.         StringBuffer buf = new StringBuffer("Nama\tArrival\tBurstTime\tWaitingTime\n");
  159.         System.out.println("Nama\tArrival\tBurstTime\tWaitingTime");
  160.         for (int i = 0; i < listProcess.size(); i++) {
  161.             Process temp = listProcess.get(i);
  162.             buf.append(temp.getNama() + "\t" + temp.getArrivalTime() + "\t" + temp.getBurstTime() + "\t" + temp.getWaitingTime() + "\n");
  163.         }
  164.         return buf.toString();
  165.     }
  166.  
  167.     public String getTimelineReport() {
  168.         StringBuffer buf = new StringBuffer();
  169.         buf.append("Nama\tStart\tEnd\n");
  170.         for (int i = 0; i < processTimeLine.size(); i++) {
  171.             Schedulling.Time temp = processTimeLine.get(i);
  172.             buf.append(temp.processName + "\t" + temp.start + "\t" + temp.end+"\n");
  173.         }
  174.         return buf.toString();
  175.     }
  176.  
  177.     public String getStringChart() {
  178.         String buf = "", axis = "", process = "";
  179.         int n = 0;
  180.  
  181.         for (int i = 0; i <= getLastEnd(); i++) {
  182.             axis += String.format("%3d ", i);
  183.             buf += ("--- ");
  184.             Time time = getTimeByStartValue(i);
  185.             if (time != null) {
  186.                 process += String.format("%3s ",time.processName);
  187.             } else if (i == getLastEnd()) {
  188.                 process += String.format("%3s ",getTimeByEndValue(i).processName);
  189.             } else {
  190.                 process += String.format("%3s ","");
  191.             }
  192.  
  193.         }
  194.         return axis+"\n"+buf+"\n"+process+"\n";
  195.     }
  196.  
  197. }
Advertisement
Add Comment
Please, Sign In to add comment