test/java/lang/ProcessBuilder/Basic.java

Print this page

        

@@ -2015,11 +2015,10 @@
         try {
             if (Unix.is()
                 && new File("/bin/bash").exists()
                 && new File("/bin/sleep").exists()) {
                 final String[] cmd = { "/bin/bash", "-c", "(/bin/sleep 6666)" };
-                final String[] cmdkill = { "/bin/bash", "-c", "(/usr/bin/pkill -f \"sleep 6666\")" };
                 final ProcessBuilder pb = new ProcessBuilder(cmd);
                 final Process p = pb.start();
                 final InputStream stdout = p.getInputStream();
                 final InputStream stderr = p.getErrorStream();
                 final OutputStream stdin = p.getOutputStream();

@@ -2043,17 +2042,17 @@
                 check(p.waitFor() != 0);
                 check(p.exitValue() != 0);
                 stdout.close();
                 stderr.close();
                 stdin.close();
-                new ProcessBuilder(cmdkill).start();
                 //----------------------------------------------------------
                 // There remain unsolved issues with asynchronous close.
                 // Here's a highly non-portable experiment to demonstrate:
                 //----------------------------------------------------------
                 if (Boolean.getBoolean("wakeupJeff!")) {
                     System.out.println("wakeupJeff!");
+                    long startTime = System.nanoTime();
                     // Initialize signal handler for INTERRUPT_SIGNAL.
                     new FileInputStream("/bin/sleep").getChannel().close();
                     // Send INTERRUPT_SIGNAL to every thread in this java.
                     String[] wakeupJeff = {
                         "/bin/bash", "-c",

@@ -2062,12 +2061,21 @@
                         // INTERRUPT_SIGNAL == 62 on my machine du jour.
                         "/usr/bin/xargs kill -62"
                     };
                     new ProcessBuilder(wakeupJeff).start().waitFor();
                     // If wakeupJeff worked, reader probably got EBADF.
-                    reader.join();
+                    long timeout = 60L * 1000L;
+                    reader.join(timeout);
+                    long elapsedTimeMillis =
+                         (System.nanoTime() - startTime) / (1024L * 1024L); 
+                    check(elapsedTimeMillis < timeout);
+                    check(!reader.isAlive());
                 }
+                // Try to clean up the sleep process, but don't fret about it.
+                try {
+                    new ProcessBuilder("/usr/bin/pkill", "-f", "sleep 6666").start();
+                } catch (IOException noPkillCommandInstalled) { }
             }
         } catch (Throwable t) { unexpected(t); }
 
         //----------------------------------------------------------------
         // Attempt to start process with insufficient permissions fails.