Logo AND Algorithmique Numérique Distribuée

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