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;
17 * A process may be defined as a code, with some private data, executing
18 * in a location (host). All the process used by your simulation must be
19 * declared in the deployment file (XML format).
20 * To create your ouwn process you must inherite your own process from this
21 * class and override the method "main()". For example if you want to use
22 * a process named Slave proceed as it :
24 * (1) import the class Process of the package simgrid.msg
25 * import simgrid.msg.Process;
27 * public class Slave extends simgrid.msg.Process {
29 * (2) Write a default constructor and
30 * call the default constructor of the base class.
36 * (3) Override the method function
37 * public void main(String[] args) {
38 * System.out.println("Hello MSG");
41 * The name of your process must be declared in the deployment file of your simulation.
42 * For the exemple, for the previouse process Slave this file must contains a line :
43 * <process host="Maxims" function="Slave"/>, where Maxims is the host of the process
44 * Slave. All the process of your simulation are automaticaly launched and managed by Msg.
45 * A process use tasks to simulate communications or computations with another process.
46 * For more information see Task and ParallelTask. For more information on host concept
49 * @author Abdelmalek Cherier
50 * @author Martin Quinson
51 * @version 1.00, 07/05/01
59 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() {
90 * The arguments of the method function of the process.
94 /* Used to schedule the process */
98 * Default constructor. (used in ApplicationHandler to initialize it)
100 protected Process() {
105 this.args = new Vector();
106 sem = new Sem(0); /* suspend the thread first */
111 * Constructs a new process from the name of a host and his name. The method
112 * function of the process doesn't have argument.
114 * @param hostName The name of the host of the process to create.
115 * @param name The name of the process.
117 * @exception HostNotFoundException if no host with this name exists.
118 * NullPointerException if the provided name is null
119 * JniException on JNI madness
122 public Process(String hostname,String name)
123 throws NullPointerException, HostNotFoundException, JniException, NativeException {
124 this(Host.getByName(hostname),name,null);
128 * Constructs a new process from the name of a host and his name. The arguments
129 * of the method function of the process are specified by the parameter args.
131 * @param hostName The name of the host of the process to create.
132 * @param name The name of the process.
133 * @param args The arguments of the main function of the process.
135 * @exception HostNotFoundException if no host with this name exists.
136 * NullPointerException if the provided name is null
137 * JniException on JNI madness
140 public Process(String hostname,String name,String args[])
141 throws NullPointerException, HostNotFoundException, JniException, NativeException {
142 this(Host.getByName(hostname),name,args);
146 * Constructs a new process from a host and his name. The method function of the
147 * process doesn't have argument.
149 * @param host The host of the process to create.
150 * @param name The name of the process.
152 * @exception NullPointerException if the provided name is null
153 * JniException on JNI madness
156 public Process(Host host,String name) throws NullPointerException, JniException {
157 this(host,name,null);
161 * Constructs a new process from a host and his name, the arguments of here method function are
162 * specified by the parameter args.
164 * @param host The host of the process to create.
165 * @param name The name of the process.
166 * @param args The arguments of main method of the process.
168 * @exception NullPointerException if the provided name is null
169 * JniException on JNI madness
172 public Process(Host host,String name,String[] args) throws NullPointerException, JniException {
173 /* This is the constructor called by all others */
176 throw new NullPointerException("Process name cannot be NULL");
179 this.args = new Vector();
182 this.args.addAll(Arrays.asList(args));
185 this.id = nextProcessId++;
187 sem = new Sem(0); /* suspend the thread first */
189 Msg.processCreate(this,host);
193 * This method kills all running process of the simulation.
195 * @param resetPID Should we reset the PID numbers. A negative number means no reset
196 * and a positive number will be used to set the PID of the next newly
199 * @return The function returns the PID of the next created process.
202 public static int killAll(int resetPID){
203 return Msg.processKillAll(resetPID);
207 * This method adds an argument in the list of the arguments of the main function
210 * @param arg The argument to add.
212 protected void addArg(String arg) {
217 * This method suspends the process by suspending the task on which it was
218 * waiting for the completion.
220 * @exception JniException on JNI madness
221 * NativeException on error in the native SimGrid code
223 public void pause() throws JniException,NativeException {
224 Msg.processSuspend(this);
228 * This method resumes a suspended process by resuming the task on which it was
229 * waiting for the completion.
231 * @exception JniException on JNI madness
232 * NativeException on error in the native SimGrid code
235 public void restart() throws JniException,NativeException {
236 Msg.processResume(this);
240 * This method tests if a process is suspended.
242 * @return The method returns true if the process is suspended.
243 * Otherwise the method returns false.
245 * @exception JniException on JNI madness
247 public boolean isSuspended() throws JniException {
248 return Msg.processIsSuspended(this);
252 * This method returns the host of a process.
254 * @return The host instance of the process.
256 * @exception JniException on JNI madness
257 * NativeException on error in the native SimGrid code
260 public Host getHost() throws JniException,NativeException{
261 return Msg.processGetHost(this);
265 * This static method get a process from a PID.
267 * @param PID The process identifier of the process to get.
269 * @return The process with the specified PID.
271 * @exception NativeException on error in the native SimGrid code
273 public static Process fromPID(int PID) throws NativeException {
274 return Msg.processFromPID(PID);
278 * This method returns the PID of the process.
280 * @return The PID of the process.
282 * @exception JniException on JNI madness
283 * NativeException on error in the native SimGrid code
285 public int getPID() throws JniException,NativeException{
286 return Msg.processGetPID(this);
290 * This method returns the PID of the parent of a process.
292 * @return The PID of the parent of the process.
294 * @exception NativeException on error in the native SimGrid code
296 public int getPPID() throws NativeException{
297 return Msg.processGetPPID(this);
301 * This static method returns the currently running process.
303 * @return The current process.
305 * @exception NativeException on error in the native SimGrid code
309 public static Process currentProcess() throws NativeException{
310 return Msg.processSelf();
314 * This static method returns the PID of the currently running process.
316 * @return The PID of the current process.
318 public static int currentProcessPID(){
319 return Msg.processSelfPID();
323 * This static method returns the PID of the parent of the currently running process.
325 * @return The PID of the parent of current process.
327 public static int currentProcessPPID(){
328 return Msg.processSelfPPID();
332 * This function migrates a process to another host.
334 * @parm host The host where to migrate the process.
336 * @exception JniException on JNI madness
337 * NativeException on error in the native SimGrid code
339 public void migrate(Host host) throws JniException, NativeException{
340 Msg.processChangeHost(this,host);
344 * This method makes the current process sleep until time seconds have elapsed.
346 * @param seconds The time the current process must sleep.
348 * @exception NativeException on error in the native SimGrid code
350 public static void waitFor(double seconds) throws NativeException {
351 Msg.processWaitFor(seconds);
355 public void showArgs(){
357 Msg.info("["+this.name+"/"+this.getHost().getName()+"] argc="+this.args.size() );
358 for(int i = 0; i < this.args.size(); i++)
359 Msg.info("["+this.msgName()+"/"+this.getHost().getName()+
360 "] args["+i+"]="+(String)(this.args.get(i)));
361 } catch (MsgException e) {
362 Msg.info("Damn JNI stuff");
368 * This method runs the process. Il calls the method function that you must overwrite.
370 public synchronized void run() {
373 String[] args = null; /* do not fill it before the signal or this.args will be empty */
375 waitSignal(); /* wait for other people to fill the process in */
377 if(this.args.size() > 0)
380 args = new String[this.args.size()];
381 this.args.toArray(args);
385 Msg.processExit(this);
386 } catch (MsgException e) {
388 Msg.info("Unexpected behavior. Stopping now");
394 * Process synchronization. The process wait the signal of the simulator to start.
396 * @exception JniException on JNI madness
398 private void waitSignal() throws JniException{
399 Msg.waitSignal(this);
404 * The main function of the process (to implement).
406 public abstract void main(String[] args)
407 throws JniException, NativeException;
410 public void unschedule() {
413 } catch(InterruptedException e)
419 public void schedule() {