Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / examples / c / actor-join / actor-join.c
1 /* Copyright (c) 2010-2022. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "simgrid/actor.h"
8 #include "simgrid/engine.h"
9 #include "simgrid/host.h"
10
11 #include "xbt/log.h"
12 #include "xbt/sysdep.h"
13
14 XBT_LOG_NEW_DEFAULT_CATEGORY(actor_join, "Messages specific for this example");
15
16 static void sleeper(int argc, char* argv[])
17 {
18   XBT_INFO("Sleeper started");
19   sg_actor_sleep_for(3);
20   XBT_INFO("I'm done. See you!");
21 }
22
23 static void master(int argc, char* argv[])
24 {
25   const_sg_actor_t actor;
26
27   XBT_INFO("Start sleeper");
28   actor = sg_actor_create("sleeper from master", sg_host_self(), sleeper, 0, NULL);
29   XBT_INFO("Join the sleeper (timeout 2)");
30   sg_actor_join(actor, 2);
31
32   XBT_INFO("Start sleeper");
33   actor = sg_actor_create("sleeper from master", sg_host_self(), sleeper, 0, NULL);
34   XBT_INFO("Join the sleeper (timeout 4)");
35   sg_actor_join(actor, 4);
36
37   XBT_INFO("Start sleeper");
38   actor = sg_actor_create("sleeper from master", sg_host_self(), sleeper, 0, NULL);
39   XBT_INFO("Join the sleeper (timeout 2)");
40   sg_actor_join(actor, 2);
41
42   XBT_INFO("Start sleeper");
43   actor = sg_actor_create("sleeper from master", sg_host_self(), sleeper, 0, NULL);
44   sg_actor_ref(actor); // We have to take that ref because the actor will stop before we join it
45   XBT_INFO("Waiting 4");
46   sg_actor_sleep_for(4);
47   XBT_INFO("Join the sleeper after its end (timeout 1)");
48   sg_actor_join(actor, 1);
49   sg_actor_unref(actor); // Avoid to leak memory
50
51   XBT_INFO("Goodbye now!");
52
53   sg_actor_sleep_for(1);
54
55   XBT_INFO("Goodbye now!");
56 }
57
58 int main(int argc, char* argv[])
59 {
60   simgrid_init(&argc, argv);
61   xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s platform.xml\n", argv[0], argv[0]);
62
63   simgrid_load_platform(argv[1]);
64
65   sg_actor_create("master", sg_host_by_name("Tremblay"), master, 0, NULL);
66
67   simgrid_run();
68
69   XBT_INFO("Simulation time %g", simgrid_get_clock());
70
71   return 0;
72 }