Esempio n. 1
0
  @Override
  public void run() {
    while (readyQueue.isEmpty()) {} // wait until LTS sends a new process to the ready queue

    while (true) {

      // If there isn't any process in ready or Auxiliary queue to dispatch into the CPU
      if (readyQueue.isEmpty() && Auxiliary.isEmpty()) {
        try {
          cpu.getGui().skip(); // skip painting the current progressbar in the gui
          Thread.sleep(1000 * factor);
          stopwatch++;

        } catch (InterruptedException ex) {
          Logger.getLogger(STS.class.getName()).log(Level.SEVERE, null, ex);
        }

      } else {

        System.out.println("");
        // if there's a process in the Auxiliary queue, take it! (because we give priority to this
        // queue over the ready queue)
        if (!Auxiliary.isEmpty()) {
          chosenProcess = Auxiliary.dequeue();
          queueIndex = 1;
          Auxiliary.displayDequeue(queueIndex, chosenProcess);

        } else {
          chosenProcess = readyQueue.dequeue();
          queueIndex = 0;
          readyQueue.displayDequeue(queueIndex, chosenProcess);
        }

        cpu.dispatch(chosenProcess);
        System.out.println(
            "the chosen process "
                + chosenProcess.getName()
                + " is dispatched to the CPU at "
                + stopwatch);

        nextCPUtime =
            chosenProcess.getNextCPUtime(
                stopwatch); // get the time, the proces will be in the CPU (always less than or
        // equal to time slice)
        //  System.out.println(nextCPUtime+"*******************");

        cpu.execute(nextCPUtime, chosenProcess);

        // if this chosen process will get blocked due to an IO request after dispatching
        if (chosenProcess.isIOhappens()) {
          // wait before blocking the running process (no more than a timeslice)
          try {
            Thread.sleep(
                (long)
                    (nextCPUtime * 1000 * factor)); // 1000 because of sleep() takes milliseconds.
            stopwatch += nextCPUtime;

          } catch (InterruptedException ex) {
            System.out.println("interrupted"); // continue sending new processes to the ready queue
          }

          // block the running process
          blockedProcess = cpu.block();
          System.out.println(
              "the running process "
                  + blockedProcess.getName()
                  + " is blocked at "
                  + stopwatch
                  + "s");

          // add the blocked process in to the IO queue
          IoWaiting.enqueue(blockedProcess, stopwatch);
          IoWaiting.dislayEnqueue(2, blockedProcess);

          System.out.println(
              "the blocked process "
                  + blockedProcess.getName()
                  + " is enqueed to the IO Queue at "
                  + stopwatch
                  + "s");

        } // if this chosen process will get preempted, without being blocked dueto an IO request
        // after dispatching
        else {

          // wait before preempting the running process
          try {
            Thread.sleep(
                (long)
                    (nextCPUtime * 1000 * factor)); // 1000 because of sleep() takes milliseconds.
            stopwatch += nextCPUtime;

          } catch (InterruptedException ex) {
            System.out.println("interrupted"); // continue sending new processes to the ready queue
          }

          // preempt the running process
          preemptedProcess = cpu.preempt();
          System.out.println(
              "the running process "
                  + preemptedProcess.getName()
                  + " is preempted at "
                  + stopwatch
                  + "s");

          // add the preempted process in to the ready queue if there is remainig service time to
          // do, else relese the process
          if (preemptedProcess.getNextCPUtime(stopwatch) > 0) {
            if (!readyQueue.isEmpty() || !Auxiliary.isEmpty()) {
              readyQueue.dislayEnqueue(0, chosenProcess);
            }
            readyQueue.enqueue(preemptedProcess);

            System.out.println(
                "the preempted process "
                    + preemptedProcess.getName()
                    + " is enqueed backck to the ready Queue at "
                    + stopwatch
                    + "s");
          } else {
            System.out.println(
                "the preempted process "
                    + preemptedProcess.getName()
                    + " is released at "
                    + stopwatch
                    + "s");
          }
        }
      }
    }
  }
 public synchronized void dislayEnqueue(int queueIndex, Process process) {
   // display in the queue in the GUI
   osgui.enqueue(
       queueIndex, process.getName(), process.getRemainingServiceTime(), process.getServiceTime());
 }
 public synchronized void displayDequeue(int queueIndex, Process process) {
   osgui.dequeue(queueIndex, process.getName());
   System.out.println(
       "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDdqueue!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! "
           + process.getName());
 }
 public synchronized void enqueue(Process process, int stopwatch) {
   process.setIoInTime(stopwatch + 0); // to coppy the value, not to send the reference??
   queue.add(process);
 }