Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
actor-exiting: Align the C test with the C++ one
[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 #include <simgrid/mailbox.h>
23
24 #include <xbt/asserts.h>
25 #include <xbt/log.h>
26
27 XBT_LOG_NEW_DEFAULT_CATEGORY(actor_exiting, "Messages specific for this example");
28
29 static int A_on_exit(XBT_ATTRIB_UNUSED int ignored1, XBT_ATTRIB_UNUSED void* ignored2)
30 {
31   XBT_INFO("I stop now");
32   return 0;
33 }
34
35 static void actorA_fun(XBT_ATTRIB_UNUSED int argc, XBT_ATTRIB_UNUSED char* argv[])
36 {
37   // Register a lambda function to be executed once it stops
38   sg_actor_on_exit(&A_on_exit, NULL);
39
40   sg_actor_sleep_for(1);
41 }
42 static void actorB_fun(XBT_ATTRIB_UNUSED int argc, XBT_ATTRIB_UNUSED char* argv[])
43 {
44   sg_actor_sleep_for(2);
45 }
46 static int C_on_exit(int failed, XBT_ATTRIB_UNUSED void* ignored2)
47 {
48   if (failed) {
49     XBT_INFO("I was killed!");
50     if (xbt_log_no_loc)
51       XBT_INFO("The backtrace would be displayed here if --log=no_loc would not have been passed");
52     else
53       xbt_backtrace_display_current();
54   } else
55     XBT_INFO("Exiting gracefully.");
56   return 0;
57 }
58 static void actorC_fun(XBT_ATTRIB_UNUSED int argc, XBT_ATTRIB_UNUSED char* argv[])
59 {
60   // Register a lambda function to be executed once it stops
61   sg_actor_on_exit(&C_on_exit, NULL);
62
63   sg_actor_sleep_for(3);
64   XBT_INFO("And now, induce a deadlock by waiting for a message that will never come\n\n");
65   sg_mailbox_get(sg_mailbox_by_name("nobody"));
66   xbt_die("Receiving is not supposed to succeed when nobody is sending");
67 }
68
69 int main(int argc, char* argv[])
70 {
71   simgrid_init(&argc, argv);
72   xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s ../platforms/small_platform.xml\n", argv[0], argv[0]);
73
74   simgrid_load_platform(argv[1]); /* - Load the platform description */
75
76   sg_actor_create("A", sg_host_by_name("Tremblay"), actorA_fun, 0, NULL);
77   sg_actor_create("B", sg_host_by_name("Fafard"), actorB_fun, 0, NULL);
78   sg_actor_create("C", sg_host_by_name("Ginette"), actorC_fun, 0, NULL);
79
80   simgrid_run();
81
82   return 0;
83 }