Path: blob/master/test/jdk/java/lang/ProcessBuilder/Zombies.java
41149 views
/*1* Copyright (c) 2006, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @run main/othervm Zombies26* @bug 6474073 618015127* @summary Make sure zombies don't get created on Unix28* @author Martin Buchholz29*/3031import java.io.*;3233public class Zombies {3435static final String os = System.getProperty("os.name");3637static final String TrueCommand = os.contains("OS X")?38"/usr/bin/true" : "/bin/true";3940public static void main(String[] args) throws Throwable {41if (! new File("/usr/bin/perl").canExecute() ||42! new File("/bin/ps").canExecute())43return;44System.out.println("Looks like a Unix system.");45long mypid = ProcessHandle.current().pid();46System.out.printf("mypid: %d%n", mypid);4748final Runtime rt = Runtime.getRuntime();4950try {51rt.exec("no-such-file");52throw new Error("expected IOException not thrown");53} catch (IOException expected) {/* OK */}5455try {56rt.exec(".");57throw new Error("expected IOException not thrown");58} catch (IOException expected) {/* OK */}5960try {61rt.exec(TrueCommand, null, new File("no-such-dir"));62throw new Error("expected IOException not thrown");63} catch (IOException expected) {/* OK */}6465Process p = rt.exec(TrueCommand);66ProcessHandle pp = p.toHandle().parent().orElse(null);67System.out.printf("%s pid: %d, parent: %s%n", TrueCommand, p.pid(), pp);68p.waitFor();6970// Count all the zombies that are children of this Java process71final String[] zombieCounter = {72"/usr/bin/perl", "-e",73"$a=`/bin/ps -eo ppid,pid,s,command`;" +74"print @b=$a=~/^ *@{[getppid]} +[0-9]+ +Z.*$/mog;" +75"exit @b"76};7778ProcessBuilder pb = new ProcessBuilder(zombieCounter);79pb.inheritIO();80int zombies = pb.start().waitFor();81if (zombies != 0) {82throw new Error(zombies + " zombies!");83}84}85}868788