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(); |
Andreas Gampe | aa3317e | 2017-09-19 08:10:00 -0700 | [diff] [blame] | 6 | |
| 7 | // Warm up the reaper so that there are no issues with scheduling because of static |
| 8 | // initialization. |
| 9 | { |
| 10 | ProcessBuilder pb = new ProcessBuilder("sleep", "0"); |
| 11 | Process proc = pb.start(); |
| 12 | proc.waitFor(); |
| 13 | Thread.sleep(500); // Consider checking for (and waiting on) the reaper state here. |
| 14 | } |
| 15 | |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 16 | for (int i = 1; i <= 2; i++) { |
| 17 | System.out.println("\nspawning child #" + i); |
| 18 | child(); |
| 19 | Thread.sleep(2000); |
| 20 | checkManager(); |
| 21 | } |
| 22 | System.out.println("\ndone!"); |
| 23 | } |
| 24 | |
| 25 | static private void child() throws Exception { |
| 26 | System.out.println("spawning child"); |
TDYa127 | 6ce558b | 2012-04-11 11:17:55 -0700 | [diff] [blame] | 27 | ProcessBuilder pb = new ProcessBuilder("sleep", "5"); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 28 | Process proc = pb.start(); |
Jeff Hao | ef7b9b8 | 2017-08-21 16:43:01 -0700 | [diff] [blame] | 29 | Thread.sleep(250); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 30 | checkManager(); |
| 31 | proc.waitFor(); |
| 32 | System.out.println("child died"); |
| 33 | } |
| 34 | |
| 35 | static private void checkManager() { |
| 36 | Map<Thread, StackTraceElement[]> traces = Thread.getAllStackTraces(); |
| 37 | boolean found = false; |
| 38 | |
| 39 | for (Map.Entry<Thread, StackTraceElement[]> entry : |
| 40 | traces.entrySet()) { |
| 41 | Thread t = entry.getKey(); |
| 42 | String name = t.getName(); |
Narayan Kamath | cad4205 | 2015-11-16 14:51:16 +0000 | [diff] [blame] | 43 | if (name.indexOf("process reaper") >= 0) { |
Andreas Gampe | 4486442 | 2017-09-18 13:33:40 -0700 | [diff] [blame] | 44 | Thread.State state = t.getState(); |
| 45 | System.out.println("process manager: " + state); |
| 46 | if (state != Thread.State.RUNNABLE && state != Thread.State.TIMED_WAITING) { |
| 47 | for (StackTraceElement e : entry.getValue()) { |
| 48 | System.out.println(" " + e); |
| 49 | } |
| 50 | } |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 51 | found = true; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | if (! found) { |
| 56 | System.out.println("process manager: nonexistent"); |
| 57 | } |
| 58 | } |
| 59 | } |