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.util.Arrays;
15 import java.util.Hashtable;
16 import java.util.Vector;
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 extends Thread {
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.
58 * Even if this attribute is public you must never access to it.
59 * It is used to compute the id of an MSG process.
61 public static long nextProcessId = 0;
64 * Even if this attribute is public you must never access to it.
65 * It is compute automatically during the creation of the object.
66 * The native functions use this identifier to synchronize the process.
70 public Hashtable<String,String> properties;
73 * The name of the process.
75 protected String name;
76 public String msgName() {
79 /** The arguments of the method function of the process. */
80 public Vector<String> args;
82 /* process synchronization tools */
83 protected Sem schedBegin, schedEnd;
86 * Default constructor (used in ApplicationHandler to initialize it)
90 this.id = nextProcessId++;
93 this.args = new Vector<String>();
94 this.properties = null;
95 schedBegin = new Sem(0);
96 schedEnd = new Sem(0);
101 * Constructs a new process from the name of a host and his name. The method
102 * function of the process doesn't have argument.
104 * @param hostname The name of the host of the process to create.
105 * @param name The name of the process.
107 * @exception HostNotFoundException if no host with this name exists.
111 public Process(String hostname, String name) throws HostNotFoundException {
112 this(Host.getByName(hostname), name, null);
115 * Constructs a new process from the name of a host and his name. The arguments
116 * of the method function of the process are specified by the parameter args.
118 * @param hostname The name of the host of the process to create.
119 * @param name The name of the process.
120 * @param args The arguments of the main function of the process.
122 * @exception HostNotFoundException if no host with this name exists.
126 public Process(String hostname, String name, String args[]) throws HostNotFoundException, NativeException {
127 this(Host.getByName(hostname), name, args);
130 * Constructs a new process from a host and his name. The method function of the
131 * process doesn't have argument.
133 * @param host The host of the process to create.
134 * @param name The name of the process.
137 public Process(Host host, String name) {
138 this(host, name, null);
141 * Constructs a new process from a host and his name, the arguments of here method function are
142 * specified by the parameter args.
144 * @param host The host of the process to create.
145 * @param name The name of the process.
146 * @param args The arguments of main method of the process.
149 public Process(Host host, String name, String[]args) {
150 /* This is the constructor called by all others */
154 throw new NullPointerException("Process name cannot be NULL");
157 this.args = new Vector<String>();
159 this.args.addAll(Arrays.asList(args));
161 MsgNative.processCreate(this, host);
166 * This method kills all running process of the simulation.
168 * @param resetPID Should we reset the PID numbers. A negative number means no reset
169 * and a positive number will be used to set the PID of the next newly
172 * @return The function returns the PID of the next created process.
175 public static int killAll(int resetPID) {
176 return MsgNative.processKillAll(resetPID);
180 * This method adds an argument in the list of the arguments of the main function
183 * @param arg The argument to add.
186 protected void addArg(String arg) {
191 * Suspends the process by suspending the task on which it was
192 * waiting for the completion.
195 public void pause() {
196 MsgNative.processSuspend(this);
199 * Resumes a suspended process by resuming the task on which it was
200 * waiting for the completion.
204 public void restart() {
205 MsgNative.processResume(this);
208 * Tests if a process is suspended.
210 * @return The method returns true if the process is suspended.
211 * Otherwise the method returns false.
213 public boolean isSuspended() {
214 return MsgNative.processIsSuspended(this);
217 * Returns the host of a process.
219 * @return The host instance of the process.
221 * @exception NativeException on error in the native SimGrid code
224 public Host getHost() {
225 return MsgNative.processGetHost(this);
228 * This static method gets a process from a PID.
230 * @param PID The process identifier of the process to get.
232 * @return The process with the specified PID.
234 * @exception NativeException on error in the native SimGrid code
236 public static Process fromPID(int PID) throws NativeException {
237 return MsgNative.processFromPID(PID);
240 * This method returns the PID of the process.
242 * @return The PID of the process.
245 public int getPID() {
246 return MsgNative.processGetPID(this);
249 * This method returns the PID of the parent of a process.
251 * @return The PID of the parent of the process.
254 public int getPPID() {
255 return MsgNative.processGetPPID(this);
258 * This static method returns the currently running process.
260 * @return The current process.
263 public static Process currentProcess() {
264 return MsgNative.processSelf();
267 * Migrates a process to another host.
269 * @param host The host where to migrate the process.
272 public void migrate(Host host) {
273 MsgNative.processChangeHost(this, host);
276 * Makes the current process sleep until time seconds have elapsed.
278 * @param seconds The time the current process must sleep.
280 * @exception HostFailureException on error in the native SimGrid code
282 public static void waitFor(double seconds) throws HostFailureException {
283 MsgNative.processWaitFor(seconds);
285 public void showArgs() {
286 Msg.info("[" + this.name + "/" + this.getHost().getName() + "] argc=" +
288 for (int i = 0; i < this.args.size(); i++)
289 Msg.info("[" + this.msgName() + "/" + this.getHost().getName() +
290 "] args[" + i + "]=" + (String) (this.args.get(i)));
293 * This method runs the process. Il calls the method function that you must overwrite.
297 String[]args = null; /* do not fill it before the signal or this.args will be empty */
299 //waitSignal(); /* wait for other people to fill the process in */
303 schedBegin.acquire();
304 } catch(InterruptedException e) {
308 args = new String[this.args.size()];
309 if (this.args.size() > 0) {
310 this.args.toArray(args);
314 MsgNative.processExit(this);
316 } catch(MsgException e) {
318 Msg.info("Unexpected behavior. Stopping now");
324 * The main function of the process (to implement).
326 public abstract void main(String[]args) throws MsgException;
329 public void unschedule() {
332 schedBegin.acquire();
333 } catch(InterruptedException e) {
337 public void schedule() {
339 schedBegin.release();
341 } catch(InterruptedException e) {
345 /** Send the given task in the mailbox associated with the specified alias (waiting at most given time)
346 * @throws TimeoutException
347 * @throws HostFailureException
348 * @throws TransferFailureException */
349 public void taskSend(String mailbox, Task task, double timeout) throws TransferFailureException, HostFailureException, TimeoutException {
350 MsgNative.taskSend(mailbox, task, timeout);
353 /** Send the given task in the mailbox associated with the specified alias
354 * @throws TimeoutException
355 * @throws HostFailureException
356 * @throws TransferFailureException */
357 public void taskSend(String mailbox, Task task) throws TransferFailureException, HostFailureException, TimeoutException {
358 MsgNative.taskSend(mailbox, task, -1);
361 /** Receive a task on mailbox associated with the specified mailbox */
362 public Task taskReceive(String mailbox) throws TransferFailureException, HostFailureException, TimeoutException {
363 return MsgNative.taskReceive(mailbox, -1.0, null);
366 /** Receive a task on mailbox associated with the specified alias (waiting at most given time) */
367 public Task taskReceive(String mailbox, double timeout) throws TransferFailureException, HostFailureException, TimeoutException {
368 return MsgNative.taskReceive(mailbox, timeout, null);
371 /** Receive a task on mailbox associated with the specified alias from given sender */
372 public Task taskReceive(String mailbox, double timeout, Host host) throws TransferFailureException, HostFailureException, TimeoutException {
373 return MsgNative.taskReceive(mailbox, timeout, host);
376 /** Receive a task on mailbox associated with the specified alias from given sender*/
377 public Task taskReceive(String mailbox, Host host) throws TransferFailureException, HostFailureException, TimeoutException {
378 return MsgNative.taskReceive(mailbox, -1.0, host);