blob: 1005b7710746d849cdb31935b9288e73a6d70df8 [file] [log] [blame]
jeffhao5d1ac922011-09-29 17:41:15 -07001import java.util.Map;
2
3public class Main {
4 static public void main(String[] args) throws Exception {
5 checkManager();
Andreas Gampeaa3317e2017-09-19 08:10:00 -07006
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
jeffhao5d1ac922011-09-29 17:41:15 -070016 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");
TDYa1276ce558b2012-04-11 11:17:55 -070027 ProcessBuilder pb = new ProcessBuilder("sleep", "5");
jeffhao5d1ac922011-09-29 17:41:15 -070028 Process proc = pb.start();
Jeff Haoef7b9b82017-08-21 16:43:01 -070029 Thread.sleep(250);
jeffhao5d1ac922011-09-29 17:41:15 -070030 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 Kamathcad42052015-11-16 14:51:16 +000043 if (name.indexOf("process reaper") >= 0) {
Andreas Gampe44864422017-09-18 13:33:40 -070044 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 }
jeffhao5d1ac922011-09-29 17:41:15 -070051 found = true;
52 }
53 }
54
55 if (! found) {
56 System.out.println("process manager: nonexistent");
57 }
58 }
59}