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)
89 this.id = nextProcessId++;
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.
110 public Process(String hostname, String name) throws HostNotFoundException, NativeException {
111 this(Host.getByName(hostname), name, null);
114 * Constructs a new process from the name of a host and his name. The arguments
115 * of the method function of the process are specified by the parameter args.
117 * @param hostname The name of the host of the process to create.
118 * @param name The name of the process.
119 * @param args The arguments of the main function of the process.
121 * @exception HostNotFoundException if no host with this name exists.
125 public Process(String hostname, String name, String args[]) throws HostNotFoundException, NativeException {
126 this(Host.getByName(hostname), name, args);
129 * Constructs a new process from a host and his name. The method function of the
130 * process doesn't have argument.
132 * @param host The host of the process to create.
133 * @param name The name of the process.
136 public Process(Host host, String name) {
137 this(host, name, null);
140 * Constructs a new process from a host and his name, the arguments of here method function are
141 * specified by the parameter args.
143 * @param host The host of the process to create.
144 * @param name The name of the process.
145 * @param args The arguments of main method of the process.
148 public Process(Host host, String name, String[]args) {
149 /* This is the constructor called by all others */
153 throw new NullPointerException("Process name cannot be NULL");
156 this.args = new Vector<String>();
158 this.args.addAll(Arrays.asList(args));
160 MsgNative.processCreate(this, host);
165 * This method kills all running process of the simulation.
167 * @param resetPID Should we reset the PID numbers. A negative number means no reset
168 * and a positive number will be used to set the PID of the next newly
171 * @return The function returns the PID of the next created process.
174 public static int killAll(int resetPID) {
175 return MsgNative.processKillAll(resetPID);
179 * This method adds an argument in the list of the arguments of the main function
182 * @param arg The argument to add.
185 protected void addArg(String arg) {
190 * Suspends the process by suspending the task on which it was
191 * waiting for the completion.
193 * @exception NativeException on error in the native SimGrid code
195 public void pause() throws NativeException {
196 MsgNative.processSuspend(this);
199 * Resumes a suspended process by resuming the task on which it was
200 * waiting for the completion.
202 * @exception NativeException on error in the native SimGrid code
205 public void restart() throws NativeException {
206 MsgNative.processResume(this);
209 * Tests if a process is suspended.
211 * @return The method returns true if the process is suspended.
212 * Otherwise the method returns false.
214 public boolean isSuspended() {
215 return MsgNative.processIsSuspended(this);
218 * Returns the host of a process.
220 * @return The host instance of the process.
222 * @exception NativeException on error in the native SimGrid code
225 public Host getHost() throws NativeException {
226 return MsgNative.processGetHost(this);
229 * This static method gets a process from a PID.
231 * @param PID The process identifier of the process to get.
233 * @return The process with the specified PID.
235 * @exception NativeException on error in the native SimGrid code
237 public static Process fromPID(int PID) throws NativeException {
238 return MsgNative.processFromPID(PID);
241 * This method returns the PID of the process.
243 * @return The PID of the process.
245 * @exception NativeException on error in the native SimGrid code
247 public int getPID() throws NativeException {
248 return MsgNative.processGetPID(this);
251 * This method returns the PID of the parent of a process.
253 * @return The PID of the parent of the process.
255 * @exception NativeException on error in the native SimGrid code
257 public int getPPID() throws NativeException {
258 return MsgNative.processGetPPID(this);
261 * This static method returns the currently running process.
263 * @return The current process.
265 * @exception NativeException on error in the native SimGrid code
267 public static Process currentProcess() throws NativeException {
268 return MsgNative.processSelf();
271 * Migrates a process to another host.
273 * @param host The host where to migrate the process.
275 * @exception NativeException on error in the native SimGrid code
277 public void migrate(Host host) throws NativeException {
278 MsgNative.processChangeHost(this, host);
281 * Makes the current process sleep until time seconds have elapsed.
283 * @param seconds The time the current process must sleep.
285 * @exception NativeException on error in the native SimGrid code
287 public static void waitFor(double seconds) throws NativeException {
288 MsgNative.processWaitFor(seconds);
290 public void showArgs() {
292 Msg.info("[" + this.name + "/" + this.getHost().getName() + "] argc=" +
294 for (int i = 0; i < this.args.size(); i++)
295 Msg.info("[" + this.msgName() + "/" + this.getHost().getName() +
296 "] args[" + i + "]=" + (String) (this.args.get(i)));
297 } catch(MsgException e) {
298 Msg.info("Damn JNI stuff");
304 * This method runs the process. Il calls the method function that you must overwrite.
308 String[]args = null; /* do not fill it before the signal or this.args will be empty */
310 //waitSignal(); /* wait for other people to fill the process in */
314 schedBegin.acquire();
315 } catch(InterruptedException e) {
319 args = new String[this.args.size()];
320 if (this.args.size() > 0) {
321 this.args.toArray(args);
325 MsgNative.processExit(this);
327 } catch(MsgException e) {
329 Msg.info("Unexpected behavior. Stopping now");
335 * The main function of the process (to implement).
337 public abstract void main(String[]args) throws NativeException;
340 public void unschedule() {
343 schedBegin.acquire();
344 } catch(InterruptedException e) {
348 public void schedule() {
350 schedBegin.release();
352 } catch(InterruptedException e) {
356 /** Send the given task in the mailbox associated with the specified alias (waiting at most given time) */
357 public void taskSend(String mailbox, Task task, double timeout) throws NativeException {
358 MsgNative.taskSend(mailbox, task, timeout);
361 /** Send the given task in the mailbox associated with the specified alias*/
362 public void taskSend(String mailbox, Task task) throws NativeException {
363 MsgNative.taskSend(mailbox, task, -1);
366 /** Receive a task on mailbox associated with the specified mailbox */
367 public Task taskReceive(String mailbox) throws NativeException {
368 return MsgNative.taskReceive(mailbox, -1.0, null);
371 /** Receive a task on mailbox associated with the specified alias (waiting at most given time) */
372 public Task taskReceive(String mailbox, double timeout) throws NativeException {
373 return MsgNative.taskReceive(mailbox, timeout, null);
376 /** Receive a task on mailbox associated with the specified alias from given sender */
377 public Task taskReceive(String mailbox, double timeout, Host host) throws NativeException {
378 return MsgNative.taskReceive(mailbox, timeout, host);
381 /** Receive a task on mailbox associated with the specified alias from given sender*/
382 public Task taskReceive(String mailbox, Host host) throws NativeException {
383 return MsgNative.taskReceive(mailbox, -1.0, host);