1 #include <Process.hpp>
\r
6 Process* Process::currentProcess = NULL;
\r
8 // Default constructor.
\r
11 this->nativeProcess = NULL;
\r
14 Process::Process(const char* hostname, const char* name)
\r
15 throw(HostNotFoundException)
\r
17 Host host = Host::getByName(hostname);
\r
19 create(host, name, 0, NULL);
\r
22 Process::Process(const char* hostname, const char* name, int argc, char** argv)
\r
23 throw(HostNotFoundException)
\r
25 Host host = Host::getByName(hostname);
\r
27 create(host, name, argc, argv);
\r
30 Process::Process(const Host& rHost, const char* name)
\r
31 throw(HostNotFoundException)
\r
34 create(rHost, name, 0, NULL);
\r
37 Process::Process(const Host& rHost, const char* name, int argc, char** argv)
\r
38 throw(HostNotFoundException)
\r
41 create(rHost, name, argc, argv);
\r
44 int Process::run(int argc, char** argv)
\r
46 Process* process =(Process*)argv[argc];
\r
48 return process->main(argc, argv);
\r
51 void Process::create(const Host& rHost, const char* name, int argc, char** argv)
\r
52 throw(HostNotFoundException)
\r
54 smx_process_t nativeCurrentProcess = NULL;
\r
55 nativeProcess = xbt_new0(s_smx_process_t, 1);
\r
56 smx_simdata_process_t simdata = xbt_new0(s_smx_simdata_process_t, 1);
\r
57 smx_host_t nativeHost = SIMIX_host_get_by_name(rHost.getName());
\r
59 argv = (char**)realloc(argc + 1, sizeo(char*));
\r
61 argv[argc] = (char*)this;
\r
64 // TODO throw HostNotFoundException if host is NULL
\r
67 simdata->smx_host = nativeHost;
\r
68 simdata->mutex = NULL;
\r
69 simdata->cond = NULL;
\r
70 simdata->argc = argc;
\r
71 simdata->argv = argv;
\r
72 simdata->context = xbt_context_new(name, Process::run, NULL, NULL, simix_global->cleanup_process_function, nativeProcess, simdata->argc, simdata->argv);
\r
74 /* Process structure */
\r
75 nativeProcess->name = xbt_strdup(name);
\r
76 nativeProcess->simdata = simdata;
\r
79 nativeProcess->data = NULL;
\r
81 // Set process properties
\r
82 simdata->properties = NULL;
\r
84 xbt_swag_insert(nativeProcess, nativeHost->simdata->process_list);
\r
86 /* fix current_process, about which xbt_context_start mocks around */
\r
87 nativeCurrentProcess = simix_global->current_process;
\r
88 xbt_context_start(nativeProcess->simdata->context);
\r
89 simix_global->current_process = nativeCurrentProcess;
\r
91 xbt_swag_insert(nativeProcess, simix_global->process_list);
\r
92 DEBUG2("Inserting %s(%s) in the to_run list", nativeProcess->name, nativeHost->name);
\r
93 xbt_swag_insert(nativeProcess, simix_global->process_to_run);
\r
97 int Process::killAll(int resetPID)
\r
99 return MSG_process_killall(resetPID);
\r
102 void Process::suspend(void)
\r
103 throw(NativeException)
\r
105 if(MSG_OK != MSG_process_suspend(nativeProcess))
\r
107 // TODO throw NativeException.
\r
111 void Process::resume(void)
\r
112 throw(NativeException)
\r
114 if(MSG_OK != MSG_process_resume(nativeProcess))
\r
116 // TODO throw NativeException.
\r
121 bool Process::isSuspended(void)
\r
123 return (bool)MSG_process_is_suspended(nativeProcess);
\r
127 Host& Process::getHost(void)
\r
128 throw(NativeException)
\r
130 m_host_t nativeHost = MSG_process_get_host(nativeProcess);
\r
132 if(!nativeHost->data)
\r
134 // TODO throw NativeException.
\r
138 // return the reference to the Host object
\r
139 return (*((Host*)nativeHost->data));
\r
142 Process& Process::fromPID(int PID)
\r
143 throw(ProcessNotFoundException, NativeException)
\r
145 Process* process = NULL;
\r
146 m_process_t nativeProcess = MSG_process_from_PID(PID);
\r
151 throw ProcessNotFoundException;
\r
155 process = Process::fromNativeProcess(nativeProcess);
\r
159 // TODO throw NativeException
\r
163 return (*process);
\r
166 // TODO implement this method
\r
167 Process& Process::fromNativeProcess(m_process_t nativeProcess)
\r
172 int Process::getPID(void)
\r
174 return MSG_process_get_PID(nativeProcess);
\r
177 int Process::getPPID(void)
\r
179 return MSG_process_get_PPID(nativeProcess);
\r
183 Process& Process::currentProcess(void)
\r
184 throw(NativeException)
\r
186 Process* currentProcess = NULL;
\r
187 m_process_t currentNativeProcess = MSG_process_self();
\r
190 if(!currentNativeProcess)
\r
192 // TODO throw NativeException
\r
195 currentProcess = Process::fromNativeProcess(currentNativeProcess);
\r
197 if(!currentProcess)
\r
199 // TODO throw NativeException
\r
203 return (*currentProcess);
\r
206 int Process::currentProcessPID(void)
\r
208 return MSG_process_self_PID();
\r
212 int Process::currentProcessPPID(void)
\r
214 return MSG_process_self_PPID();
\r
217 void Process::migrate(const Host& rHost)
\r
218 throw(NativeException)
\r
220 if(MSG_OK != MSG_process_change_host(nativeProcess, rHost.nativeHost))
\r
222 // TODO throw NativeException
\r
227 void Process::sleep(double seconds)
\r
228 throw(NativeException)
\r
230 if(MSG_OK != MSG_process_sleep(seconds))
\r
232 // TODO throw NativeException
\r
237 void Process::putTask(const Host& rHost, int channel, const Task& rTask)
\r
238 throw( NativeException)
\r
240 if(MSG_OK != MSG_task_put_with_timeout(rTask.nativeTask, rHost.nativeHost, channel, -1.0))
\r
242 // TODO throw NativeException
\r
246 void Process::putTask(const Host& rHost, int channel, const Task& rTask, double timeout)
\r
247 throw(NativeException)
\r
249 if(MSG_OK != MSG_task_put_with_timeout(rTask.nativeTask, rHost.nativeHost, channel, timeout))
\r
251 // TODO throw NativeException
\r
255 Task& Process::getTask(int channel)
\r
256 throw(NativeException)
\r
258 m_task_t nativeTask = NULL;
\r
261 if (MSG_OK != MSG_task_get_ext(&nativeTask, channel, -1.0, NULL))
\r
263 // TODO throw NativeException
\r
267 return (*((Task*)(nativeTask->data)));
\r
270 Task& Process::getTask(int channel, double timeout)
\r
271 throw(NativeException)
\r
273 m_task_t nativeTask = NULL;
\r
276 if (MSG_OK != MSG_task_get_ext(&nativeTask, channel, timeout, NULL))
\r
278 // TODO throw NativeException
\r
282 return (*((Task*)(nativeTask->data)));
\r
285 Task& Process::getTask(int channel, const Host& rHost)
\r
286 throw(NativeException)
\r
288 m_task_t nativeTask = NULL;
\r
291 if (MSG_OK != MSG_task_get_ext(&nativeTask, channel, -1.0, rHost.nativeHost))
\r
293 // TODO throw NativeException
\r
297 return (*((Task*)(nativeTask->data)));
\r
300 Task& Process::getTask(int channel, double timeout, const Host& rHost)
\r
301 throw(NativeException)
\r
303 m_task_t nativeTask = NULL;
\r
306 if (MSG_OK != MSG_task_get_ext(&nativeTask, channel, timeout, rHost.nativeHost))
\r
308 // TODO throw NativeException
\r
312 return (*((Task*)(nativeTask->data)));
\r
315 void Process::sendTask(const char* alias, const Task& rTask, double timeout)
\r
316 throw(NativeException)
\r
318 if(MSG_OK != MSG_task_send_with_timeout(rTask.nativeTask, alias ,timeout))
\r
320 // TODO throw NativeException
\r
325 void Process::sendTask(const char* alias, const Task& rTask)
\r
326 throw(NativeException)
\r
328 if(MSG_OK != MSG_task_send_with_timeout(rTask.nativeTask, alias ,-1.0))
\r
330 // TODO throw NativeException
\r
334 void Process::sendTask(const Task& rTask)
\r
335 throw(NativeException)
\r
339 char* alias = (char*)calloc(strlen(Host::currentHost().getName() + strlen(nativeProcess->name) + 2);
\r
340 sprintf(alias,"%s:%s", Host::currentHost().getName() ,nativeProcess->name);
\r
342 rv = MSG_task_send_with_timeout(rTask.nativeTask, alias ,-1.0);
\r
348 // TODO throw NativeException
\r
352 void Process::sendTask(const Task& rTask, double timeout)
\r
353 throw(NativeException)
\r
357 char* alias = (char*)calloc(strlen(Host::currentHost().getName() + strlen(nativeProcess->name) + 2);
\r
358 sprintf(alias,"%s:%s", Host::currentHost().getName() ,nativeProcess->name);
\r
360 rv = MSG_task_send_with_timeout(rTask.nativeTask, alias ,timeout);
\r
366 // TODO throw NativeException
\r
370 Task& Process::receiveTask(const char* alias)
\r
371 throw(NativeException)
\r
374 m_task_t nativeTask = NULL;
\r
376 if (MSG_OK != MSG_task_receive_ext(&nativeTask,alias, -1.0, NULL))
\r
378 // TODO throw NativeException
\r
382 return (*((Task*)nativeTask->data));
\r
386 Task& Process::receiveTask(void)
\r
387 throw(NativeException)
\r
389 m_task_t nativeTask = NULL;
\r
391 char* alias = (char*)calloc(strlen(Host::currentHost().getName() + strlen(nativeProcess->name) + 2);
\r
392 sprintf(alias,"%s:%s", Host::currentHost().getName() ,nativeProcess->name);
\r
394 MSG_error_t rv = MSG_task_receive_ext(&nativeTask, alias, -1.0, NULL);
\r
400 //TODO throw NativeException
\r
404 return (*((Task*)nativeTask->data));
\r
408 Task& Process::receiveTask(const char* alias, double timeout)
\r
409 throw(NativeException)
\r
411 m_task_t nativeTask = NULL;
\r
413 if(MSG_OK != MSG_task_receive_ext(&nativeTask, alias, timeout, NULL))
\r
415 //TODO throw NativeException
\r
419 return (*((Task*)nativeTask->data));
\r
423 Task& Process::receiveTask(double timeout)
\r
424 throw(NativeException)
\r
426 m_task_t nativeTask = NULL;
\r
428 char* alias = (char*)calloc(strlen(Host::currentHost().getName() + strlen(nativeProcess->name) + 2);
\r
429 sprintf(alias,"%s:%s", Host::currentHost().getName() ,nativeProcess->name);
\r
431 MSG_error_t rv = MSG_task_receive_ext(&nativeTask, alias, timeout, NULL);
\r
437 //TODO throw NativeException
\r
441 return (*((Task*)nativeTask->data));
\r
445 Task& Process::receiveTask(const char* alias, double timeout, const Host& rHost)
\r
446 throw(NativeException)
\r
448 m_task_t nativeTask = NULL;
\r
450 if(MSG_OK != MSG_task_receive_ext(&nativeTask, alias, timeout, rHost.nativeHost))
\r
452 //TODO throw NativeException
\r
456 return (*((Task*)nativeTask->data));
\r
460 Task& Process::receiveTask(double timeout, const Host& rHost)
\r
461 throw(NativeException)
\r
463 m_task_t nativeTask = NULL;
\r
465 char* alias = (char*)calloc(strlen(Host::currentHost().getName() + strlen(nativeProcess->name) + 2);
\r
466 sprintf(alias,"%s:%s", Host::currentHost().getName() ,nativeProcess->name);
\r
468 MSG_error_t rv = MSG_task_receive_ext(&nativeTask, alias, timeout, rHost.nativeHost);
\r
474 //TODO throw NativeException
\r
478 return (*((Task*)nativeTask->data));
\r
482 Task& Process::receiveTask(const char* alias, const Host& rHost)
\r
483 throw(NativeException)
\r
486 m_task_t nativeTask = NULL;
\r
489 if(MSG_OK != MSG_task_receive_ext(&nativeTask, alias, -1.0, rHost.nativeHost))
\r
491 //TODO throw NativeException
\r
495 return (*((Task*)nativeTask->data));
\r
498 Task& Process::receiveTask(const Host& rHost)
\r
499 throw(NativeException)
\r
501 m_task_t nativeTask = NULL;
\r
503 char* alias = (char*)calloc(strlen(Host::currentHost().getName() + strlen(nativeProcess->name) + 2);
\r
504 sprintf(alias,"%s:%s", Host::currentHost().getName() ,nativeProcess->name);
\r
506 MSG_error_t rv = MSG_task_receive_ext(&nativeTask, alias, -1.0, rHost.nativeHost);
\r
512 //TODO throw NativeException
\r
516 return (*((Task*)nativeTask->data));
\r
519 const char* Process::getName(void) const
\r
521 return nativeProcess->name;
\r