4 * Copyright 2006,2007 Martin Quinson, Malek Cherier
\r
5 * All right reserved.
\r
7 * This program is free software; you can redistribute
\r
8 * it and/or modify it under the terms of the license
\r
9 *(GNU LGPL) which comes with this package.
\r
13 #ifndef MSG_PROCESS_HPP
\r
14 #define MSG_PROCESS_HPP
\r
16 // Compilation C++ recquise
\r
18 #error Process.hpp requires C++ compilation (use a .cxx suffix)
\r
21 #include <msg/datatypes.h>
\r
23 #include <ApplicationHandler.hpp>
\r
24 #include <Object.hpp>
\r
30 class NullPointerException;
\r
31 class HostNotFoundException;
\r
32 class ProcessNotFoundException;
\r
33 class InvalidArgumentException;
\r
34 class BadAllocException;
\r
35 class LogicException;
\r
38 class ApplicationHandler;
\r
42 // SimGrid::Msg::Process class declaration.
\r
43 class SIMGRIDX_EXPORT Process : public Object
\r
46 friend ApplicationHandler::ProcessFactory;
\r
48 MSG_DECLARE_DYNAMIC(Process);
\r
51 // Disable the default constructor.
\r
55 /*! \brief Constructs a process from the name of the host and its name.
\r
57 * \param hostName The host name of the process to create.
\r
58 * \param name The name of the process to create.
\r
60 * \exception If the constructor failed, it throws one of the exceptions described
\r
63 * [NullPointerException] if the name of the process is NULL or if the
\r
64 * name of the host is NULL.
\r
65 * [HostNotFoundException] if the host is not found.
\r
67 Process(const char* hostName, const char* name)
\r
68 throw(NullPointerException, HostNotFoundException);
\r
70 /*! \brief Constructs a process from a reference to an host object and its name.
\r
72 * \param rHost A reference to the host object representing the native
\r
73 * MSG host where to create the process.
\r
74 * \param name The name of the process to create.
\r
76 * \exception If the constructor failed, it throws the exception described
\r
79 * [NullPointerException] if the name of process is NULL.
\r
81 Process(const Host& rHost, const char* name)
\r
82 throw(NullPointerException);
\r
84 /*! brief Construct a proces from reference to an host object and the name of the process.
\r
85 * This constuctor takes also the list of the arguments of the process.
\r
87 * \param host A reference to the host where to create the process.
\r
88 * \param name The name of the process to create.
\r
89 * \param argc The number of the arguments of the process to create.
\r
90 * \param argv The list of arguments of the process to create.
\r
92 * \exception If the constructor failed, it throws one of the exceptions described
\r
95 * [NullPointerException] if the name of the host is NULL or
\r
96 * if the name of the process is NULL.
\r
97 * [InvalidArgumentException] if the value of the parameter argv is
\r
99 * [LogicException] if the parameter argv is NULL and the
\r
100 * parameter argc is different than zero
\r
101 * if the parameter argv is not NULL and
\r
102 * the parameter argc is zero.
\r
104 Process(const Host& rHost, const char* name, int argc, char** argv)
\r
105 throw(NullPointerException, InvalidArgumentException, LogicException);
\r
107 /*! brief Constructs a proces from the name of a host and the name of the process.
\r
108 * This constuctor takes also the list of the arguments of the process.
\r
110 * \param hostName The name of the host where to create the process.
\r
111 * \param name The name of the process to create.
\r
112 * \param argc The number of the arguments of the process to create.
\r
113 * \param argv The list of arguments of the process to create.
\r
115 * \exception If the constructor failed, it throws one of the exceptions described
\r
118 * [NullPointerException] if the name of the process or if the name
\r
119 * of the host is NULL.
\r
120 * [InvalidArgumentException] if the value of the parameter argv is
\r
122 * [LogicException] if the parameter argv is NULL and the
\r
123 * parameter argc is different than zero
\r
124 * if the parameter argv is not NULL and
\r
125 * the parameter argc is zero.
\r
126 * [HostNotFoundException] if the specified host is no found.
\r
128 Process(const char* hostName, const char* name, int argc, char** argv)
\r
129 throw(NullPointerException, HostNotFoundException);
\r
131 /*! \brief Process::killAll() - kill all the running processes of the simulation.
\r
133 * \param resetPID Should we reset the PID numbers. A negative number means no reset
\r
134 * and a positive number will be used to set the PID of the next newly
\r
137 * \return The static method returns the PID of the next created process.
\r
139 static int killAll(int resetPID);
\r
141 /*! \brief Process::suspend() - Suspend an MSG process.
\r
143 * \excetpion If this method failed, it throws one the exception described below:
\r
145 * [MsgException] if an internal exception occurs during the operation.
\r
148 throw(MsgException);
\r
152 /*! \brief Process::resume() - Resume the MSG process.
\r
154 * \exception If this method failed, it throws the exception described below:
\r
156 * [MsgException] if an internal exception occurs during the operation.
\r
159 throw(MsgException);
\r
161 /*! \brief Process::isSuspend() - Tests if a process is suspended.
\r
163 * \return This method returns 1 is the process is suspended.
\r
164 * Otherwise the method returns 0.
\r
166 int isSuspended(void);
\r
168 /*! \brief Process::getHost() - Retrieves the host of a process object.
\r
170 * \return The method returns a reference to the
\r
171 * host of the process.
\r
174 Host& getHost(void);
\r
176 /*! \brief Process::fromPID() - Retrieves a process from its PID.
\r
178 * \param PID The PID of the process to retrieve.
\r
180 * \return If successful the method returns a reference to
\r
181 * to process. Otherwise, the method throws one of
\r
182 * the exceptions described below:
\r
184 * \exception [ProcessNotFoundException] if the process is not found (no process with this PID).
\r
186 * [InvalidArgumentException] if the parameter PID is less than 1.
\r
188 * [MsgException] if a native error occurs during the operation.
\r
190 static Process& fromPID(int PID)
\r
191 throw(ProcessNotFoundException, InvalidArgumentException, MsgException);
\r
193 /*! \brief Process::getPID() - Gets the PID of a process object.
\r
195 * \return This method returns the PID of a process object.
\r
199 /*! \brief Process::getPPID() - Gets the parent PID of a process object.
\r
201 * \return This method returns the parent PID of a process object.
\r
205 /*! \brief Process::getName() - Gets the name of a process object.
\r
207 * \return This method returns the name of a process object.
\r
209 const char* getName(void) const;
\r
211 /*! \brief Process::currentProcess() - Retrieves the current process.
\r
213 * \return This method returns a reference to the current process. Otherwise
\r
214 * the method throws the excepction described below:
\r
216 * \exception [MsgException] if an internal exception occurs.
\r
218 static Process& currentProcess(void)
\r
219 throw (MsgException);
\r
221 /*! \brief Process::currentProcessPID() - Retrieves the PID of the current process.
\r
223 * \return This method returns the PID of the current process.
\r
225 static int currentProcessPID(void);
\r
227 /*! \brief Process::currentProcessPPID() - Retrieves the parent PID of the current process.
\r
229 * \return This method returns the parent PID of the current process.
\r
231 static int currentProcessPPID(void);
\r
233 /*! \brief Process::migrate() - Migrate a process object to an another host.
\r
235 * \param rHost A reference to the host to migrate the process to.
\r
237 * \return If successful the method migrate the process to the specified
\r
238 * host. Otherwise the method throws the exception described
\r
241 * \exception [MsgException] if an internal exception occurs.
\r
243 void migrate(const Host& rHost)
\r
244 throw(MsgException);
\r
246 /*! \brief Process::sleep() - Makes the current process sleep until time seconds have elapsed.
\r
248 * \param seconds The number of seconds to sleep.
\r
250 * \execption If this method failed, it throws one of the exceptions described
\r
253 * [InvalidArgumentException] if the parameter seconds is
\r
254 * less or equals to zero.
\r
256 * [MsgException] if an internal exception occurs.
\r
258 static void sleep(double seconds)
\r
259 throw(InvalidArgumentException, MsgException);
\r
261 /*! \brief Process::putTask() - This method puts a task on a given channel of a given host.
\r
263 * \exception If this method failed, it throws one of the exceptions described
\r
266 * [InvalidArgumentException] if the channel number is negative.
\r
268 * [MsgException] if an internal exception occurs.
\r
270 void putTask(const Host& rHost, int channel, const Task& rTask)
\r
271 throw(InvalidArgumentException, MsgException);
\r
273 /*! \brief Process::putTask() - This method puts a task on a given channel of a given host (waiting at most given time).
\r
275 * \exception If this method failed, it throws one of the exceptions described below:
\r
277 * [MsgException] if an internal error occurs.
\r
279 * [InvalidArgumentException] if the value of the channel specified as
\r
280 * parameter is negative or if the timeout value
\r
281 * is less than zero and différent of -1.
\r
283 * \remark Set the timeout with -1.0 to disable it.
\r
285 void putTask(const Host& rHost, int channel, const Task& rTask, double timeout)
\r
286 throw(InvalidArgumentException, MsgException);
\r
288 /*! \brief Process::getTask() - Retrieves a task from a channel number (waiting at most given time).
\r
290 * \param channel The number of the channel where to get the task.
\r
292 * \return If successful the method returns a reference to
\r
293 * the getted task. Otherwise the method throws one
\r
294 * of the exceptions described below:
\r
296 * \exception [InvalidArgumentException] if the channel number is negative.
\r
298 * [MsgException] if an internal exception occurs.
\r
300 Task& getTask(int channel)
\r
301 throw(InvalidArgumentException, MsgException);
\r
303 /*! \brief Process::taskGet() - Retrieves a task from a channel number (waiting at most given time).
\r
305 * \param channel The number of the channel where to get the task.
\r
306 * \param timeout The timeout value.
\r
308 * \return If successful the method returns a reference to
\r
309 * the getted task. Otherwise the method throws one
\r
310 * of the exceptions described below:
\r
312 * \exception [InvalidArgumentException] if the channel number is negative
\r
313 * or if the timeout value is less than
\r
314 * zero and different of -1.0.
\r
315 * [MsgException] if an internal exception occurs.
\r
317 Task& getTask(int channel, double timeout)
\r
318 throw(InvalidArgumentException, MsgException);
\r
320 /*! \brief Process::taskGet() - Retrieves a task from a channel number and a host.
\r
322 * \param channel The number of the channel where to get the task.
\r
323 * \param host The host of the channel to get the task.
\r
325 * \return If successful the method returns a reference to
\r
326 * the getted task. Otherwise the method throws one
\r
327 * of the exceptions described below.
\r
329 * \exception [InvalidArgumentException] if the channel number is negative.
\r
330 * [MsgException] if an internal exception occurs.
\r
332 Task& getTask(int channel, const Host& rHost)
\r
333 throw(InvalidArgumentException, MsgException);
\r
335 /*! \brief Process::taskGet() - Retrieves a task from a channel number and a host (waiting at most given time).
\r
337 * \param channel The number of the channel where to get the task.
\r
338 * \param timeout The timeout value.
\r
339 * \param rHost The host owning the channel.
\r
341 * \return If successful the method returns a reference to
\r
342 * the getted task. Otherwise the method throws one
\r
343 * of the exceptions described below:
\r
345 * \exception [InvalidArgumentException] if the channel number is negative or
\r
346 * if the timeout value is negative and different
\r
348 * [MsgException] if an internal exception occurs.
\r
350 * \remark Set the timeout with -1.0 to disable it.
\r
352 Task& getTask(int channel, double timeout, const Host& rHost)
\r
353 throw(InvalidArgumentException, MsgException);
\r
355 /*! \brief Process::sendTask() - Sends the given task in the mailbox identified by the specified alias
\r
356 * (waiting at most given time).
\r
358 * \param alias The alias of the mailbox where to send the task.
\r
359 * \param rTask A reference to the task object to send.
\r
360 * \param timeout The timeout value.
\r
362 * \exception If this method failed, it throws one of the exceptions described below:
\r
364 * [NullPointerException] if the alias specified as parameter is NULL.
\r
366 * [InvalidArgumentException] if the timeout value is negative and different than
\r
368 * [MsgException] if an internal exception occurs.
\r
370 * \remark Set the timeout with -1.0 to disable it.
\r
372 void sendTask(const char* alias, const Task& rTask, double timeout)
\r
373 throw(NullPointerException, InvalidArgumentException, MsgException);
\r
375 /*! \brief Process::sendTask() - Sends the given task in the mailbox identified by the specified alias.
\r
377 * \param alias The alias of the mailbox where to send the task.
\r
378 * \param rTask A reference to the task object to send.
\r
380 * \exception If this method failed, it throws one of the exceptions described below:
\r
382 * [NullPointerException] if the alias parameter is NULL.
\r
384 * [MsgException] if an internal exception occurs.
\r
387 void sendTask(const char* alias, const Task& rTask)
\r
388 throw(NullPointerException, MsgException);
\r
390 /*! \brief Process::sendTask() - Sends the given task in the mailbox identified by the default alias.
\r
392 * \param rTask A reference to the task object to send.
\r
394 * \exception If this method failed, it throws one of the exceptions described below:
\r
396 * [BadAllocException] if there is not enough memory to build the default alias.
\r
398 * [MsgException] if an internal exception occurs.
\r
401 void sendTask(const Task& rTask)
\r
402 throw(BadAllocException, MsgException);
\r
404 /*! \brief Process::sendTask() - Sends the given task in the mailbox identified by the default alias
\r
405 * (waiting at most given time).
\r
407 * \param rTask A reference to the task object to send.
\r
408 * \param timeout The timeout value.
\r
410 * \exception If this method failed, it throws one of the exceptions described below:
\r
412 * [BadAllocException] if there is not enough memory to build the default alias.
\r
414 * [InvalidArgumentException] if the timeout value is negative and different than -1.0.
\r
416 * [MsgException] if an internal exception occurs.
\r
418 * \remark set the timeout value with -1.0 to disable it.
\r
420 void sendTask(const Task& rTask, double timeout)
\r
421 throw(BadAllocException, InvalidArgumentException, MsgException);
\r
423 /*! \brief Process::receiveTask() - Retrieves a task from the mailbox identified by the alias specified as
\r
426 * \param alias The alias of the mailbox where to retrieve the task.
\r
428 * \return If succcessful, the method returns a task of the mailbox. Otherwise, the method
\r
429 * throws one of the exceptions described below:
\r
431 * \exception [NullPointerException] if the parameter alias is NULL.
\r
433 * [MsgException] if an internal exception occurs.
\r
435 Task& receiveTask(const char* alias)
\r
436 throw(NullPointerException, MsgException);
\r
438 /*! \brief Process::receiveTask() - Retrieves a task from the mailbox identified by the default alias.
\r
440 * \return If succcessful, the method returns a task of the mailbox. Otherwise, the method
\r
441 * throws one of the exceptions described below:
\r
443 * \exception [BadAllocException] if there is not enough memory to build the alias.
\r
444 * [MsgException] if an internal exception occurs.
\r
446 Task& receiveTask(void)
\r
447 throw(BadAllocException, MsgException);
\r
449 /*! \brief Process::receiveTask() - Retrieves a task from the mailbox identified by the alias specified as
\r
450 * parameter(waiting at most given time).
\r
452 * \param alias The alias of the mailbox where to retrieve the task.
\r
453 * \param timeout The timeout value.
\r
455 * \return If succcessful, the method returns a task of the mailbox. Otherwise, the method
\r
456 * throws one of the exceptions described below.
\r
458 * \exception [InvalidArgumentException] if the timeout value is negative or different of -1.0.
\r
460 * [NullPointerException] if the parameter alias is NULL.
\r
462 * [MsgException] if an internal exception occurs.
\r
464 Task& receiveTask(const char* alias, double timeout)
\r
465 throw(NullPointerException, InvalidArgumentException, MsgException);
\r
467 /*! \brief Process::receiveTask() - Retrieves a task from the mailbox identified by the default alias
\r
468 * (waiting at most given time).
\r
470 * \param timeout The timeout value.
\r
472 * \return If succcessful, the method returns a task of the mailbox. Otherwise, the method
\r
473 * throws one of the exceptions described below:
\r
475 * \exception [InvalidArgumentException] if the timeout value is negative or different than -1.0.
\r
477 * [BadAllocException] if there is not enough memory to build the alias.
\r
479 * [MsgException] if an internal exception occurs.
\r
481 Task& receiveTask(double timeout)
\r
482 throw(InvalidArgumentException, BadAllocException, MsgException);
\r
484 /*! \brief Process::receiveTask() - Retrieves a task from the mailbox identified by the alias specified as
\r
485 * parameter located on a given host (waiting at most given time).
\r
487 * \param alias The alias of the mailbox where to retrieve the task.
\r
488 * \param timeout The timeout value.
\r
489 * \param rHost A reference to the host object owning the mailbox.
\r
491 * \return If succcessful, the method returns a task of the mailbox. Otherwise, the method
\r
492 * throws one of the exceptions described below:
\r
494 * \exception [InvalidArgumentException] if the timeout value is negative or different of -1.0.
\r
496 * [NullPointerException] if the parameter alias is NULL.
\r
498 * [MsgException] if an internal exception occurs.
\r
500 Task& receiveTask(const char* alias, double timeout, const Host& rHost)
\r
501 throw(NullPointerException, InvalidArgumentException, MsgException);
\r
503 /*! \brief Process::receiveTask() - Retrieves a task from the mailbox identified by default alias
\r
504 * and located on a given host (waiting at most given time).
\r
506 * \param timeout The timeout value.
\r
507 * \param rHost A reference to the host object owning the mailbox.
\r
509 * \return If succcessful, the method returns a task of the mailbox. Otherwise, the method
\r
510 * throws one of the exceptions described below:
\r
512 * \exception [InvalidArgumentException] if the timeout value is negative and different than -1.0.
\r
514 * [BadAllocException] if there is not enough memory to build the default alias.
\r
516 * [MsgException] if an internal exception occurs.
\r
518 Task& receiveTask(double timeout, const Host& rHost)
\r
519 throw(BadAllocException, InvalidArgumentException, MsgException);
\r
521 /*! \brief Process::receiveTask() - Retrieves a task from the mailbox identified by the alias
\r
522 * specified as parameter and located on a given host.
\r
524 * \param alias The alias using to identify the mailbox from which to get the task.
\r
525 * \param rHost A reference to the host object owning the mailbox.
\r
527 * \return If succcessful, the method returns a task of the mailbox. Otherwise, the method
\r
528 * throws one of the exceptions described below:
\r
530 * \exception [NullPointerException] if the parameter alias is NULL.
\r
532 * [MsgException] if an internal exception occurs.
\r
534 Task& receiveTask(const char* alias, const Host& rHost)
\r
535 throw(NullPointerException, MsgException);
\r
537 /*! \brief Process::receiveTask() - Retrieves a task from the mailbox identified by default alias
\r
538 * and located on a given host.
\r
540 * \param rHost A reference to the host object owning the mailbox.
\r
542 * \return If succcessful, the method returns a task of the mailbox. Otherwise, the method
\r
543 * throws one of the exceptions described below:
\r
545 * \exception [BadAllocException] if there is not enough memory to build the alias.
\r
547 * [MsgException] if an internal exception occurs.
\r
549 Task& receiveTask(const Host& rHost)
\r
550 throw(BadAllocException, MsgException);
\r
555 /* Process::create() - Creates a process on a given host.
\r
557 * param rHost A reference to a host object where to create the process.
\r
558 * param name The name of the process to create.
\r
559 * param argc The number of argument to pass to the main function of the process.
\r
560 * param argv The list of the arguments of the main function of the process.
\r
562 * exception If this method failed, it throws one of the exceptions described below:
\r
564 * [HostNotFoundException] if the host specified as parameter doesn't exist.
\r
566 void create(const Host& rHost, const char* name, int argc, char** argv)
\r
567 throw(HostNotFoundException);
\r
569 /* Process::fromNativeProcess() - Retrieves the process wrapper associated with a native process.
\r
571 * \param nativeProcess The native process to get the wrapper.
\r
573 * \return The wrapper associated with the native process specified as parameter.
\r
575 static Process* fromNativeProcess(m_process_t nativeProcess);
\r
580 /* Process::run() - used to set the field code of the context of the process.
\r
582 static int run(int argc, char** argv);
\r
584 /*! \brief Process::main() - This virtual pure function is the main function of the process.
\r
585 * You must override this function for each new class of process that you create.
\r
587 * \param argc The number of parameters of the main function.
\r
588 * \param argv The list of the parameter of the main function.
\r
590 * \return The exit code of the main function.
\r
592 virtual int main(int argc, char** argv);
\r
598 m_process_t nativeProcess; // pointer to the native msg process.
\r
604 } // namespace SimGrid
\r
606 #endif // !MSG_PROCESS_HPP