Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
peersimgrid release 1.0
[simgrid.git] / contrib / psg / src / peersim / rangesim / ProcessManager.java
diff --git a/contrib/psg/src/peersim/rangesim/ProcessManager.java b/contrib/psg/src/peersim/rangesim/ProcessManager.java
new file mode 100644 (file)
index 0000000..1ccfc79
--- /dev/null
@@ -0,0 +1,53 @@
+package peersim.rangesim;
+
+import java.util.*;
+
+/**
+ * This thread is used to kill forked processes in the case of an abnormal
+ * termination of the Java virtual machine (for example, due to a signal).
+ */
+public class ProcessManager extends Thread
+{
+
+/** The threads that must be killed */
+private List<ProcessHandler> threads;
+
+public ProcessManager()
+{
+       threads = Collections.synchronizedList(new ArrayList<ProcessHandler>());
+}
+
+public void addThread(ProcessHandler p)
+{
+       threads.add(p);
+}
+
+/**
+ * Assumes that the process manager
+ */
+public void joinAll()
+{
+       int i=0;
+       while (i < threads.size()) {
+               try {
+                       threads.get(i).join();
+                       i++;
+               } catch (InterruptedException e) {
+               }
+       }
+}
+
+
+/**
+ * Kill the child process.
+ */
+public void run()
+{
+       System.err.println("Terminating simulation.");
+       for (int i=0; i < threads.size(); i++) {
+               if (threads.get(i) != null)
+                       threads.get(i).doStop();
+       }
+}
+
+}
\ No newline at end of file