1 /* Copyright (c) 2006-2015. The SimGrid Team. All rights reserved. */
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. */
6 #ifndef SIMGRID_S4U_ACTOR_HPP
7 #define SIMGRID_S4U_ACTOR_HPP
9 #include "simgrid/simix.h"
17 /** @brief Simulation Agent
19 * An actor may be defined as a code executing in a location (host).
21 * All actors should be started from the XML deployment file (using the @link{s4u::Engine::loadDeployment()}),
22 * even if you can also start new actors 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.
26 * The code that you define for a given actor should be placed in the main method that is virtual.
27 * For example, a Worker actor should be declared as follows:
30 * #include "s4u/actor.hpp"
32 * class Worker : simgrid::s4u::Actor {
34 * int main(int argc, char **argv) {
35 * printf("Hello s4u");
43 Actor(const char*name, s4u::Host *host, int argc, char **argv);
44 Actor(const char*name, s4u::Host *host, int argc, char **argv, double killTime);
47 /** The main method of your agent */
48 virtual int main(int argc, char **argv)=0;
50 /** The Actor that is currently running */
51 static Actor *current();
52 /** Retrieves the actor that have the given PID (or NULL if not existing) */
53 static Actor *byPid(int pid);
55 /** Retrieves the name of that actor */
57 /** Retrieves the host on which that actor is running */
59 /** Retrieves the PID of that actor */
62 /** If set to true, the actor will automatically restart when its host reboots */
63 void setAutoRestart(bool autorestart);
64 /** Sets the time at which that actor should be killed */
65 void setKillTime(double time);
66 /** Retrieves the time at which that actor will be killed (or -1 if not set) */
69 /** Ask kindly to all actors to die. Only the issuer will survive. */
70 static void killAll();
71 /** Ask the actor to die.
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 actors that are currently communicating and such. We are working on it to fix the issues.
78 /** Block the actor sleeping for that amount of seconds (may throws hostFailure) */
79 void sleep(double duration);
81 /** Block the actor, computing the given amount of flops */
82 void execute(double flop);
84 /** Block the actor until it gets a message from the given mailbox */
85 //void* recv(const char *mailbox);
87 /** Block the actor until it gets a string message (to be freed after use) from the given mailbox */
88 char *recvstr(Mailbox &chan);
90 /** Block the actor until it delivers a string message (that will be copied) to the given mailbox */
91 void sendstr(Mailbox &chan, const char*msg);
94 smx_process_t getInferior() {return p_smx_process;}
96 smx_process_t p_smx_process;
98 }} // namespace simgrid::s4u
100 #endif /* SIMGRID_S4U_ACTOR_HPP */
104 public abstract class Actor 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();
113 * Returns the value of a given process property.
115 public native String getProperty(String name);
119 * Migrates a process to another host.
121 * @param host The host where to migrate the process.
124 public native void migrate(Host host);
126 public native void exit();
128 * This static method returns the current amount of processes running
130 * @return The count of the running processes
132 public native static int getCount();