Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
convert more exemples from MSG to S4U-C
[simgrid.git] / examples / c / actor-join / actor-join.c
1 /* Copyright (c) 2010-2020. 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(XBT_ATTRIB_UNUSED int argc, XBT_ATTRIB_UNUSED 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(XBT_ATTRIB_UNUSED int argc, XBT_ATTRIB_UNUSED char* argv[])
24 {
25   sg_actor_t actor;
26
27   XBT_INFO("Start sleeper");
28   actor = sg_actor_init("sleeper from master", sg_host_self());
29   sg_actor_start(actor, sleeper, 0, NULL);
30   XBT_INFO("Join the sleeper (timeout 2)");
31   sg_actor_join(actor, 2);
32
33   XBT_INFO("Start sleeper");
34   actor = sg_actor_init("sleeper from master", sg_host_self());
35   sg_actor_start(actor, sleeper, 0, NULL);
36   XBT_INFO("Join the sleeper (timeout 4)");
37   sg_actor_join(actor, 4);
38
39   XBT_INFO("Start sleeper");
40   actor = sg_actor_init("sleeper from master", sg_host_self());
41   sg_actor_start(actor, sleeper, 0, NULL);
42   XBT_INFO("Join the sleeper (timeout 2)");
43   sg_actor_join(actor, 2);
44
45   XBT_INFO("Start sleeper");
46   actor = sg_actor_init("sleeper from master", sg_host_self());
47   sg_actor_start(actor, sleeper, 0, NULL);
48   sg_actor_ref(actor); // We have to take that ref because the actor will stop before we join it
49   XBT_INFO("Waiting 4");
50   sg_actor_sleep_for(4);
51   XBT_INFO("Join the sleeper after its end (timeout 1)");
52   sg_actor_join(actor, 1);
53   sg_actor_unref(actor); // Avoid to leak memory
54
55   XBT_INFO("Goodbye now!");
56
57   sg_actor_sleep_for(1);
58
59   XBT_INFO("Goodbye now!");
60 }
61
62 int main(int argc, char* argv[])
63 {
64   simgrid_init(&argc, argv);
65   xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s msg_platform.xml\n", argv[0], argv[0]);
66
67   simgrid_load_platform(argv[1]);
68
69   sg_actor_t actor = sg_actor_init("master", sg_host_by_name("Tremblay"));
70   sg_actor_start(actor, master, 0, NULL);
71
72   simgrid_run();
73
74   XBT_INFO("Simulation time %g", simgrid_get_clock());
75
76   return 0;
77 }