コード例 #1
0
  private void releaseProcess(Process process) {
    process.getParent().removeChild(process);
    for (Process child : process.getChildren()) {
      mInit.addChild(child);
      child.setParent(mInit);
    }

    Pid pid = process.getPid();
    mPidGenerator.release(pid);
    mLogger.info("released the process of pid %s", pid);
  }
コード例 #2
0
ファイル: Client.java プロジェクト: joaopmgd/BullyAlgorithm
 private void Response(ArrayList<Socket> socketList) {
   try {
     for (Socket s : socketList) {
       ObjectInputStream input = new ObjectInputStream(s.getInputStream());
       Message message = (Message) input.readObject();
       if (message.isOk()) {
         System.out.println(
             "Process: " + process.getPid() + " got an OK from Process: " + message.getPid());
       }
     }
   } catch (IOException e) {
     e.printStackTrace();
   } catch (ClassNotFoundException e) {
     e.printStackTrace();
   }
 }
コード例 #3
0
  public static void main(String[] args) {
    final String PROCESSES_FILE = "processes.txt";
    final int TIME_QUANTUM = 2;

    // Read processes in from the file and add them to the ready queue.
    Queue<Process> readyQueue = new LinkedList<Process>();

    for (int i = 0; i < 20; i++) {
      readyQueue.add(new Process(i, i + 20, i + 13, 0.3));
    }

    int ticker = 0;
    CPU cpu = new CPU();

    Process process = readyQueue.poll();

    while (readyQueue.size() > 0) {

      if (process.getState() == ProcessState.TERMINATED) {
        readyQueue.remove(process);
        process = readyQueue.poll();
      } else if (process.getBurstTime() % TIME_QUANTUM == 0) {
        // Put the current process in ready state and add it back to
        // the queue
        process.setState(ProcessState.READY);
        readyQueue.add(process);
        process = readyQueue.poll();
      }

      process.setState(ProcessState.RUNNING);

      try {
        cpu.setPC(process.generateInstruction());
        cpu.setRegisters(process.generateRegisters());
        process.incrementBurstTime();
        System.out.print("CPU at " + ticker + ": " + cpu + " on pid: " + process.getPid() + "\n");
      } catch (Exception e) {
        System.out.println(e.getMessage());
      }

      ticker++;
    }
  }
コード例 #4
0
ファイル: Client.java プロジェクト: joaopmgd/BullyAlgorithm
 public void sendMessage() {
   try {
     ArrayList<Socket> socketList = new ArrayList<>();
     Message message = new Message(process.getPid());
     for (ConnectedClient connectedClient : this.connectedClients) {
       if (connectedClient.getPid() > process.getPid()) {
         System.out.println(
             "Process: "
                 + process.getPid()
                 + " is sending ELECTION message to Process: "
                 + connectedClient.getPid());
         ObjectOutputStream out =
             new ObjectOutputStream(connectedClient.getSocket().getOutputStream());
         out.writeObject(message);
         socketList.add(connectedClient.getSocket());
       }
     }
     if (socketList.isEmpty()) {
       process.setCoordinator(process.getPid());
       System.out.println("Process: " + process.getPid() + " is the new COORDINATOR");
       message = new Message("Coordinator", process.getPid());
       for (ConnectedClient connectedClient : this.connectedClients) {
         System.out.println(
             "Process: "
                 + process.getPid()
                 + " is sending COORDINATOR message to Process: "
                 + connectedClient.getPid());
         ObjectOutputStream out =
             new ObjectOutputStream(connectedClient.getSocket().getOutputStream());
         out.writeObject(message);
       }
     } else {
       Response(socketList);
     }
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
コード例 #5
0
  private static boolean doTest(String testId, String arg) throws Exception {
    List<String> args = new ArrayList<>();
    args.add("-XX:+UsePerfData");
    args.addAll(Utils.getVmOptions());
    args.add("-cp");
    args.add(TEST_CLASSPATH);

    if (arg != null) {
      args.add(arg);
    }
    args.add("TestApplication");
    ProcessBuilder server =
        ProcessTools.createJavaProcessBuilder(args.toArray(new String[args.size()]));

    Process serverPrc = null, clientPrc = null;
    try {
      final AtomicReference<String> port = new AtomicReference<>();

      serverPrc =
          ProcessTools.startProcess(
              "TestApplication(" + testId + ")",
              server,
              (String line) -> {
                if (line.startsWith("port:")) {
                  port.set(line.split("\\:")[1]);
                } else if (line.startsWith("waiting")) {
                  return true;
                }
                return false;
              });

      System.out.println("Attaching test manager:");
      System.out.println("=========================");
      System.out.println("  PID           : " + serverPrc.getPid());
      System.out.println("  shutdown port : " + port.get());

      ProcessBuilder client =
          ProcessTools.createJavaProcessBuilder(
              "-cp",
              TEST_CLASSPATH,
              "--add-exports",
              "java.management/sun.management=ALL-UNNAMED",
              "TestManager",
              String.valueOf(serverPrc.getPid()),
              port.get(),
              "true");

      clientPrc =
          ProcessTools.startProcess(
              "TestManager",
              client,
              (String line) -> line.startsWith("Starting TestManager for PID"));

      int clientExitCode = clientPrc.waitFor();
      int serverExitCode = serverPrc.waitFor();
      return clientExitCode == 0 && serverExitCode == 0;
    } finally {
      if (clientPrc != null) {
        System.out.println("Stopping process " + clientPrc);
        clientPrc.destroy();
        clientPrc.waitFor();
      }
      if (serverPrc != null) {
        System.out.println("Stopping process " + serverPrc);
        serverPrc.destroy();
        serverPrc.waitFor();
      }
    }
  }