Ejemplo n.º 1
0
  public void startTime() throws FileNotFoundException {

    PrintWriter outFile = new PrintWriter("output.data");

    Pcb cpuProc;

    while (!arrivalQueue.isEmpty() || !readyQueue.isEmpty()) {

      if (!arrivalQueue.isEmpty()) // makes pointer to head object of arrival queue
      test = arrivalQueue.peek();

      if (test.getArrivalTime() == sysTime
          && !arrivalQueue.isEmpty()) // adds to ready queue from arrival queue
      {
        readyQueue.add(arrivalQueue.remove());
        //				System.out.println("Job " + test.getId() + " entered the Ready Queue.");
      }

      if (cpu.currentProc == null && !readyQueue.isEmpty()) // puts the first job in cpu
      {
        cpu.currentProc = readyQueue.remove();
        //				System.out.println("This job is in the cpu and has an id of: " +
        // cpu.currentProc.getId());
        outFile.print(sysTime + " "); // entered cpu
      }

      if (!readyQueue.isEmpty()) test = readyQueue.peek();

      if (cpu.currentProc != null
          && !readyQueue.isEmpty()
          && cpu.currentProc.getPriority() < test.getPriority()) {
        Pcb cpuTemp = cpu.currentProc;

        outFile.println(sysTime + " " + cpu.currentProc.getId());

        cpu.currentProc = readyQueue.remove();
        readyQueue.add(cpuTemp);
        outFile.print(sysTime + " "); // entered cpu

        //				System.out.println("Job " + cpu.currentProc.getId() + " entered the CPU. With priority
        // " + cpu.currentProc.getPriority());

      }

      if (cpu.currentProc != null && cpu.currentProc.getCpuBurstTime() == 0) {
        outFile.println(sysTime + " " + cpu.currentProc.getId()); // lefted cpu

        cpu.currentProc = readyQueue.remove();

        outFile.print(sysTime + " "); // entered cpu

        //				System.out.println("the process in the cpu got it's burst time to zero.");
        //			 System.out.println("Job " + cpu.currentProc.getId() + " entered the CPU. With priority
        // " + cpu.currentProc.getPriority());
      }

      if (cpu.currentProc != null) {
        cpu.currentProc.setCpuBurstTime(cpu.currentProc.getCpuBurstTime() - 1);
        //		System.out.println("Tried to subtact one from cpu burst time");
      }

      sysTime++;
    }

    while (cpu.currentProc.getCpuBurstTime() != 0) {
      cpu.currentProc.setCpuBurstTime(cpu.currentProc.getCpuBurstTime() - 1);
      sysTime++;
    }

    System.out.println("Jobs are finished");

    outFile.println(sysTime + " " + cpu.currentProc.getId()); // lefted cpu	

    outFile.close();
    // after both Queues are empty finish the process in the cpu
  }