1 #include <Process.hpp>
\r
7 Process* Process::currentProcess = NULL;
\r
9 // Default constructor.
\r
12 this->nativeProcess = NULL;
\r
15 Process::Process(const char* hostName, const char* name)
\r
16 throw(InvalidArgumentException, HostNotFoundException)
\r
18 // check the parameters
\r
21 throw NullPointerException("name");
\r
24 throw NullPointerException("hostName");
\r
26 Host host = Host::getByName(hostName);
\r
28 create(host, name, 0, NULL);
\r
31 Process::Process(const Host& rHost, const char* name)
\r
32 throw(NullPointerException)
\r
35 throw NullPointerException("name");
\r
37 create(rHost, name, 0, NULL);
\r
40 Process::Process(const Host& rHost, const char* name, int argc, char** argv)
\r
41 throw(NullPointerException, InvalidArgumentException, LogicException)
\r
44 // check the parameters
\r
47 throw NullPointerException("name");
\r
50 throw NullPointerException("hostName");
\r
53 throw InvalidArgument("argc (must be positive)");
\r
56 throw LogicException("argv is not NULL but argc is zero");
\r
59 throw LogicException("argv is NULL but argc is not zero");
\r
61 create(rHost, name, argc, argv);
\r
64 Process::Process(const char* hostname, const char* name, int argc, char** argv)
\r
65 throw(NullPointerException, InvalidArgumentException, LogicException, HostNotFoundException)
\r
67 // check the parameters
\r
70 throw NullPointerException("name");
\r
73 throw NullPointerException("hostName");
\r
76 throw InvalidArgument("argc (must be positive)");
\r
79 throw LogicException("argv is not NULL but argc is zero");
\r
82 throw LogicException("argv is NULL but argc is not zero");
\r
84 Host host = Host::getByName(hostname);
\r
86 create(host, name, argc, argv);
\r
89 int Process::killAll(int resetPID)
\r
91 return MSG_process_killall(resetPID);
\r
94 void Process::suspend(void)
\r
97 if(MSG_OK != MSG_process_suspend(nativeProcess))
\r
98 throw MsgException("MSG_process_suspend() failed");
\r
101 void Process::resume(void)
\r
102 throw(MsgException)
\r
104 if(MSG_OK != MSG_process_resume(nativeProcess))
\r
105 throw MsgException("MSG_process_resume() failed");
\r
108 bool Process::isSuspended(void)
\r
110 return (bool)MSG_process_is_suspended(nativeProcess);
\r
113 Host& Process::getHost(void)
\r
115 m_host_t nativeHost = MSG_process_get_host(nativeProcess);
\r
117 // return the reference to the Host object
\r
118 return (*((Host*)nativeHost->data));
\r
121 Process& Process::fromPID(int PID)
\r
122 throw(ProcessNotFoundException, InvalidArgumentException, MsgException)
\r
124 // check the parameters
\r
127 throw InvalidArgumentException("PID (the PID of the process to retrieve is not less than 1)");
\r
129 Process* process = NULL;
\r
130 m_process_t nativeProcess = MSG_process_from_PID(PID);
\r
132 if(!nativeProcess)
\r
133 throw ProcessNotFoundException(PID);
\r
135 process = Process::fromNativeProcess(nativeProcess);
\r
138 throw MsgException("Process::fromNativeProcess() failed");
\r
140 return (*process);
\r
143 int Process::getPID(void)
\r
145 return MSG_process_get_PID(nativeProcess);
\r
148 int Process::getPPID(void)
\r
150 return MSG_process_get_PPID(nativeProcess);
\r
153 const char* Process::getName(void) const
\r
155 return nativeProcess->name;
\r
158 Process& Process::currentProcess(void)
\r
159 throw(MsgException)
\r
161 Process* currentProcess = NULL;
\r
162 m_process_t currentNativeProcess = MSG_process_self();
\r
165 if(!currentNativeProcess)
\r
166 throw MsgException("MSG_process_self() failed");
\r
168 currentProcess = Process::fromNativeProcess(currentNativeProcess);
\r
170 if(!currentProcess)
\r
171 throw MsgException("Process::fromNativeProcess() failed");
\r
173 return (*currentProcess);
\r
176 int Process::currentProcessPID(void)
\r
178 return MSG_process_self_PID();
\r
182 int Process::currentProcessPPID(void)
\r
184 return MSG_process_self_PPID();
\r
187 void Process::migrate(const Host& rHost)
\r
188 throw(MsgException)
\r
190 if(MSG_OK != MSG_process_change_host(nativeProcess, rHost.nativeHost))
\r
191 throw MsgException("MSG_process_change_host()");
\r
195 void Process::sleep(double seconds)
\r
196 throw(throw(InvalidArgumentException, MsgException))
\r
198 // check the parameters.
\r
200 throw InvalidArgumentException("seconds (must not be less or equals to zero");
\r
202 if(MSG_OK != MSG_process_sleep(seconds))
\r
203 throw MsgException("MSG_process_sleep()");
\r
207 void Process::putTask(const Host& rHost, int channel, const Task& rTask)
\r
208 throw(InvalidArgumentException, MsgException)
\r
210 // check the parameters
\r
213 throw InvalidArgumentException("channel (must not be negative)");
\r
215 if(MSG_OK != MSG_task_put_with_timeout(rTask.nativeTask, rHost.nativeHost, channel, -1.0))
\r
216 throw MsgException("MSG_task_put_with_timeout()");
\r
219 void Process::putTask(const Host& rHost, int channel, const Task& rTask, double timeout)
\r
220 throw(InvalidArgumentException, MsgException)
\r
222 // check the parameters
\r
224 throw InvalidArgumentException("channel (must not be negative)");
\r
226 if(timeout < 0 && timeout != -1.0)
\r
227 throw InvalidArgumentException("timeout (must not be less than zero an different of -1.0)");
\r
229 if(MSG_OK != MSG_task_put_with_timeout(rTask.nativeTask, rHost.nativeHost, channel, timeout))
\r
230 throw MsgException("MSG_task_put_with_timeout() failed");
\r
233 Task& Process::getTask(int channel)
\r
234 throw(InvalidArgumentException, MsgException)
\r
236 // check the parameters
\r
239 throw InvalidArgumentException("channel (must not be negative)");
\r
241 m_task_t nativeTask = NULL;
\r
243 if (MSG_OK != MSG_task_get_ext(&nativeTask, channel, -1.0, NULL))
\r
244 throw MsgException("MSG_task_get_ext() failed");
\r
246 return (*((Task*)(nativeTask->data)));
\r
249 Task& Process::getTask(int channel, double timeout)
\r
250 throw(InvalidArgumentException, MsgException)
\r
252 // check the parameters
\r
254 throw InvalidArgumentException("channel (must not be negative)");
\r
256 if(timeout < 0 && timeout != -1.0)
\r
257 throw InvalidArgumentException("timeout (must not be less than zero an different of -1.0)");
\r
259 m_task_t nativeTask = NULL;
\r
261 if (MSG_OK != MSG_task_get_ext(&nativeTask, channel, timeout, NULL))
\r
262 throw MsgException("MSG_task_get_ext() failed");
\r
264 return (*((Task*)(nativeTask->data)));
\r
267 Task& Process::getTask(int channel, const Host& rHost)
\r
268 throw(InvalidArgumentException, MsgException)
\r
270 // check the parameters
\r
272 throw InvalidArgumentException("channel (must not be negative)");
\r
274 m_task_t nativeTask = NULL;
\r
276 if (MSG_OK != MSG_task_get_ext(&nativeTask, channel, -1.0, rHost.nativeHost))
\r
277 throw MsgException("MSG_task_get_ext() failed");
\r
279 return (*((Task*)(nativeTask->data)));
\r
282 Task& Process::getTask(int channel, double timeout, const Host& rHost)
\r
283 throw(InvalidArgumentException, MsgException)
\r
285 // check the parameters
\r
287 throw InvalidArgumentException("channel (must not be negative)");
\r
289 if(timeout < 0 && timeout != -1.0)
\r
290 throw InvalidArgumentException("timeout (must not be less than zero an different of -1.0)");
\r
292 m_task_t nativeTask = NULL;
\r
294 if (MSG_OK != MSG_task_get_ext(&nativeTask, channel, timeout, rHost.nativeHost))
\r
295 throw MsgException("MSG_task_get_ext() failed");
\r
297 return (*((Task*)(nativeTask->data)));
\r
300 void Process::sendTask(const char* alias, const Task& rTask, double timeout)
\r
301 throw(NullPointerException, InvalidArgumentException, MsgException)
\r
303 // check the parameters
\r
306 throw NullPointerException("alias");
\r
308 if(timeout < 0 && timeout !=-1.0)
\r
309 throw InvalidArgumentException("timeout (the timeout value must not be negative and different than -1.0)");
\r
311 if(MSG_OK != MSG_task_send_with_timeout(rTask.nativeTask, alias ,timeout))
\r
312 throw MsgException("MSG_task_send_with_timeout()");
\r
316 void Process::sendTask(const char* alias, const Task& rTask)
\r
317 throw(NullPointerException, MsgException)
\r
319 // check the parameters
\r
322 throw NullPointerException("alias");
\r
324 if(MSG_OK != MSG_task_send_with_timeout(rTask.nativeTask, alias ,-1.0))
\r
325 throw MsgException("MSG_task_send_with_timeout()");
\r
328 void Process::sendTask(const Task& rTask)
\r
329 throw(BadAllocException, MsgException)
\r
331 char* alias = (char*)calloc(strlen(Host::currentHost().getName() + strlen(nativeProcess->name) + 2);
\r
334 throw BadAllocException("alias");
\r
336 sprintf(alias,"%s:%s", Host::currentHost().getName() ,nativeProcess->name);
\r
338 MSG_error_t rv = MSG_task_send_with_timeout(rTask.nativeTask, alias ,-1.0);
\r
343 throw MsgException("MSG_task_send_with_timeout()");
\r
346 void Process::sendTask(const Task& rTask, double timeout)
\r
347 throw(BadAllocException, InvalidArgumentException, MsgException)
\r
349 // check the parameters
\r
351 if(timeout < 0 && timeout !=-1.0)
\r
352 throw InvalidArgumentException("timeout (the timeout value must not be negative and different than -1.0)");
\r
354 char* alias = (char*)calloc(strlen(Host::currentHost().getName() + strlen(nativeProcess->name) + 2);
\r
357 throw BadAllocException("alias");
\r
359 sprintf(alias,"%s:%s", Host::currentHost().getName() ,nativeProcess->name);
\r
361 MSG_error_t rv = MSG_task_send_with_timeout(rTask.nativeTask, alias ,timeout);
\r
366 throw MsgException("MSG_task_send_with_timeout()");
\r
369 Task& Process::receiveTask(const char* alias)
\r
370 throw(NullPointerException, MsgException)
\r
372 // check the parameters
\r
375 throw NullPointerException(alias);
\r
377 m_task_t nativeTask = NULL;
\r
379 if (MSG_OK != MSG_task_receive_ext(&nativeTask,alias, -1.0, NULL))
\r
380 throw MsgException("MSG_task_receive_ext() failed");
\r
382 return (*((Task*)nativeTask->data));
\r
386 Task& Process::receiveTask(void)
\r
387 throw(BadAllocException, MsgException)
\r
390 char* alias = (char*)calloc(strlen(Host::currentHost().getName() + strlen(nativeProcess->name) + 2);
\r
393 throw BadAllocException("alias");
\r
395 sprintf(alias,"%s:%s", Host::currentHost().getName() ,nativeProcess->name);
\r
397 m_task_t nativeTask = NULL;
\r
399 MSG_error_t rv = MSG_task_receive_ext(&nativeTask, alias, -1.0, NULL);
\r
404 throw MsgException("MSG_task_receive_ext() failed");
\r
406 return (*((Task*)nativeTask->data));
\r
410 Task& Process::receiveTask(const char* alias, double timeout)
\r
411 throw(NullPointerException, InvalidArgumentException, MsgException)
\r
413 // check the parameters
\r
416 throw NullPointerException("alias");
\r
418 if(timeout < 0 && timeout !=-1.0)
\r
419 throw InvalidArgumentException("timeout (the timeout value must not be negative and different than -1.0)");
\r
421 m_task_t nativeTask = NULL;
\r
423 if(MSG_OK != MSG_task_receive_ext(&nativeTask, alias, timeout, NULL))
\r
424 throw MsgException("MSG_task_receive_ext() failed");
\r
426 return (*((Task*)nativeTask->data));
\r
430 Task& Process::receiveTask(double timeout)
\r
431 throw(InvalidArgumentException, BadAllocException, MsgException)
\r
433 // check the parameters
\r
435 if(timeout < 0 && timeout !=-1.0)
\r
436 throw InvalidArgumentException("timeout (the timeout value must not be negative and different than -1.0)");
\r
439 char* alias = (char*)calloc(strlen(Host::currentHost().getName() + strlen(nativeProcess->name) + 2);
\r
442 throw BadAllocException("alias");
\r
444 sprintf(alias,"%s:%s", Host::currentHost().getName() ,nativeProcess->name);
\r
446 m_task_t nativeTask = NULL;
\r
448 MSG_error_t rv = MSG_task_receive_ext(&nativeTask, alias, timeout, NULL);
\r
453 throw MsgException("MSG_task_receive_ext() failed");
\r
455 return (*((Task*)nativeTask->data));
\r
459 Task& Process::receiveTask(const char* alias, double timeout, const Host& rHost)
\r
460 throw(NullPointerException, InvalidArgumentException, MsgException)
\r
462 // check the parameters
\r
465 throw NullPointerException("alias");
\r
467 if(timeout < 0 && timeout !=-1.0)
\r
468 throw InvalidArgumentException("timeout (the timeout value must not be negative and different than -1.0)");
\r
470 m_task_t nativeTask = NULL;
\r
472 if(MSG_OK != MSG_task_receive_ext(&nativeTask, alias, timeout, rHost.nativeHost))
\r
473 throw MsgException("MSG_task_receive_ext() failed");
\r
475 return (*((Task*)nativeTask->data));
\r
479 Task& Process::receiveTask(double timeout, const Host& rHost)
\r
480 throw(BadAllocException, InvalidArgumentException, MsgException)
\r
482 // check the parameters
\r
484 if(timeout < 0 && timeout !=-1.0)
\r
485 throw InvalidArgumentException("timeout (the timeout value must not be negative and different than -1.0)");
\r
487 char* alias = (char*)calloc(strlen(Host::currentHost().getName() + strlen(nativeProcess->name) + 2);
\r
490 throw BadAllocException("alias");
\r
492 sprintf(alias,"%s:%s", Host::currentHost().getName() ,nativeProcess->name);
\r
494 m_task_t nativeTask = NULL;
\r
496 MSG_error_t rv = MSG_task_receive_ext(&nativeTask, alias, timeout, rHost.nativeHost);
\r
501 throw MsgException("MSG_task_receive_ext() failed");
\r
503 return (*((Task*)nativeTask->data));
\r
507 Task& Process::receiveTask(const char* alias, const Host& rHost)
\r
508 throw(NullPointerException, MsgException)
\r
511 // check the parameters
\r
514 throw NullPointerException("alias");
\r
516 m_task_t nativeTask = NULL;
\r
518 if(MSG_OK != MSG_task_receive_ext(&nativeTask, alias, -1.0, rHost.nativeHost))
\r
519 throw MsgException("MSG_task_receive_ext() failed");
\r
521 return (*((Task*)nativeTask->data));
\r
524 Task& Process::receiveTask(const Host& rHost)
\r
525 throw(BadAllocException, MsgException)
\r
527 char* alias = (char*)calloc(strlen(Host::currentHost().getName() + strlen(nativeProcess->name) + 2);
\r
530 throw BadAllocException("alias");
\r
532 sprintf(alias,"%s:%s", Host::currentHost().getName() ,nativeProcess->name);
\r
534 m_task_t nativeTask = NULL;
\r
536 MSG_error_t rv = MSG_task_receive_ext(&nativeTask, alias, -1.0, rHost.nativeHost);
\r
541 throw MsgException("MSG_task_receive_ext() failed");
\r
543 return (*((Task*)nativeTask->data));
\r
546 void Process::create(const Host& rHost, const char* name, int argc, char** argv)
\r
547 throw(HostNotFoundException)
\r
549 smx_process_t nativeCurrentProcess = NULL;
\r
550 nativeProcess = xbt_new0(s_smx_process_t, 1);
\r
551 smx_simdata_process_t simdata = xbt_new0(s_smx_simdata_process_t, 1);
\r
552 smx_host_t nativeHost = SIMIX_host_get_by_name(rHost.getName());
\r
554 throw HostNotFoundException(rHost.getName());
\r
556 argv = (char**)realloc(argc + 1, sizeo(char*));
\r
558 argv[argc] = (char*)this;
\r
561 simdata->smx_host = nativeHost;
\r
562 simdata->mutex = NULL;
\r
563 simdata->cond = NULL;
\r
564 simdata->argc = argc;
\r
565 simdata->argv = argv;
\r
566 simdata->context = xbt_context_new(name, Process::run, NULL, NULL, simix_global->cleanup_process_function, nativeProcess, simdata->argc, simdata->argv);
\r
568 /* Process structure */
\r
569 nativeProcess->name = xbt_strdup(name);
\r
570 nativeProcess->simdata = simdata;
\r
572 // Set process data
\r
573 nativeProcess->data = NULL;
\r
575 // Set process properties
\r
576 simdata->properties = NULL;
\r
578 xbt_swag_insert(nativeProcess, nativeHost->simdata->process_list);
\r
580 /* fix current_process, about which xbt_context_start mocks around */
\r
581 nativeCurrentProcess = simix_global->current_process;
\r
582 xbt_context_start(nativeProcess->simdata->context);
\r
583 simix_global->current_process = nativeCurrentProcess;
\r
585 xbt_swag_insert(nativeProcess, simix_global->process_list);
\r
586 DEBUG2("Inserting %s(%s) in the to_run list", nativeProcess->name, nativeHost->name);
\r
587 xbt_swag_insert(nativeProcess, simix_global->process_to_run);
\r
590 Process& Process::fromNativeProcess(m_process_t nativeProcess)
\r
592 return (*((Process*)(nativeProcess->simdata->arg[nativeProcess->argc])));
\r
595 int Process::run(int argc, char** argv)
\r
597 Process* process =(Process*)argv[argc];
\r
599 return process->main(argc, argv);
\r
604 } // namespace SimGrid