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.
12 package org.simgrid.msg;
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.
73 public Hashtable<String,String> properties;
76 * The name of the process.
78 protected String name;
83 public String msgName() {
86 /** The arguments of the method function of the process. */
87 public Vector<String> args;
89 /* process synchronization tools */
96 protected Sem schedBegin, schedEnd;
97 private boolean nativeStop = false;
100 * Default constructor (used in ApplicationHandler to initialize it)
102 protected Process() {
104 this.id = nextProcessId++;
107 this.args = new Vector<String>();
108 this.properties = null;
109 schedBegin = new Sem(0);
110 schedEnd = new Sem(0);
115 * Constructs a new process from the name of a host and his name. The method
116 * function of the process doesn't have argument.
118 * @param hostname The name of the host of the process to create.
119 * @param name The name of the process.
121 * @exception HostNotFoundException if no host with this name exists.
125 public Process(String hostname, String name) throws HostNotFoundException {
126 this(Host.getByName(hostname), name, null);
129 * Constructs a new process from the name of a host and his name. The arguments
130 * of the method function of the process are specified by the parameter args.
132 * @param hostname The name of the host of the process to create.
133 * @param name The name of the process.
134 * @param args The arguments of the main function of the process.
136 * @exception HostNotFoundException if no host with this name exists.
138 * @throws NativeException
141 public Process(String hostname, String name, String args[]) throws HostNotFoundException, NativeException {
142 this(Host.getByName(hostname), name, args);
145 * Constructs a new process from a host and his name. The method function of the
146 * process doesn't have argument.
148 * @param host The host of the process to create.
149 * @param name The name of the process.
152 public Process(Host host, String name) {
153 this(host, name, null);
156 * Constructs a new process from a host and his name, the arguments of here method function are
157 * specified by the parameter args.
159 * @param host The host of the process to create.
160 * @param name The name of the process.
161 * @param args The arguments of main method of the process.
164 public Process(Host host, String name, String[]args) {
165 /* This is the constructor called by all others */
169 throw new NullPointerException("Process name cannot be NULL");
172 this.args = new Vector<String>();
174 this.args.addAll(Arrays.asList(args));
176 MsgNative.processCreate(this, host);
181 * This method kills all running process of the simulation.
183 * @param resetPID Should we reset the PID numbers. A negative number means no reset
184 * and a positive number will be used to set the PID of the next newly
187 * @return The function returns the PID of the next created process.
190 public static int killAll(int resetPID) {
191 return MsgNative.processKillAll(resetPID);
195 * This method sets a flag to indicate that this thread must be killed. End user must use static method kill
200 public void nativeStop()
205 * getter for the flag that indicates that this thread must be killed
210 public boolean getNativeStop()
215 * checks if the flag that indicates that this thread must be killed is set to true; if true, starts to kill it. End users should not have to deal with it
216 * If you develop a new MSG native call, please include a call to interruptedStop() at the beginning of your method code, so as the process can be killed if he call
222 public static void ifInterruptedStop() {
223 if ( (Thread.currentThread() instanceof Process) &&((Process) Thread.currentThread()).getNativeStop()) {
224 throw new RuntimeException("Interrupted");
230 * This method kill a process.
231 * @param process the process to be killed.
234 public static void kill(Process process) {
235 process.nativeStop();
236 Msg.info("Process " + process.msgName() + " will be killed.");
240 * This method adds an argument in the list of the arguments of the main function
243 * @param arg The argument to add.
248 protected void addArg(String arg) {
253 * Suspends the process by suspending the task on which it was
254 * waiting for the completion.
257 public void pause() {
258 Process.ifInterruptedStop();
259 MsgNative.processSuspend(this);
262 * Resumes a suspended process by resuming the task on which it was
263 * waiting for the completion.
267 public void restart() {
268 Process.ifInterruptedStop();
269 MsgNative.processResume(this);
272 * Tests if a process is suspended.
274 * @return The method returns true if the process is suspended.
275 * Otherwise the method returns false.
277 public boolean isSuspended() {
278 Process.ifInterruptedStop();
279 return MsgNative.processIsSuspended(this);
282 * Returns the host of a process.
284 * @return The host instance of the process.
288 public Host getHost() {
289 Process.ifInterruptedStop();
290 return MsgNative.processGetHost(this);
293 * This static method gets a process from a PID.
295 * @param PID The process identifier of the process to get.
297 * @return The process with the specified PID.
299 * @exception NativeException on error in the native SimGrid code
301 public static Process fromPID(int PID) throws NativeException {
302 Process.ifInterruptedStop();
303 return MsgNative.processFromPID(PID);
306 * This method returns the PID of the process.
308 * @return The PID of the process.
311 public int getPID() {
312 Process.ifInterruptedStop();
313 return MsgNative.processGetPID(this);
316 * This method returns the PID of the parent of a process.
318 * @return The PID of the parent of the process.
321 public int getPPID() {
322 Process.ifInterruptedStop();
323 return MsgNative.processGetPPID(this);
326 * This static method returns the currently running process.
328 * @return The current process.
331 public static Process currentProcess() {
332 Process.ifInterruptedStop();
333 return MsgNative.processSelf();
336 * Migrates a process to another host.
338 * @param process The process to migrate.
339 * @param host The host where to migrate the process.
342 public static void migrate(Process process, Host host) {
343 Process.ifInterruptedStop();
344 MsgNative.processMigrate(process, host);
347 * Makes the current process sleep until time seconds have elapsed.
349 * @param seconds The time the current process must sleep.
351 * @exception HostFailureException on error in the native SimGrid code
353 public static void waitFor(double seconds) throws HostFailureException {
354 Process.ifInterruptedStop();
355 MsgNative.processWaitFor(seconds);
360 public void showArgs() {
361 Process.ifInterruptedStop();
362 Msg.info("[" + this.name + "/" + this.getHost().getName() + "] argc=" +
364 for (int i = 0; i < this.args.size(); i++)
365 Msg.info("[" + this.msgName() + "/" + this.getHost().getName() +
366 "] args[" + i + "]=" + (String) (this.args.get(i)));
369 * This method runs the process. Il calls the method function that you must overwrite.
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 */
379 schedBegin.acquire();
380 } catch(InterruptedException e) {
384 args = new String[this.args.size()];
385 if (this.args.size() > 0) {
386 this.args.toArray(args);
390 MsgNative.processExit(this);
392 } catch(MsgException e) {
394 Msg.info("Unexpected behavior. Stopping now");
397 catch(RuntimeException re) {
400 MsgNative.processExit(this);
401 Msg.info(" Process " + ((Process) Thread.currentThread()).msgName() + " has been killed.");
405 re.printStackTrace();
406 Msg.info("Unexpected behavior. Stopping now");
413 * The main function of the process (to implement).
416 * @throws MsgException
418 public abstract void main(String[]args) throws MsgException;
424 public void unschedule() {
425 //Process.ifInterruptedStop();
428 schedBegin.acquire();
429 } catch (InterruptedException e) {
436 public void schedule() {
437 //System.err.println("Scheduling process in Java");
438 //Process.ifInterruptedStop();
440 schedBegin.release();
442 } catch(InterruptedException e) {
443 System.err.println("Got an interuption while scheduling process in Java");
448 /** Send the given task in the mailbox associated with the specified alias (waiting at most given time)
452 * @throws TimeoutException
453 * @throws HostFailureException
454 * @throws TransferFailureException */
455 public void taskSend(String mailbox, Task task, double timeout) throws TransferFailureException, HostFailureException, TimeoutException {
456 Process.ifInterruptedStop();
457 MsgNative.taskSend(mailbox, task, timeout);
460 /** Send the given task in the mailbox associated with the specified alias
463 * @throws TimeoutException
464 * @throws HostFailureException
465 * @throws TransferFailureException */
466 public void taskSend(String mailbox, Task task) throws TransferFailureException, HostFailureException, TimeoutException {
467 Process.ifInterruptedStop();
468 MsgNative.taskSend(mailbox, task, -1);
471 /** Receive a task on mailbox associated with the specified mailbox
474 * @throws TransferFailureException
475 * @throws HostFailureException
476 * @throws TimeoutException
478 public Task taskReceive(String mailbox) throws TransferFailureException, HostFailureException, TimeoutException {
479 Process.ifInterruptedStop();
480 return MsgNative.taskReceive(mailbox, -1.0, null);
483 /** Receive a task on mailbox associated with the specified alias (waiting at most given time)
487 * @throws TransferFailureException
488 * @throws HostFailureException
489 * @throws TimeoutException
491 public Task taskReceive(String mailbox, double timeout) throws TransferFailureException, HostFailureException, TimeoutException {
492 Process.ifInterruptedStop();
493 return MsgNative.taskReceive(mailbox, timeout, null);
496 /** Receive a task on mailbox associated with the specified alias from given sender
501 * @throws TransferFailureException
502 * @throws HostFailureException
503 * @throws TimeoutException
505 public Task taskReceive(String mailbox, double timeout, Host host) throws TransferFailureException, HostFailureException, TimeoutException {
506 Process.ifInterruptedStop();
507 return MsgNative.taskReceive(mailbox, timeout, host);
510 /** Receive a task on mailbox associated with the specified alias from given sender
514 * @throws TransferFailureException
515 * @throws HostFailureException
516 * @throws TimeoutException
518 public Task taskReceive(String mailbox, Host host) throws TransferFailureException, HostFailureException, TimeoutException {
519 Process.ifInterruptedStop();
520 return MsgNative.taskReceive(mailbox, -1.0, host);