2 * Copyright 2006-2012 The SimGrid team
5 * This program is free software; you can redistribute
6 * it and/or modify it under the terms of the license
7 * (GNU LGPL) which comes with this package.
10 package org.simgrid.msg;
12 import java.util.Arrays;
13 import java.util.Hashtable;
14 import java.util.Vector;
15 import java.lang.Runnable;
16 import java.util.concurrent.Semaphore;
19 * A process may be defined as a code, with some private data, executing
20 * in a location (host). All the process used by your simulation must be
21 * declared in the deployment file (XML format).
22 * To create your own process you must inherit your own process from this
23 * class and override the method "main()". For example if you want to use
24 * a process named Slave proceed as it :
26 * (1) import the class Process of the package simgrid.msg
27 * import simgrid.msg.Process;
29 * public class Slave extends simgrid.msg.Process {
31 * (2) Override the method function
34 * public void main(String[] args) {
35 * System.out.println("Hello MSG");
39 * The name of your process must be declared in the deployment file of your simulation.
40 * For the example, for the previous process Slave this file must contains a line :
41 * <process host="Maxims" function="Slave"/>, where Maxims is the host of the process
42 * Slave. All the process of your simulation are automatically launched and managed by Msg.
43 * A process use tasks to simulate communications or computations with another process.
44 * For more information see Task. For more information on host concept
49 public abstract class Process implements Runnable {
51 * This attribute represents a bind between a java process object and
52 * a native process. Even if this attribute is public you must never
53 * access to it. It is set automatically during the build of the object.
57 * Indicates if the process is started
61 * Even if this attribute is public you must never access to it.
62 * It is used to compute the id of an MSG process.
64 public static long nextProcessId = 0;
67 * Even if this attribute is public you must never access to it.
68 * It is compute automatically during the creation of the object.
69 * The native functions use this identifier to synchronize the process.
74 * Start time of the process
76 public double startTime = 0;
78 * Kill time of the process
80 public double killTime = -1;
82 public Hashtable<String,String> properties;
85 * The name of the process.
87 protected String name;
89 * The PID of the process
91 protected int pid = -1;
93 * The PPID of the process
95 protected int ppid = -1;
97 * The host of the process
99 protected Host host = null;
101 /** The arguments of the method function of the process. */
102 public Vector<String> args;
106 * Default constructor (used in ApplicationHandler to initialize it)
108 protected Process() {
109 this.id = nextProcessId++;
112 this.args = new Vector<String>();
113 this.properties = null;
118 * Constructs a new process from the name of a host and his name. The method
119 * function of the process doesn't have argument.
121 * @param hostname The name of the host of the process to create.
122 * @param name The name of the process.
124 * @exception HostNotFoundException if no host with this name exists.
128 public Process(String hostname, String name) throws HostNotFoundException {
129 this(Host.getByName(hostname), name, null);
132 * Constructs a new process from the name of a host and his name. The arguments
133 * of the method function of the process are specified by the parameter args.
135 * @param hostname The name of the host of the process to create.
136 * @param name The name of the process.
137 * @param args The arguments of the main function of the process.
139 * @exception HostNotFoundException if no host with this name exists.
141 * @throws NativeException
144 public Process(String hostname, String name, String args[]) throws HostNotFoundException, NativeException {
145 this(Host.getByName(hostname), name, args);
148 * Constructs a new process from a host and his name. The method function of the
149 * process doesn't have argument.
151 * @param host The host of the process to create.
152 * @param name The name of the process.
155 public Process(Host host, String name) {
156 this(host, name, null);
159 * Constructs a new process from a host and his name, the arguments of here method function are
160 * specified by the parameter args.
162 * @param host The host of the process to create.
163 * @param name The name of the process.
164 * @param args The arguments of main method of the process.
166 public Process(Host host, String name, String[]args) {
170 throw new NullPointerException("Process name cannot be NULL");
173 this.args = new Vector<String>();
175 this.args.addAll(Arrays.asList(args));
177 this.properties = new Hashtable<String,String>();
180 * Constructs a new process from a host and his name, the arguments of here method function are
181 * specified by the parameter args.
183 * @param host The host of the process to create.
184 * @param name The name of the process.
185 * @param args The arguments of main method of the process.
186 * @param startTime Start time of the process
187 * @param killTime Kill time of the process
190 public Process(Host host, String name, String[]args, double startTime, double killTime) {
194 throw new NullPointerException("Process name cannot be NULL");
197 this.args = new Vector<String>();
199 this.args.addAll(Arrays.asList(args));
201 this.properties = new Hashtable<String,String>();
203 this.startTime = startTime;
204 this.killTime = killTime;
207 * The natively implemented method to create an MSG process.
208 * @param host A valid (binded) host where create the process.
210 protected native void create(String hostName) throws HostNotFoundException;
212 * This method kills all running process of the simulation.
214 * @param resetPID Should we reset the PID numbers. A negative number means no reset
215 * and a positive number will be used to set the PID of the next newly
218 * @return The function returns the PID of the next created process.
221 public static native int killAll(int resetPID);
224 * This method kill a process.
227 public native void kill();
229 * Suspends the process by suspending the task on which it was
230 * waiting for the completion.
232 public native void suspend();
234 * Suspends the process by suspending the task on which it was
235 * waiting for the completion.
236 * DEPRECATED: use suspend instead.
239 public void pause() {
243 * Sets the "auto-restart" flag of the process.
245 public native void setAutoRestart(boolean autoRestart);
247 * Restarts the process from the beginning
249 public native void restart();
251 * Resumes a suspended process by resuming the task on which it was
252 * waiting for the completion.
254 public native void resume();
256 * Tests if a process is suspended.
258 * @return The method returns true if the process is suspended.
259 * Otherwise the method returns false.
261 public native boolean isSuspended();
263 * Returns the name of the process
265 public String msgName() {
269 * Returns the host of the process.
270 * @return The host instance of the process.
272 public Host getHost() {
276 * This static method gets a process from a PID.
278 * @param PID The process identifier of the process to get.
280 * @return The process with the specified PID.
282 * @exception NativeException on error in the native SimGrid code
284 public static native Process fromPID(int PID) throws NativeException;
286 * This method returns the PID of the process.
288 * @return The PID of the process.
291 public int getPID() {
295 * This method returns the PID of the parent of a process.
297 * @return The PID of the parent of the process.
300 public int getPPID() {
304 * Set the kill time of the process
305 * @param killTime the time when the process is killed
307 public native void setKillTime(double killTime);
310 * This static method returns the currently running process.
312 * @return The current process.
315 public static native Process currentProcess();
317 * Migrates a process to another host.
319 * @param process The process to migrate.
320 * @param host The host where to migrate the process.
323 public native void migrate(Host host);
325 * Makes the current process sleep until millis millisecondes have elapsed.
326 * You should note that unlike "waitFor" which takes seconds, this method takes milliseconds.
327 * FIXME: Not optimal, maybe we should have two native functions.
328 * @param millis the length of time to sleep in milliseconds.
330 public static void sleep(long millis) throws HostFailureException {
334 * Makes the current process sleep until millis milliseconds and nanos nanoseconds
336 * You should note that unlike "waitFor" which takes seconds, this method takes milliseconds and nanoseconds.
337 * Overloads Thread.sleep.
338 * @param millis the length of time to sleep in milliseconds.
339 * @param nanos additionnal nanoseconds to sleep.
341 public native static void sleep(long millis, int nanos) throws HostFailureException;
343 * Makes the current process sleep until time seconds have elapsed.
344 * @param seconds The time the current process must sleep.
346 public native void waitFor(double seconds) throws HostFailureException;
350 public void showArgs() {
351 Msg.info("[" + this.name + "/" + this.getHost().getName() + "] argc=" +
353 for (int i = 0; i < this.args.size(); i++)
354 Msg.info("[" + this.msgName() + "/" + this.getHost().getName() +
355 "] args[" + i + "]=" + (String) (this.args.get(i)));
358 * This method actually creates and run the process.
359 * It is a noop if the process is already launched.
360 * @throws HostNotFoundException
362 public final void start() throws HostNotFoundException {
365 create(host.getName());
370 * This method runs the process. Il calls the method function that you must overwrite.
374 String[] args = null; /* do not fill it before the signal or this.args will be empty */
375 //waitSignal(); /* wait for other people to fill the process in */
378 args = new String[this.args.size()];
379 if (this.args.size() > 0) {
380 this.args.toArray(args);
384 } catch(MsgException e) {
386 Msg.info("Unexpected behavior. Stopping now");
389 catch(ProcessKilledError pk) {
396 * The main function of the process (to implement).
399 * @throws MsgException
401 public abstract void main(String[]args) throws MsgException;
403 public native void exit();
405 * Class initializer, to initialize various JNI stuff
407 public static native void nativeInit();