// To be called exactly twice by the parent process static <T> T rendezvousParent(Process p, Callable<T> callable) throws Throwable { p.getInputStream().read(); T result = callable.call(); OutputStream os = p.getOutputStream(); os.write((byte) '\n'); os.flush(); return result; }
public static void main(String[] args) throws Exception { int counter = 0; while (true) { Thread outThread = null; Thread errThread = null; try { // org.pitest.mutationtest.instrument.MutationTestUnit#runTestInSeperateProcessForMutationRange // *** start slave ServerSocket commSocket = new ServerSocket(0); int commPort = commSocket.getLocalPort(); System.out.println("commPort = " + commPort); // org.pitest.mutationtest.execute.MutationTestProcess#start // - org.pitest.util.CommunicationThread#start FutureTask<Integer> commFuture = createFuture(commSocket); // - org.pitest.util.WrappingProcess#start // - org.pitest.util.JavaProcess#launch Process slaveProcess = startSlaveProcess(commPort); outThread = new Thread(new ReadFromInputStream(slaveProcess.getInputStream()), "stdout"); errThread = new Thread(new ReadFromInputStream(slaveProcess.getErrorStream()), "stderr"); outThread.start(); errThread.start(); // *** wait for slave to die // org.pitest.mutationtest.execute.MutationTestProcess#waitToDie // - org.pitest.util.CommunicationThread#waitToFinish System.out.println("waitToFinish"); Integer controlReturned = commFuture.get(); System.out.println("controlReturned = " + controlReturned); // NOTE: the following won't get called if commFuture.get() fails! // - org.pitest.util.JavaProcess#destroy outThread.interrupt(); // org.pitest.util.AbstractMonitor#requestStop errThread.interrupt(); // org.pitest.util.AbstractMonitor#requestStop slaveProcess.destroy(); } catch (Exception e) { e.printStackTrace(System.out); } // test: the threads should exit eventually outThread.join(); errThread.join(); counter++; System.out.println("try " + counter + ": stdout and stderr threads exited normally"); } }
static String outputOf(final Process p) { try { Future<String> outputFuture = futureOutputOf(p.getInputStream()); Future<String> errorFuture = futureOutputOf(p.getErrorStream()); final String output = outputFuture.get(); final String error = errorFuture.get(); // Check for successful process completion equal(error, ""); equal(p.waitFor(), 0); equal(p.exitValue(), 0); return output; } catch (Throwable t) { unexpected(t); throw new Error(t); } }
private void execute(String[] process_command) { this.running = true; // ProcessBuilder pb = new ProcessBuilder("C:\\Archivos de programa\\python2.5\\python", // "C:\\Archivos de programa\\python2.5\\test.py"); // ProcessBuilder pb = new ProcessBuilder(Executables.PYTHON_EXEC, "C:\\Archivos de // programa\\python2.5\\test.py", "--parameter2", "--parameter3", "--parameter4=\\\"C:\\Archivos // de programa\\python2.5\\test.py\\\""); // System.err.println("Executing "+process_command); StringBuffer str = new StringBuffer(); for (int i = 0; i < process_command.length; i++) { str.append(process_command[i]); // str.append("\n"); } System.err.println("Executing " + str.toString()); ProcessBuilder pb = new ProcessBuilder(process_command); pb.redirectErrorStream(true); Process p = null; boolean started = false; try { p = pb.start(); BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); started = true; this.current_process++; FileOutputStream fout = null; try { File temp = File.createTempFile("bianaParser", ".txt"); temp.deleteOnExit(); fout = new FileOutputStream(temp.getAbsoluteFile()); this.window.setCurrentOutput(this.current_process, temp.getAbsolutePath()); } catch (IOException err) { System.err.println("Error in creating parser out temp file"); } // this.window.setCurrentOutput(this.current_process,"process_"+this.current_process); this.window.change_process_status(current_process, "Running"); int c; while (true) { c = in.read(); if (c == -1) { break; } fout.write(c); } fout.close(); } catch (IOException exc) { exc.printStackTrace(); if (started) p.destroy(); return; } try { // System.err.println("Going to wait to finish process..."); System.err.println(p.waitFor()); // System.err.println("Process "+this.current_process+" really finished..."); System.err.println("Status: " + p.exitValue()); if (p.exitValue() == 0) { this.window.change_process_status(current_process, "Finished"); } else { this.window.change_process_status(current_process, "Error in parsing"); } } catch (InterruptedException e) { p.destroy(); e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } // System.err.println("Going to destroy..."); // p.destroy(); this.running = false; }