1 /* Copyright (c) 2006-2021. 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 /* This example shows how to declare and start your actors.
8 * The first step is to declare the code of your actors (what they do exactly does not matter to this example) and then
9 * you ask SimGrid to start your actors. There is three ways of doing so:
10 * - Directly, by instantiating your actor as parameter to Actor::create()
11 * - By first registering your actors before instantiating it;
12 * - Through the deployment file.
14 * This example shows all these solutions, even if you obviously should use only one of these solutions to start your
15 * actors. The most advised solution is to use a deployment file, as it creates a clear separation between your
16 * application and the settings to test it. This is a better scientific methodology. Actually, starting an actor with
17 * Actor::create() is mostly useful to start an actor from another actor.
20 #include <simgrid/actor.h>
21 #include <simgrid/engine.h>
22 #include <simgrid/host.h>
23 #include <simgrid/mailbox.h>
24 #include <xbt/asserts.h>
27 // This declares a logging channel so that XBT_INFO can be used later
28 XBT_LOG_NEW_DEFAULT_CATEGORY(actor_create, "The logging channel used in this example");
30 /* Our first class of actors is simply implemented with a function.
31 * As every CSG actors, its parameters are in a vector of string (argc/argv), and it does not return anything.
33 * One 'receiver' actor is instantiated within the simulation later in this file.
35 static void receiver(int argc, char** argv)
37 xbt_assert(argc == 2, "This actor expects a single argument: the mailbox on which to get messages");
38 sg_mailbox_t mailbox = sg_mailbox_by_name(argv[1]);
40 XBT_INFO("Hello, I'm ready to get any message you'd want on %s", argv[1]);
42 char* msg1 = sg_mailbox_get(mailbox);
43 char* msg2 = sg_mailbox_get(mailbox);
44 char* msg3 = sg_mailbox_get(mailbox);
45 XBT_INFO("I received '%s', '%s' and '%s'", msg1, msg2, msg3);
49 XBT_INFO("I'm done. See you.");
52 /* Our second class of actors, in charge of sending stuff */
53 static void sender(int argc, char** argv)
55 xbt_assert(argc == 3, "Actor 'sender' requires 2 parameters (mailbox and data to send), but got only %d", argc - 1);
56 XBT_INFO("Hello, I have something to send");
57 const char* sent_data = argv[1];
58 sg_mailbox_t mailbox = sg_mailbox_by_name(argv[2]);
60 sg_mailbox_put(mailbox, xbt_strdup(sent_data), strlen(sent_data));
61 XBT_INFO("I'm done. See you.");
64 static void forwarder(int argc, char** argv)
66 xbt_assert(argc >= 3, "Actor forwarder requires 2 parameters, but got only %d", argc - 1);
67 sg_mailbox_t mailbox_in = sg_mailbox_by_name(argv[1]);
68 sg_mailbox_t mailbox_out = sg_mailbox_by_name(argv[2]);
69 char* msg = sg_mailbox_get(mailbox_in);
70 XBT_INFO("Forward '%s'.", msg);
71 sg_mailbox_put(mailbox_out, msg, strlen(msg));
74 /* Here comes the main function of your program */
75 int main(int argc, char** argv)
77 /* When your program starts, you have to first start a new simulation engine, as follows */
78 simgrid_init(&argc, argv);
80 /* Then you should load a platform file, describing your simulated platform */
81 simgrid_load_platform("../../platforms/small_platform.xml");
83 /* And now you have to ask SimGrid to actually start your actors.
85 * The easiest way to do so is to implement the behavior of your actor in a single function,
86 * as we do here for the receiver actors. This function can take any kind of parameters, as
87 * long as the last parameters of sg_actor_create() or sg_actor_start() match what your function expects.
90 const char* recv_argv[] = {"receiver", "mb42", NULL};
91 sg_actor_create("receiver", sg_host_by_name("Fafard"), receiver, recv_argc, recv_argv);
94 const char* sender1_argv[] = {"sender", "GaBuZoMeu", "mb42", NULL};
95 sg_actor_create("sender1", sg_host_by_name("Tremblay"), sender, sender1_argc, sender1_argv);
98 const char* sender2_argv[] = {"sender", "GloubiBoulga", "mb42", NULL};
99 sg_actor_t sender2 = sg_actor_init("sender2", sg_host_by_name("Jupiter"));
100 sg_actor_start(sender2, sender, sender2_argc, sender2_argv);
102 /* But starting actors directly is considered as a bad experimental habit, since it ties the code
103 * you want to test with the experimental scenario. Starting your actors from an external deployment
104 * file in XML ensures that you can test your code in several scenarios without changing the code itself.
106 * For that, you first need to register your function or your actor as follows.
107 * actor functions must be of type void(*)(int argc, char**argv). */
108 simgrid_register_function("sender", sender);
109 simgrid_register_function("forwarder", forwarder);
110 /* Once actors and functions are registered, just load the deployment file */
111 simgrid_load_deployment("actor-create_d.xml");
113 /* Once every actors are started in the engine, the simulation can start */
116 /* Once the simulation is done, the program is ended */