Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Change "ProcessKilledException" to "ProcessKilledError" -> make it not catchable...
[simgrid.git] / org / simgrid / msg / Process.java
1 /*
2  * $Id$
3  *
4  * Copyright 2006,2007 Martin Quinson, Malek Cherier           
5  * All right reserved. 
6  *
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. 
10  */
11
12 package org.simgrid.msg;
13  
14 import java.util.Arrays;
15 import java.util.Hashtable;
16 import java.util.Vector;
17 import java.lang.Runnable;
18 import java.util.concurrent.Semaphore;
19
20 /**
21  * A process may be defined as a code, with some private data, executing 
22  * in a location (host). All the process used by your simulation must be
23  * declared in the deployment file (XML format).
24  * To create your own process you must inherit your own process from this 
25  * class and override the method "main()". For example if you want to use 
26  * a process named Slave proceed as it :
27  *
28  * (1) import the class Process of the package simgrid.msg
29  * import simgrid.msg.Process;
30  * 
31  * public class Slave extends simgrid.msg.Process {
32  *
33  *  (2) Override the method function
34  * 
35  *  \verbatim
36  *      public void main(String[] args) {
37  *              System.out.println("Hello MSG");
38  *      }
39  *  \endverbatim
40  * }
41  * The name of your process must be declared in the deployment file of your simulation.
42  * For the example, for the previous 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 automatically launched and managed by Msg.
45  * A process use tasks to simulate communications or computations with another process. 
46  * For more information see Task. For more information on host concept 
47  * see Host.
48  * 
49  */
50
51 public abstract class Process implements Runnable {
52         /**
53          * This attribute represents a bind between a java process object and
54          * a native process. Even if this attribute is public you must never
55          * access to it. It is set automatically during the build of the object.
56          */
57         public long bind;
58
59         /**
60          * Even if this attribute is public you must never access to it.
61          * It is used to compute the id of an MSG process.
62          */
63         public static long nextProcessId = 0;
64
65         /**
66          * Even if this attribute is public you must never access to it.
67          * It is compute automatically during the creation of the object. 
68          * The native functions use this identifier to synchronize the process.
69          */
70         public long id;
71
72     public Hashtable<String,String> properties;
73
74         /**
75          * The name of the process.                                                     
76          */
77         protected String name;
78         /**
79           * The PID of the process
80           */
81         protected int pid = -1;
82         /**
83          * The PPID of the process 
84          */
85         protected int ppid = -1;
86         /**
87          * The host of the process
88          */
89         protected Host host = null;
90
91         /** The arguments of the method function of the process. */     
92         public Vector<String> args;
93         
94         /**
95          * Default constructor (used in ApplicationHandler to initialize it)
96          */
97         protected Process() {
98                 this.id = nextProcessId++;
99                 this.name = null;
100                 this.bind = 0;
101                 this.args = new Vector<String>();
102                 this.properties = null;
103         }
104
105
106         /**
107          * Constructs a new process from the name of a host and his name. The method
108          * function of the process doesn't have argument.
109          *
110          * @param hostname              The name of the host of the process to create.
111          * @param name                  The name of the process.
112          *
113          * @exception                   HostNotFoundException  if no host with this name exists.
114          *                      
115          *
116          */
117         public Process(String hostname, String name) throws HostNotFoundException {
118                 this(Host.getByName(hostname), name, null);
119         }
120         /**
121          * Constructs a new process from the name of a host and his name. The arguments
122          * of the method function of the process are specified by the parameter args.
123          *
124          * @param hostname              The name of the host of the process to create.
125          * @param name                  The name of the process.
126          * @param args                  The arguments of the main function of the process.
127          *
128          * @exception                   HostNotFoundException  if no host with this name exists.
129      *                      NativeException
130      * @throws NativeException
131          *
132          */ 
133         public Process(String hostname, String name, String args[]) throws HostNotFoundException, NativeException {
134                 this(Host.getByName(hostname), name, args);
135         }
136         /**
137          * Constructs a new process from a host and his name. The method function of the 
138          * process doesn't have argument.
139          *
140          * @param host                  The host of the process to create.
141          * @param name                  The name of the process.
142          *
143          */
144         public Process(Host host, String name) {
145                 this(host, name, null);
146         }
147         /**
148          * Constructs a new process from a host and his name, the arguments of here method function are
149          * specified by the parameter args.
150          *
151          * @param host                  The host of the process to create.
152          * @param name                  The name of the process.
153          * @param args                  The arguments of main method of the process.
154          *
155          */
156         public Process(Host host, String name, String[]args) {
157                 /* This is the constructor called by all others */
158                 this();
159                 this.host = host;
160                 if (name == null)
161                         throw new NullPointerException("Process name cannot be NULL");
162                 this.name = name;
163
164                 this.args = new Vector<String>();
165                 if (null != args)
166                         this.args.addAll(Arrays.asList(args));
167                 
168                 this.properties = new Hashtable<String,String>();
169                 
170         }
171         /**
172          * The natively implemented method to create an MSG process.
173          * @param host    A valid (binded) host where create the process.
174          */
175         protected native void create(String hostName) throws HostNotFoundException;
176         /**
177          * This method kills all running process of the simulation.
178          *
179          * @param resetPID              Should we reset the PID numbers. A negative number means no reset
180          *                                              and a positive number will be used to set the PID of the next newly
181          *                                              created process.
182          *
183          * @return                              The function returns the PID of the next created process.
184          *                      
185          */ 
186         public static native int killAll(int resetPID);
187
188         /**
189          * This method kill a process.
190          * @param process  the process to be killed.
191          *
192          */
193         public native void kill();
194         /**
195          * Suspends the process by suspending the task on which it was
196          * waiting for the completion.
197          *
198          */
199         public native void pause();
200         /**
201          * Resumes a suspended process by resuming the task on which it was
202          * waiting for the completion.
203          *
204          *
205          */ 
206         public native void restart();
207         /**
208          * Tests if a process is suspended.
209          *
210          * @return                              The method returns true if the process is suspended.
211          *                                              Otherwise the method returns false.
212          */ 
213         public native boolean isSuspended();
214         /**
215          * Returns the name of the process
216          */
217         public String msgName() {
218                 return this.name;
219         }
220         /**
221          * Returns the host of the process.
222          * @return                              The host instance of the process.
223          */ 
224         public Host getHost() {
225                 return this.host;
226         }
227         /**
228          * This static method gets a process from a PID.
229          *
230          * @param PID                   The process identifier of the process to get.
231          *
232          * @return                              The process with the specified PID.
233          *
234          * @exception                   NativeException on error in the native SimGrid code
235          */ 
236         public static native Process fromPID(int PID) throws NativeException;
237         /**
238          * This method returns the PID of the process.
239          *
240          * @return                              The PID of the process.
241          *
242          */ 
243         public int getPID()  {
244                 return pid;
245         }
246         /**
247          * This method returns the PID of the parent of a process.
248          *
249          * @return                              The PID of the parent of the process.
250          *
251          */ 
252         public int getPPID()  {
253                 return ppid;
254         }
255         /**
256          * This static method returns the currently running process.
257          *
258          * @return                              The current process.
259          *
260          */ 
261         public static native Process currentProcess();
262         /**
263          * Migrates a process to another host.
264          *
265          * @param process               The process to migrate.
266          * @param host                  The host where to migrate the process.
267          *
268          */
269         public native static void migrate(Process process, Host host);  
270         /**
271          * Makes the current process sleep until millis millisecondes have elapsed.
272          * You should note that unlike "waitFor" which takes seconds, this method takes milliseconds.
273          * FIXME: Not optimal, maybe we should have two native functions.
274          * @param millis the length of time to sleep in milliseconds.
275          */
276         public static void sleep(long millis)  {
277                 sleep(millis,0);
278         }
279         /**
280          * Makes the current process sleep until millis milliseconds and nanos nanoseconds 
281          * have elapsed.
282          * You should note that unlike "waitFor" which takes seconds, this method takes milliseconds and nanoseconds.
283          * Overloads Thread.sleep.
284          * @param millis the length of time to sleep in milliseconds.
285          * @param nanos additionnal nanoseconds to sleep.
286          */
287         public native static void sleep(long millis, int nanos);
288         /**
289          * Makes the current process sleep until time seconds have elapsed.
290          * @param seconds               The time the current process must sleep.
291          */ 
292         public native void waitFor(double seconds);    
293         /**
294      *
295      */
296     public void showArgs() {
297                 Msg.info("[" + this.name + "/" + this.getHost().getName() + "] argc=" +
298                                 this.args.size());
299                 for (int i = 0; i < this.args.size(); i++)
300                         Msg.info("[" + this.msgName() + "/" + this.getHost().getName() +
301                                         "] args[" + i + "]=" + (String) (this.args.get(i)));
302         }
303     /**
304      * This method actually creates and run the process.
305      * @throws HostNotFoundException
306      */
307     public void start() throws HostNotFoundException {
308         create(host.getName());
309     }
310     
311         /**
312          * This method runs the process. Il calls the method function that you must overwrite.
313          */
314         public void run() {
315
316                 String[] args = null;      /* do not fill it before the signal or this.args will be empty */
317                 //waitSignal(); /* wait for other people to fill the process in */
318
319                 try {
320                         args = new String[this.args.size()];
321                         if (this.args.size() > 0) {
322                                 this.args.toArray(args);
323                         }
324
325                         this.main(args);
326                 } catch(MsgException e) {
327                         e.printStackTrace();
328                         Msg.info("Unexpected behavior. Stopping now");
329                         System.exit(1);
330                 }
331                  catch(ProcessKilledError pk) {
332
333                  }      
334         }
335
336         /**
337          * The main function of the process (to implement).
338      *
339      * @param args
340      * @throws MsgException
341      */
342         public abstract void main(String[]args) throws MsgException;
343
344     
345         /**
346          * Class initializer, to initialize various JNI stuff
347          */
348         public static native void nativeInit();
349         static {
350                 nativeInit();
351         }
352 }