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 {
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.
194 public void pause() {
195 MsgNative.processSuspend(this);
198 * Resumes a suspended process by resuming the task on which it was
199 * waiting for the completion.
203 public void restart() {
204 MsgNative.processResume(this);
207 * Tests if a process is suspended.
209 * @return The method returns true if the process is suspended.
210 * Otherwise the method returns false.
212 public boolean isSuspended() {
213 return MsgNative.processIsSuspended(this);
216 * Returns the host of a process.
218 * @return The host instance of the process.
220 * @exception NativeException on error in the native SimGrid code
223 public Host getHost() {
224 return MsgNative.processGetHost(this);
227 * This static method gets a process from a PID.
229 * @param PID The process identifier of the process to get.
231 * @return The process with the specified PID.
233 * @exception NativeException on error in the native SimGrid code
235 public static Process fromPID(int PID) throws NativeException {
236 return MsgNative.processFromPID(PID);
239 * This method returns the PID of the process.
241 * @return The PID of the process.
244 public int getPID() {
245 return MsgNative.processGetPID(this);
248 * This method returns the PID of the parent of a process.
250 * @return The PID of the parent of the process.
253 public int getPPID() {
254 return MsgNative.processGetPPID(this);
257 * This static method returns the currently running process.
259 * @return The current process.
262 public static Process currentProcess() {
263 return MsgNative.processSelf();
266 * Migrates a process to another host.
268 * @param host The host where to migrate the process.
271 public void migrate(Host host) {
272 MsgNative.processChangeHost(this, host);
275 * Makes the current process sleep until time seconds have elapsed.
277 * @param seconds The time the current process must sleep.
279 * @exception HostFailureException on error in the native SimGrid code
281 public static void waitFor(double seconds) throws HostFailureException {
282 MsgNative.processWaitFor(seconds);
284 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)));
291 } catch(MsgException e) {
292 Msg.info("Damn JNI stuff");
298 * This method runs the process. Il calls the method function that you must overwrite.
302 String[]args = null; /* do not fill it before the signal or this.args will be empty */
304 //waitSignal(); /* wait for other people to fill the process in */
308 schedBegin.acquire();
309 } catch(InterruptedException e) {
313 args = new String[this.args.size()];
314 if (this.args.size() > 0) {
315 this.args.toArray(args);
319 MsgNative.processExit(this);
321 } catch(MsgException e) {
323 Msg.info("Unexpected behavior. Stopping now");
329 * The main function of the process (to implement).
331 public abstract void main(String[]args) throws MsgException;
334 public void unschedule() {
337 schedBegin.acquire();
338 } catch(InterruptedException e) {
342 public void schedule() {
344 schedBegin.release();
346 } catch(InterruptedException e) {
350 /** Send the given task in the mailbox associated with the specified alias (waiting at most given time)
351 * @throws TimeoutException
352 * @throws HostFailureException
353 * @throws TransferFailureException */
354 public void taskSend(String mailbox, Task task, double timeout) throws TransferFailureException, HostFailureException, TimeoutException {
355 MsgNative.taskSend(mailbox, task, timeout);
358 /** Send the given task in the mailbox associated with the specified alias
359 * @throws TimeoutException
360 * @throws HostFailureException
361 * @throws TransferFailureException */
362 public void taskSend(String mailbox, Task task) throws TransferFailureException, HostFailureException, TimeoutException {
363 MsgNative.taskSend(mailbox, task, -1);
366 /** Receive a task on mailbox associated with the specified mailbox */
367 public Task taskReceive(String mailbox) throws TransferFailureException, HostFailureException, TimeoutException {
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 TransferFailureException, HostFailureException, TimeoutException {
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 TransferFailureException, HostFailureException, TimeoutException {
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 TransferFailureException, HostFailureException, TimeoutException {
383 return MsgNative.taskReceive(mailbox, -1.0, host);