4 * Copyright 2006,2007 Martin Quinson, Malek Cherier
7 * This program is free software; you can redistribute
8 * it and/or modify it under the terms of the license
9 *(GNU LGPL) which comes with this package.
12 package org.simgrid.msg;
14 import java.util.Arrays;
15 import java.util.Hashtable;
16 import java.util.Vector;
17 import java.lang.Runnable;
18 import java.util.concurrent.Semaphore;
21 * A process may be defined as a code, with some private data, executing
22 * in a location (host). All the process used by your simulation must be
23 * declared in the deployment file (XML format).
24 * To create your own process you must inherit your own process from this
25 * class and override the method "main()". For example if you want to use
26 * a process named Slave proceed as it :
28 * (1) import the class Process of the package simgrid.msg
29 * import simgrid.msg.Process;
31 * public class Slave extends simgrid.msg.Process {
33 * (2) Override the method function
36 * public void main(String[] args) {
37 * System.out.println("Hello MSG");
41 * The name of your process must be declared in the deployment file of your simulation.
42 * For the example, for the previous process Slave this file must contains a line :
43 * <process host="Maxims" function="Slave"/>, where Maxims is the host of the process
44 * Slave. All the process of your simulation are automatically launched and managed by Msg.
45 * A process use tasks to simulate communications or computations with another process.
46 * For more information see Task. For more information on host concept
51 public abstract class Process implements Runnable {
53 * This attribute represents a bind between a java process object and
54 * a native process. Even if this attribute is public you must never
55 * access to it. It is set automatically during the build of the object.
59 * Indicates if the process is started
63 * Even if this attribute is public you must never access to it.
64 * It is used to compute the id of an MSG process.
66 public static long nextProcessId = 0;
69 * Even if this attribute is public you must never access to it.
70 * It is compute automatically during the creation of the object.
71 * The native functions use this identifier to synchronize the process.
76 * Start time of the process
78 public double startTime = 0;
80 * Kill time of the process
82 public double killTime = -1;
84 public Hashtable<String,String> properties;
87 * The name of the process.
89 protected String name;
91 * The PID of the process
93 protected int pid = -1;
95 * The PPID of the process
97 protected int ppid = -1;
99 * The host of the process
101 protected Host host = null;
103 /** The arguments of the method function of the process. */
104 public Vector<String> args;
108 * Default constructor (used in ApplicationHandler to initialize it)
110 protected Process() {
111 this.id = nextProcessId++;
114 this.args = new Vector<String>();
115 this.properties = null;
120 * Constructs a new process from the name of a host and his name. The method
121 * function of the process doesn't have argument.
123 * @param hostname The name of the host of the process to create.
124 * @param name The name of the process.
126 * @exception HostNotFoundException if no host with this name exists.
130 public Process(String hostname, String name) throws HostNotFoundException {
131 this(Host.getByName(hostname), name, null);
134 * Constructs a new process from the name of a host and his name. The arguments
135 * of the method function of the process are specified by the parameter args.
137 * @param hostname The name of the host of the process to create.
138 * @param name The name of the process.
139 * @param args The arguments of the main function of the process.
141 * @exception HostNotFoundException if no host with this name exists.
143 * @throws NativeException
146 public Process(String hostname, String name, String args[]) throws HostNotFoundException, NativeException {
147 this(Host.getByName(hostname), name, args);
150 * Constructs a new process from a host and his name. The method function of the
151 * process doesn't have argument.
153 * @param host The host of the process to create.
154 * @param name The name of the process.
157 public Process(Host host, String name) {
158 this(host, name, null);
161 * Constructs a new process from a host and his name, the arguments of here method function are
162 * specified by the parameter args.
164 * @param host The host of the process to create.
165 * @param name The name of the process.
166 * @param args The arguments of main method of the process.
168 public Process(Host host, String name, String[]args) {
172 throw new NullPointerException("Process name cannot be NULL");
175 this.args = new Vector<String>();
177 this.args.addAll(Arrays.asList(args));
179 this.properties = new Hashtable<String,String>();
182 * Constructs a new process from a host and his name, the arguments of here method function are
183 * specified by the parameter args.
185 * @param host The host of the process to create.
186 * @param name The name of the process.
187 * @param args The arguments of main method of the process.
188 * @param startTime Start time of the process
189 * @param killTime Kill time of the process
192 public Process(Host host, String name, String[]args, double startTime, double killTime) {
196 throw new NullPointerException("Process name cannot be NULL");
199 this.args = new Vector<String>();
201 this.args.addAll(Arrays.asList(args));
203 this.properties = new Hashtable<String,String>();
205 this.startTime = startTime;
206 this.killTime = killTime;
209 * The natively implemented method to create an MSG process.
210 * @param host A valid (binded) host where create the process.
212 protected native void create(String hostName) throws HostNotFoundException;
214 * This method kills all running process of the simulation.
216 * @param resetPID Should we reset the PID numbers. A negative number means no reset
217 * and a positive number will be used to set the PID of the next newly
220 * @return The function returns the PID of the next created process.
223 public static native int killAll(int resetPID);
226 * This method kill a process.
229 public native void kill();
231 * Suspends the process by suspending the task on which it was
232 * waiting for the completion.
234 public native void suspend();
236 * Suspends the process by suspending the task on which it was
237 * waiting for the completion.
238 * DEPRECATED: use suspend instead.
241 public void pause() {
245 * Resumes a suspended process by resuming the task on which it was
246 * waiting for the completion.
247 * DEPRECATED: use resume instead.
250 public void restart() {
254 * Resumes a suspended process by resuming the task on which it was
255 * waiting for the completion.
257 public native void resume();
259 * Tests if a process is suspended.
261 * @return The method returns true if the process is suspended.
262 * Otherwise the method returns false.
264 public native boolean isSuspended();
266 * Returns the name of the process
268 public String msgName() {
272 * Returns the host of the process.
273 * @return The host instance of the process.
275 public Host getHost() {
279 * This static method gets a process from a PID.
281 * @param PID The process identifier of the process to get.
283 * @return The process with the specified PID.
285 * @exception NativeException on error in the native SimGrid code
287 public static native Process fromPID(int PID) throws NativeException;
289 * This method returns the PID of the process.
291 * @return The PID of the process.
294 public int getPID() {
298 * This method returns the PID of the parent of a process.
300 * @return The PID of the parent of the process.
303 public int getPPID() {
307 * Set the kill time of the process
308 * @param killTime the time when the process is killed
310 public native void setKillTime(double killTime);
313 * This static method returns the currently running process.
315 * @return The current process.
318 public static native Process currentProcess();
320 * Migrates a process to another host.
322 * @param process The process to migrate.
323 * @param host The host where to migrate the process.
326 public native void migrate(Host host);
328 * Makes the current process sleep until millis millisecondes have elapsed.
329 * You should note that unlike "waitFor" which takes seconds, this method takes milliseconds.
330 * FIXME: Not optimal, maybe we should have two native functions.
331 * @param millis the length of time to sleep in milliseconds.
333 public static void sleep(long millis) throws HostFailureException {
337 * Makes the current process sleep until millis milliseconds and nanos nanoseconds
339 * You should note that unlike "waitFor" which takes seconds, this method takes milliseconds and nanoseconds.
340 * Overloads Thread.sleep.
341 * @param millis the length of time to sleep in milliseconds.
342 * @param nanos additionnal nanoseconds to sleep.
344 public native static void sleep(long millis, int nanos) throws HostFailureException;
346 * Makes the current process sleep until time seconds have elapsed.
347 * @param seconds The time the current process must sleep.
349 public native void waitFor(double seconds) throws HostFailureException;
353 public void showArgs() {
354 Msg.info("[" + this.name + "/" + this.getHost().getName() + "] argc=" +
356 for (int i = 0; i < this.args.size(); i++)
357 Msg.info("[" + this.msgName() + "/" + this.getHost().getName() +
358 "] args[" + i + "]=" + (String) (this.args.get(i)));
361 * This method actually creates and run the process.
362 * It is a noop if the process is already launched.
363 * @throws HostNotFoundException
365 public final void start() throws HostNotFoundException {
368 create(host.getName());
373 * This method runs the process. Il calls the method function that you must overwrite.
377 String[] args = null; /* do not fill it before the signal or this.args will be empty */
378 //waitSignal(); /* wait for other people to fill the process in */
381 args = new String[this.args.size()];
382 if (this.args.size() > 0) {
383 this.args.toArray(args);
387 } catch(MsgException e) {
389 Msg.info("Unexpected behavior. Stopping now");
392 catch(ProcessKilledError pk) {
399 * The main function of the process (to implement).
402 * @throws MsgException
404 public abstract void main(String[]args) throws MsgException;
406 public native void exit();
408 * Class initializer, to initialize various JNI stuff
410 public static native void nativeInit();