2 * simgrid.msg.Host.java 1.00 07/05/01
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 ouwn process you must inherite 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) Write a default constructor and
31 * call the default constructor of the base class.
37 * (3) Override the method function
38 * public void main(String[] args) {
39 * System.out.println("Hello MSG");
42 * The name of your process must be declared in the deployment file of your simulation.
43 * For the exemple, for the previouse process Slave this file must contains a line :
44 * <process host="Maxims" function="Slave"/>, where Maxims is the host of the process
45 * Slave. All the process of your simulation are automaticaly launched and managed by Msg.
46 * A process use tasks to simulate communications or computations with another process.
47 * For more information see Task and ParallelTask. For more information on host concept
50 * @author Abdelmalek Cherier
51 * @author Martin Quinson
52 * @version 1.00, 07/05/01
60 public abstract class Process extends Thread {
62 * This attribute represents a bind between a java process object and
63 * a native process. Even if this attribute is public you must never
64 * access to it. It is set automaticatly during the build of the object.
69 * Even if this attribute is public you must never access to it.
70 * It is used to compute the id of an MSG process.
72 public static long nextProcessId = 0;
75 * Even if this attribute is public you must never access to it.
76 * It is compute automaticaly during the creation of the object.
77 * The native functions use this identifier to synchronize the process.
82 * The name of the process.
84 protected String name;
85 public String msgName() {
89 * The arguments of the method function of the process.
90 */ public Vector args;
92 /* process synchronisation tools */
93 protected Sem schedBegin, schedEnd;
96 * Default constructor. (used in ApplicationHandler to initialize it)
103 this.args = new Vector();
104 schedBegin = new Sem(0);
105 schedEnd = new Sem(0);
110 * Constructs a new process from the name of a host and his name. The method
111 * function of the process doesn't have argument.
113 * @param hostName The name of the host of the process to create.
114 * @param name The name of the process.
116 * @exception HostNotFoundException if no host with this name exists.
117 * NullPointerException if the provided name is null
118 * JniException on JNI madness
121 public Process(String hostname, String name)
122 throws NullPointerException, HostNotFoundException, JniException,
124 this(Host.getByName(hostname), name, null);
127 * Constructs a new process from the name of a host and his name. The arguments
128 * of the method function of the process are specified by the parameter args.
130 * @param hostName The name of the host of the process to create.
131 * @param name The name of the process.
132 * @param args The arguments of the main function of the process.
134 * @exception HostNotFoundException if no host with this name exists.
135 * NullPointerException if the provided name is null
136 * JniException on JNI madness
138 */ public Process(String hostname, String name, String args[])
139 throws NullPointerException, HostNotFoundException, JniException,
141 this(Host.getByName(hostname), name, args);
144 * Constructs a new process from a host and his name. The method function of the
145 * process doesn't have argument.
147 * @param host The host of the process to create.
148 * @param name The name of the process.
150 * @exception NullPointerException if the provided name is null
151 * JniException on JNI madness
154 public Process(Host host, String name) throws NullPointerException,
156 this(host, name, null);
159 * Constructs a new process from a host and his name, the arguments of here method function are
160 * specified by the parameter args.
162 * @param host The host of the process to create.
163 * @param name The name of the process.
164 * @param args The arguments of main method of the process.
166 * @exception NullPointerException if the provided name is null
167 * JniException on JNI madness
170 public Process(Host host, String name,
171 String[]args) throws NullPointerException, JniException {
172 /* This is the constructor called by all others */
175 throw new NullPointerException("Process name cannot be NULL");
178 this.args = new Vector();
181 this.args.addAll(Arrays.asList(args));
184 this.id = nextProcessId++;
186 schedBegin = new Sem(0);
187 schedEnd = new Sem(0);
189 Msg.processCreate(this, host);
192 * This method kills all running process of the simulation.
194 * @param resetPID Should we reset the PID numbers. A negative number means no reset
195 * and a positive number will be used to set the PID of the next newly
198 * @return The function returns the PID of the next created process.
200 */ public static int killAll(int resetPID) {
201 return Msg.processKillAll(resetPID);
205 * This method adds an argument in the list of the arguments of the main function
208 * @param arg The argument to add.
210 protected void addArg(String arg) {
215 * This method suspends the process by suspending the task on which it was
216 * waiting for the completion.
218 * @exception JniException on JNI madness
219 * NativeException on error in the native SimGrid code
221 public void pause() throws JniException, NativeException {
222 Msg.processSuspend(this);
225 * This method resumes a suspended process by resuming the task on which it was
226 * waiting for the completion.
228 * @exception JniException on JNI madness
229 * NativeException on error in the native SimGrid code
231 */ public void restart() throws JniException, NativeException {
232 Msg.processResume(this);
235 * This method tests if a process is suspended.
237 * @return The method returns true if the process is suspended.
238 * Otherwise the method returns false.
240 * @exception JniException on JNI madness
241 */ public boolean isSuspended() throws JniException {
242 return Msg.processIsSuspended(this);
245 * This method returns the host of a process.
247 * @return The host instance of the process.
249 * @exception JniException on JNI madness
250 * NativeException on error in the native SimGrid code
252 */ public Host getHost() throws JniException, NativeException {
253 return Msg.processGetHost(this);
256 * This static method get a process from a PID.
258 * @param PID The process identifier of the process to get.
260 * @return The process with the specified PID.
262 * @exception NativeException on error in the native SimGrid code
263 */ public static Process fromPID(int PID) throws NativeException {
264 return Msg.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
273 */ public int getPID() throws JniException, NativeException {
274 return Msg.processGetPID(this);
277 * This method returns the PID of the parent of a process.
279 * @return The PID of the parent of the process.
281 * @exception NativeException on error in the native SimGrid code
282 */ public int getPPID() throws NativeException {
283 return Msg.processGetPPID(this);
286 * This static method returns the currently running process.
288 * @return The current process.
290 * @exception NativeException on error in the native SimGrid code
293 */ public static Process currentProcess() throws NativeException {
294 return Msg.processSelf();
297 * This static method returns the PID of the currently running process.
299 * @return The PID of the current process.
300 */ public static int currentProcessPID() {
301 return Msg.processSelfPID();
305 * This static method returns the PID of the parent of the currently running process.
307 * @return The PID of the parent of current process.
309 public static int currentProcessPPID() {
310 return Msg.processSelfPPID();
314 * This function migrates a process to another host.
316 * @parm host The host where to migrate the process.
318 * @exception JniException on JNI madness
319 * NativeException on error in the native SimGrid code
321 public void migrate(Host host) throws JniException, NativeException {
322 Msg.processChangeHost(this, host);
325 * This method makes the current process sleep until time seconds have elapsed.
327 * @param seconds The time the current process must sleep.
329 * @exception NativeException on error in the native SimGrid code
330 */ public static void waitFor(double seconds) throws NativeException {
331 Msg.processWaitFor(seconds);
332 } public void showArgs() {
334 Msg.info("[" + this.name + "/" + this.getHost().getName() + "] argc=" +
336 for (int i = 0; i < this.args.size(); i++)
337 Msg.info("[" + this.msgName() + "/" + this.getHost().getName() +
338 "] args[" + i + "]=" + (String) (this.args.get(i)));
339 } catch(MsgException e) {
340 Msg.info("Damn JNI stuff");
346 * This method runs the process. Il calls the method function that you must overwrite.
348 public synchronized void run() {
351 String[]args = null; /* do not fill it before the signal or this.args will be empty */
353 //waitSignal(); /* wait for other people to fill the process in */
357 schedBegin.acquire();
358 } catch(InterruptedException e) {
363 if (this.args.size() > 0) {
365 args = new String[this.args.size()];
366 this.args.toArray(args);
370 Msg.processExit(this);
373 catch(MsgException e) {
375 Msg.info("Unexpected behavior. Stopping now");
381 * Process synchronization. The process wait the signal of the simulator to start.
383 * @exception JniException on JNI madness
385 private void waitSignal() throws JniException {
386 Msg.waitSignal(this);
390 * The main function of the process (to implement).
391 */ public abstract void main(String[]args)
392 throws JniException, NativeException;
395 public void unschedule() {
399 schedBegin.acquire();
400 } catch(InterruptedException e) {
405 public void schedule() {
408 schedBegin.release();
410 } catch(InterruptedException e) {
415 public void taskSend(Channel channel, Task task,
416 Host host) throws NativeException, JniException {
417 Msg.channelPut(channel, task, host);
418 } public Task taskReceive(Channel channel) throws NativeException,
420 return Msg.channelGet(channel);