Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
bfe5c49f72fcdb244e061863ae1e3912d2e04a0c
[simgrid.git] / include / simgrid / s4u / process.hpp
1 /* Copyright (c) 2006-2015. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #ifndef SIMGRID_S4U_PROCESS_HPP
7 #define SIMGRID_S4U_PROCESS_HPP
8
9 #include "simgrid/simix.h"
10
11 namespace simgrid {
12 namespace s4u {
13
14 class Host;
15
16 /** @brief Simulation Agent
17  *
18  * A process may be defined as a code executing in a location (host).
19  *
20  * All processes should be started from the XML deployment file (using the @link{s4u::Engine::loadDeployment()}),
21  * even if you can also start new processes directly.
22  * Separating the deployment in the XML from the logic in the code is a good habit as it makes your simulation easier
23  * to adapt to new settings.
24  *
25  * The code that you define for a given process should be placed in the main method that is virtual.
26  * For example, a Worker process should be declared as follows:
27  *
28  * \verbatim
29  * #include "s4u/process.hpp"
30  *
31  * class Worker : simgrid::s4u::Process {
32  *
33  *        int main(int argc, char **argv) {
34  *              printf("Hello s4u");
35  *        }
36  * };
37  * \endverbatim
38  *
39  */
40 class Process {
41 public:
42         Process(const char*procname, s4u::Host *host, int argc, char **argv);
43         Process(const char*procname, s4u::Host *host, int argc, char **argv, double killTime);
44         virtual ~Process() {}
45
46         /** The main method of your agent */
47         virtual int main(int argc, char **argv)=0;
48
49         /** The process that is currently running */
50         static Process *current();
51         /** Retrieves the process that have the given PID (or NULL if not existing) */
52         static Process *byPid(int pid);
53
54         /** Retrieves the name of that process */
55         const char*getName();
56         /** Retrieves the host on which that process is running */
57         s4u::Host *getHost();
58         /** Retrieves the PID of that process */
59         int getPid();
60
61         /** If set to true, the process will automatically restart when its host reboots */
62         void setAutoRestart(bool autorestart);
63         /** Sets the time at which that process should be killed */
64         void setKillTime(double time);
65         /** Retrieves the time at which that process will be killed (or -1 if not set) */
66         double getKillTime();
67
68         /** Ask kindly to all processes to die. Only the issuer will survive. */
69         static void killAll();
70         /** Ask the process to die.
71          *
72          * It will only notice your request when doing a simcall next time (a communication or similar).
73          * SimGrid sometimes have issues when you kill processes that are currently communicating and such. We are working on it to fix the issues.
74          */
75         void kill();
76
77         /** Block the process sleeping for that amount of seconds (may throws hostFailure) */
78         void sleep(double duration);
79
80         /** Block the process, computing the given amount of flops */
81         void execute(double flop);
82
83         /** Block the process until it gets a message from the given mailbox */
84         //void* recv(const char *mailbox);
85
86         /** Block the process until it gets a string message (to be freed after use) from the given mailbox */
87         char *recvstr(const char* mailbox);
88
89         /** Block the process until it delivers a string message (that will be copied) to the given mailbox */
90         void sendstr(const char*mailbox, const char*msg);
91
92 protected:
93         smx_process_t getInferior() {return p_smx_process;}
94 private:
95         smx_process_t p_smx_process;
96 };
97 }} // namespace simgrid::s4u
98
99 #endif /* SIMGRID_S4U_PROCESS_HPP */
100
101 #if 0
102
103 public abstract class Process implements Runnable {
104         /** Suspends the process. See {@link #resume()} to resume it afterward */
105         public native void suspend();
106         /** Resume a process that was suspended by {@link #suspend()}. */
107         public native void resume();    
108         /** Tests if a process is suspended. */
109         public native boolean isSuspended();
110         
111         /**
112          * Returns the value of a given process property. 
113          */
114         public native String getProperty(String name);
115
116
117         /**
118          * Migrates a process to another host.
119          *
120          * @param host                  The host where to migrate the process.
121          *
122          */
123         public native void migrate(Host host);  
124
125         public native void exit();    
126         /**
127          * This static method returns the current amount of processes running
128          *
129          * @return                      The count of the running processes
130          */ 
131         public native static int getCount();
132
133 }
134 #endif