Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cde29b7beaf7ce986e6afdbb4b09477ae3cc8d1f
[simgrid.git] / examples / c / actor-exiting / actor-exiting.c
1 /* Copyright (c) 2017-2020. 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 /* In C, there is a single way of being informed when an actor exits.
7  *
8  * The sg_actor_on_exit() function allows one to register a function to be
9  * executed when this very actor exits. The registered function will run
10  * when this actor terminates (either because its main function returns, or
11  * because it's killed in any way). No simcall are allowed here: your actor
12  * is dead already, so it cannot interact with its environment in any way
13  * (network, executions, disks, etc).
14  *
15  * Usually, the functions registered in sg_actor_on_exit() are in charge
16  * of releasing any memory allocated by the actor during its execution.
17  */
18
19 #include <simgrid/actor.h>
20 #include <simgrid/engine.h>
21 #include <simgrid/host.h>
22
23 #include <xbt/asserts.h>
24 #include <xbt/log.h>
25
26 XBT_LOG_NEW_DEFAULT_CATEGORY(actor_exiting, "Messages specific for this example");
27
28 static int my_on_exit(XBT_ATTRIB_UNUSED int ignored1, XBT_ATTRIB_UNUSED void* ignored2)
29 {
30   XBT_INFO("I stop now");
31   return 0;
32 }
33
34 static void actor_fun(XBT_ATTRIB_UNUSED int argc, XBT_ATTRIB_UNUSED char* argv[])
35 {
36   // Register a lambda function to be executed once it stops
37   sg_actor_on_exit(&my_on_exit, NULL);
38
39   sg_actor_execute(1e9);
40 }
41
42 int main(int argc, char* argv[])
43 {
44   simgrid_init(&argc, argv);
45   xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s ../platforms/small_platform.xml\n", argv[0], argv[0]);
46
47   simgrid_load_platform(argv[1]); /* - Load the platform description */
48
49   sg_actor_t actor = sg_actor_init("A", sg_host_by_name("Tremblay"));
50   sg_actor_start(actor, actor_fun, 0, NULL);
51
52   simgrid_run();
53
54   return 0;
55 }