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;
99 * Default constructor (used in ApplicationHandler to initialize it)
101 protected Process() {
103 this.id = nextProcessId++;
106 this.args = new Vector<String>();
107 this.properties = null;
108 schedBegin = new Sem(0);
109 schedEnd = new Sem(0);
114 * Constructs a new process from the name of a host and his name. The method
115 * function of the process doesn't have argument.
117 * @param hostname The name of the host of the process to create.
118 * @param name The name of the process.
120 * @exception HostNotFoundException if no host with this name exists.
124 public Process(String hostname, String name) throws HostNotFoundException {
125 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.
137 * @throws NativeException
140 public Process(String hostname, String name, String args[]) throws HostNotFoundException, NativeException {
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.
151 public Process(Host host, String name) {
152 this(host, name, null);
155 * Constructs a new process from a host and his name, the arguments of here method function are
156 * specified by the parameter args.
158 * @param host The host of the process to create.
159 * @param name The name of the process.
160 * @param args The arguments of main method of the process.
163 public Process(Host host, String name, String[]args) {
164 /* This is the constructor called by all others */
168 throw new NullPointerException("Process name cannot be NULL");
171 this.args = new Vector<String>();
173 this.args.addAll(Arrays.asList(args));
175 MsgNative.processCreate(this, host);
180 * This method kills all running process of the simulation.
182 * @param resetPID Should we reset the PID numbers. A negative number means no reset
183 * and a positive number will be used to set the PID of the next newly
186 * @return The function returns the PID of the next created process.
189 public static int killAll(int resetPID) {
190 return MsgNative.processKillAll(resetPID);
195 * This method kill the current process.
196 * @param process the process to be killed.
199 public static void kill(Process process) {
200 MsgNative.processKill(process);
203 * This method adds an argument in the list of the arguments of the main function
206 * @param arg The argument to add.
211 protected void addArg(String arg) {
216 * Suspends the process by suspending the task on which it was
217 * waiting for the completion.
220 public void pause() {
221 MsgNative.processSuspend(this);
224 * Resumes a suspended process by resuming the task on which it was
225 * waiting for the completion.
229 public void restart() {
230 MsgNative.processResume(this);
233 * Tests if a process is suspended.
235 * @return The method returns true if the process is suspended.
236 * Otherwise the method returns false.
238 public boolean isSuspended() {
239 return MsgNative.processIsSuspended(this);
242 * Returns the host of a process.
244 * @return The host instance of the process.
248 public Host getHost() {
249 return MsgNative.processGetHost(this);
252 * This static method gets a process from a PID.
254 * @param PID The process identifier of the process to get.
256 * @return The process with the specified PID.
258 * @exception NativeException on error in the native SimGrid code
260 public static Process fromPID(int PID) throws NativeException {
261 return MsgNative.processFromPID(PID);
264 * This method returns the PID of the process.
266 * @return The PID of the process.
269 public int getPID() {
270 return MsgNative.processGetPID(this);
273 * This method returns the PID of the parent of a process.
275 * @return The PID of the parent of the process.
278 public int getPPID() {
279 return MsgNative.processGetPPID(this);
282 * This static method returns the currently running process.
284 * @return The current process.
287 public static Process currentProcess() {
288 return MsgNative.processSelf();
291 * Migrates a process to another host.
293 * @param process The process to migrate.
294 * @param host The host where to migrate the process.
297 public static void migrate(Process process, Host host) {
298 MsgNative.processMigrate(process, host);
301 * Makes the current process sleep until time seconds have elapsed.
303 * @param seconds The time the current process must sleep.
305 * @exception HostFailureException on error in the native SimGrid code
307 public static void waitFor(double seconds) throws HostFailureException {
308 MsgNative.processWaitFor(seconds);
313 public void showArgs() {
314 Msg.info("[" + this.name + "/" + this.getHost().getName() + "] argc=" +
316 for (int i = 0; i < this.args.size(); i++)
317 Msg.info("[" + this.msgName() + "/" + this.getHost().getName() +
318 "] args[" + i + "]=" + (String) (this.args.get(i)));
321 * This method runs the process. Il calls the method function that you must overwrite.
325 String[]args = null; /* do not fill it before the signal or this.args will be empty */
327 //waitSignal(); /* wait for other people to fill the process in */
331 schedBegin.acquire();
332 } catch(InterruptedException e) {
336 args = new String[this.args.size()];
337 if (this.args.size() > 0) {
338 this.args.toArray(args);
342 MsgNative.processExit(this);
344 } catch(MsgException e) {
346 Msg.info("Unexpected behavior. Stopping now");
352 * The main function of the process (to implement).
355 * @throws MsgException
357 public abstract void main(String[]args) throws MsgException;
363 public void unschedule() {
366 schedBegin.acquire();
367 } catch(InterruptedException e) {
374 public void schedule() {
375 //System.err.println("Scheduling process in Java");
377 schedBegin.release();
379 } catch(InterruptedException e) {
380 System.err.println("Got an interuption while scheduling process in Java");
385 /** Send the given task in the mailbox associated with the specified alias (waiting at most given time)
389 * @throws TimeoutException
390 * @throws HostFailureException
391 * @throws TransferFailureException */
392 public void taskSend(String mailbox, Task task, double timeout) throws TransferFailureException, HostFailureException, TimeoutException {
393 MsgNative.taskSend(mailbox, task, timeout);
396 /** Send the given task in the mailbox associated with the specified alias
399 * @throws TimeoutException
400 * @throws HostFailureException
401 * @throws TransferFailureException */
402 public void taskSend(String mailbox, Task task) throws TransferFailureException, HostFailureException, TimeoutException {
403 MsgNative.taskSend(mailbox, task, -1);
406 /** Receive a task on mailbox associated with the specified mailbox
409 * @throws TransferFailureException
410 * @throws HostFailureException
411 * @throws TimeoutException
413 public Task taskReceive(String mailbox) throws TransferFailureException, HostFailureException, TimeoutException {
414 return MsgNative.taskReceive(mailbox, -1.0, null);
417 /** Receive a task on mailbox associated with the specified alias (waiting at most given time)
421 * @throws TransferFailureException
422 * @throws HostFailureException
423 * @throws TimeoutException
425 public Task taskReceive(String mailbox, double timeout) throws TransferFailureException, HostFailureException, TimeoutException {
426 return MsgNative.taskReceive(mailbox, timeout, null);
429 /** Receive a task on mailbox associated with the specified alias from given sender
434 * @throws TransferFailureException
435 * @throws HostFailureException
436 * @throws TimeoutException
438 public Task taskReceive(String mailbox, double timeout, Host host) throws TransferFailureException, HostFailureException, TimeoutException {
439 return MsgNative.taskReceive(mailbox, timeout, host);
442 /** Receive a task on mailbox associated with the specified alias from given sender
446 * @throws TransferFailureException
447 * @throws HostFailureException
448 * @throws TimeoutException
450 public Task taskReceive(String mailbox, Host host) throws TransferFailureException, HostFailureException, TimeoutException {
451 return MsgNative.taskReceive(mailbox, -1.0, host);