jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 1 | import java.util.Map; |
| 2 | |
| 3 | public class Main { |
| 4 | static public void main(String[] args) throws Exception { |
| 5 | checkManager(); |
| 6 | for (int i = 1; i <= 2; i++) { |
| 7 | System.out.println("\nspawning child #" + i); |
| 8 | child(); |
| 9 | Thread.sleep(2000); |
| 10 | checkManager(); |
| 11 | } |
| 12 | System.out.println("\ndone!"); |
| 13 | } |
| 14 | |
| 15 | static private void child() throws Exception { |
| 16 | System.out.println("spawning child"); |
TDYa127 | 6ce558b | 2012-04-11 11:17:55 -0700 | [diff] [blame] | 17 | ProcessBuilder pb = new ProcessBuilder("sleep", "5"); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 18 | Process proc = pb.start(); |
Jeff Hao | ef7b9b8 | 2017-08-21 16:43:01 -0700 | [diff] [blame^] | 19 | Thread.sleep(250); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 20 | checkManager(); |
| 21 | proc.waitFor(); |
| 22 | System.out.println("child died"); |
| 23 | } |
| 24 | |
| 25 | static private void checkManager() { |
| 26 | Map<Thread, StackTraceElement[]> traces = Thread.getAllStackTraces(); |
| 27 | boolean found = false; |
| 28 | |
| 29 | for (Map.Entry<Thread, StackTraceElement[]> entry : |
| 30 | traces.entrySet()) { |
| 31 | Thread t = entry.getKey(); |
| 32 | String name = t.getName(); |
Narayan Kamath | cad4205 | 2015-11-16 14:51:16 +0000 | [diff] [blame] | 33 | if (name.indexOf("process reaper") >= 0) { |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 34 | System.out.println("process manager: " + t.getState()); |
| 35 | found = true; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | if (! found) { |
| 40 | System.out.println("process manager: nonexistent"); |
| 41 | } |
| 42 | } |
| 43 | } |