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.
14 import java.lang.Thread;
18 * A process may be defined as a code, with some private data, executing
19 * in a location (host). All the process used by your simulation must be
20 * declared in the deployment file (XML format).
21 * To create your own process you must inherit your own process from this
22 * class and override the method "main()". For example if you want to use
23 * a process named Slave proceed as it :
25 * (1) import the class Process of the package simgrid.msg
26 * import simgrid.msg.Process;
28 * public class Slave extends simgrid.msg.Process {
30 * (2) Override the method function
33 * public void main(String[] args) {
34 * System.out.println("Hello MSG");
38 * The name of your process must be declared in the deployment file of your simulation.
39 * For the example, for the previous process Slave this file must contains a line :
40 * <process host="Maxims" function="Slave"/>, where Maxims is the host of the process
41 * Slave. All the process of your simulation are automatically launched and managed by Msg.
42 * A process use tasks to simulate communications or computations with another process.
43 * For more information see Task. For more information on host concept
48 public abstract class Process extends Thread {
50 * This attribute represents a bind between a java process object and
51 * a native process. Even if this attribute is public you must never
52 * access to it. It is set automatically during the build of the object.
57 * Even if this attribute is public you must never access to it.
58 * It is used to compute the id of an MSG process.
60 public static long nextProcessId = 0;
63 * Even if this attribute is public you must never access to it.
64 * It is compute automatically during the creation of the object.
65 * The native functions use this identifier to synchronize the process.
69 public Hashtable<String,String> properties;
72 * The name of the process.
74 protected String name;
75 public String msgName() {
78 /** The arguments of the method function of the process. */
79 public Vector<String> args;
81 /* process synchronization tools */
82 protected Sem schedBegin, schedEnd;
85 * Default constructor (used in ApplicationHandler to initialize it)
92 this.args = new Vector<String>();
93 this.properties = null;
94 schedBegin = new Sem(0);
95 schedEnd = new Sem(0);
100 * Constructs a new process from the name of a host and his name. The method
101 * function of the process doesn't have argument.
103 * @param hostname The name of the host of the process to create.
104 * @param name The name of the process.
106 * @exception HostNotFoundException if no host with this name exists.
107 * NullPointerException if the provided name is null
108 * JniException on JNI madness
112 public Process(String hostname, String name)
113 throws NullPointerException, HostNotFoundException, JniException,
115 this(Host.getByName(hostname), name, null);
118 * Constructs a new process from the name of a host and his name. The arguments
119 * of the method function of the process are specified by the parameter args.
121 * @param hostname The name of the host of the process to create.
122 * @param name The name of the process.
123 * @param args The arguments of the main function of the process.
125 * @exception HostNotFoundException if no host with this name exists.
126 * NullPointerException if the provided name is null
127 * JniException on JNI madness
130 public Process(String hostname, String name, String args[])
131 throws NullPointerException, HostNotFoundException, JniException,
133 this(Host.getByName(hostname), name, args);
136 * Constructs a new process from a host and his name. The method function of the
137 * process doesn't have argument.
139 * @param host The host of the process to create.
140 * @param name The name of the process.
142 * @exception NullPointerException if the provided name is null
143 * JniException on JNI madness
146 public Process(Host host, String name) throws NullPointerException,
148 this(host, name, null);
151 * Constructs a new process from a host and his name, the arguments of here method function are
152 * specified by the parameter args.
154 * @param host The host of the process to create.
155 * @param name The name of the process.
156 * @param args The arguments of main method of the process.
158 * @exception NullPointerException if the provided name is null
159 * JniException on JNI madness
162 public Process(Host host, String name,
163 String[]args) throws NullPointerException, JniException {
164 /* This is the constructor called by all others */
167 throw new NullPointerException("Process name cannot be NULL");
169 this.properties = null;
171 this.args = new Vector<String>();
174 this.args.addAll(Arrays.asList(args));
177 this.id = nextProcessId++;
179 schedBegin = new Sem(0);
180 schedEnd = new Sem(0);
182 MsgNative.processCreate(this, host);
187 * This method kills all running process of the simulation.
189 * @param resetPID Should we reset the PID numbers. A negative number means no reset
190 * and a positive number will be used to set the PID of the next newly
193 * @return The function returns the PID of the next created process.
196 public static int killAll(int resetPID) {
197 return MsgNative.processKillAll(resetPID);
201 * This method adds an argument in the list of the arguments of the main function
204 * @param arg The argument to add.
206 protected void addArg(String arg) {
211 * This method suspends the process by suspending the task on which it was
212 * waiting for the completion.
214 * @exception JniException on JNI madness
215 * NativeException on error in the native SimGrid code
217 public void pause() throws JniException, NativeException {
218 MsgNative.processSuspend(this);
221 * This method resumes a suspended process by resuming the task on which it was
222 * waiting for the completion.
224 * @exception JniException on JNI madness
225 * NativeException on error in the native SimGrid code
228 public void restart() throws JniException, NativeException {
229 MsgNative.processResume(this);
232 * This method tests if a process is suspended.
234 * @return The method returns true if the process is suspended.
235 * Otherwise the method returns false.
237 * @exception JniException on JNI madness
239 public boolean isSuspended() throws JniException {
240 return MsgNative.processIsSuspended(this);
243 * This method returns the host of a process.
245 * @return The host instance of the process.
247 * @exception JniException on JNI madness
248 * NativeException on error in the native SimGrid code
251 public Host getHost() throws JniException, NativeException {
252 return MsgNative.processGetHost(this);
255 * This static method get a process from a PID.
257 * @param PID The process identifier of the process to get.
259 * @return The process with the specified PID.
261 * @exception NativeException on error in the native SimGrid code
263 public static Process fromPID(int PID) throws NativeException {
264 return MsgNative.processFromPID(PID);
267 * This method returns the PID of the process.
269 * @return The PID of the process.
271 * @exception JniException on JNI madness
272 * NativeException on error in the native SimGrid code
274 public int getPID() throws JniException, NativeException {
275 return MsgNative.processGetPID(this);
278 * This method returns the PID of the parent of a process.
280 * @return The PID of the parent of the process.
282 * @exception NativeException on error in the native SimGrid code
284 public int getPPID() throws NativeException {
285 return MsgNative.processGetPPID(this);
288 * This static method returns the currently running process.
290 * @return The current process.
292 * @exception NativeException on error in the native SimGrid code
296 public static Process currentProcess() throws NativeException {
297 return MsgNative.processSelf();
300 * This static method returns the PID of the currently running process.
302 * @return The PID of the current process.
304 public static int currentProcessPID() {
305 return MsgNative.processSelfPID();
309 * This static method returns the PID of the parent of the currently running process.
311 * @return The PID of the parent of current process.
313 public static int currentProcessPPID() {
314 return MsgNative.processSelfPPID();
318 * This function migrates a process to another host.
320 * @param host The host where to migrate the process.
322 * @exception JniException on JNI madness
323 * NativeException on error in the native SimGrid code
325 public void migrate(Host host) throws JniException, NativeException {
326 MsgNative.processChangeHost(this, host);
329 * This method makes the current process sleep until time seconds have elapsed.
331 * @param seconds The time the current process must sleep.
333 * @exception NativeException on error in the native SimGrid code
335 public static void waitFor(double seconds) throws NativeException {
336 MsgNative.processWaitFor(seconds);
338 public void showArgs() {
340 Msg.info("[" + this.name + "/" + this.getHost().getName() + "] argc=" +
342 for (int i = 0; i < this.args.size(); i++)
343 Msg.info("[" + this.msgName() + "/" + this.getHost().getName() +
344 "] args[" + i + "]=" + (String) (this.args.get(i)));
345 } catch(MsgException e) {
346 Msg.info("Damn JNI stuff");
352 * This method runs the process. Il calls the method function that you must overwrite.
356 String[]args = null; /* do not fill it before the signal or this.args will be empty */
358 //waitSignal(); /* wait for other people to fill the process in */
362 schedBegin.acquire();
363 } catch(InterruptedException e) {
367 args = new String[this.args.size()];
368 if (this.args.size() > 0) {
369 this.args.toArray(args);
373 MsgNative.processExit(this);
375 } catch(MsgException e) {
377 Msg.info("Unexpected behavior. Stopping now");
383 * The main function of the process (to implement).
385 public abstract void main(String[]args) throws JniException, NativeException;
388 public void unschedule() {
391 schedBegin.acquire();
392 } catch(InterruptedException e) {
396 public void schedule() {
398 schedBegin.release();
400 } catch(InterruptedException e) {
404 /** Send the given task in the mailbox associated with the specified alias (waiting at most given time) */
405 public void taskSend(String mailbox, Task task, double timeout) throws NativeException, JniException {
406 MsgNative.taskSend(mailbox, task, timeout);
409 /** Send the given task in the mailbox associated with the specified alias*/
410 public void taskSend(String mailbox, Task task) throws NativeException, JniException {
411 MsgNative.taskSend(mailbox, task, -1);
414 /** Receive a task on mailbox associated with the specified mailbox */
415 public Task taskReceive(String mailbox) throws NativeException, JniException {
416 return MsgNative.taskReceive(mailbox, -1.0, null);
419 /** Receive a task on mailbox associated with the specified alias (waiting at most given time) */
420 public Task taskReceive(String mailbox, double timeout) throws NativeException, JniException {
421 return MsgNative.taskReceive(mailbox, timeout, null);
424 /** Receive a task on mailbox associated with the specified alias from given sender */
425 public Task taskReceive(String mailbox, double timeout, Host host) throws NativeException, JniException {
426 return MsgNative.taskReceive(mailbox, timeout, host);
429 /** Receive a task on mailbox associated with the specified alias from given sender*/
430 public Task taskReceive(String mailbox, Host host) throws NativeException, JniException {
431 return MsgNative.taskReceive(mailbox, -1.0, host);