Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add actor-exiting in C
authorFrederic Suter <frederic.suter@cc.in2p3.fr>
Tue, 11 Feb 2020 05:22:23 +0000 (06:22 +0100)
committerFrederic Suter <frederic.suter@cc.in2p3.fr>
Tue, 11 Feb 2020 22:24:15 +0000 (23:24 +0100)
This is a shorter counterpart to the C++ example as signals cannot be
used. The only way to be informed of the termination of an actor is
thus to rely on the sg_actor_on_exit function.

MANIFEST.in
examples/c/CMakeLists.txt
examples/c/actor-exiting/actor-exiting.c [new file with mode: 0644]
examples/c/actor-exiting/actor-exiting.tesh [new file with mode: 0644]

index e0d409e..4264bf3 100644 (file)
@@ -21,6 +21,8 @@ include examples/c/actor-create/actor-create.tesh
 include examples/c/actor-create/actor-create_d.xml
 include examples/c/actor-daemon/actor-daemon.c
 include examples/c/actor-daemon/actor-daemon.tesh
+include examples/c/actor-exiting/actor-exiting.c
+include examples/c/actor-exiting/actor-exiting.tesh
 include examples/c/actor-join/actor-join.c
 include examples/c/actor-join/actor-join.tesh
 include examples/c/actor-kill/actor-kill.c
index 13f924c..4d0ea2d 100644 (file)
@@ -1,5 +1,5 @@
 foreach(x
-        actor-create actor-daemon actor-join actor-kill actor-migrate actor-suspend actor-yield
+        actor-create actor-daemon actor-exiting actor-join actor-kill actor-migrate actor-suspend actor-yield
         app-pingpong app-token-ring async-waitany io-disk-raw)
   add_executable       (${x}-c EXCLUDE_FROM_ALL ${x}/${x}.c)
   target_link_libraries(${x}-c simgrid)
@@ -20,7 +20,7 @@ set(xml_files     ${xml_files} ${CMAKE_CURRENT_SOURCE_DIR}/actor-create/actor-cr
                                PARENT_SCOPE)
 
 foreach(x
-        actor-create actor-daemon actor-join actor-kill actor-migrate actor-suspend actor-yield
+        actor-create actor-daemon actor-exiting actor-join actor-kill actor-migrate actor-suspend actor-yield
         app-pingpong app-token-ring async-waitany io-disk-raw)
   ADD_TESH(c-${x} --setenv platfdir=${CMAKE_HOME_DIRECTORY}/examples/platforms
                   --setenv bindir=${CMAKE_BINARY_DIR}/examples/c/${x}
diff --git a/examples/c/actor-exiting/actor-exiting.c b/examples/c/actor-exiting/actor-exiting.c
new file mode 100644 (file)
index 0000000..23017e7
--- /dev/null
@@ -0,0 +1,55 @@
+/* Copyright (c) 2017-2020. The SimGrid Team. All rights reserved.          */
+
+/* This program is free software; you can redistribute it and/or modify it
+ * under the terms of the license (GNU LGPL) which comes with this package. */
+
+/* In C, there is a single way of being informed when an actor exits.
+ *
+ * The sg_actor_on_exit() function allows one to register a function to be
+ * executed when this very actor exits. The registered function will run
+ * when this actor terminates (either because its main function returns, or
+ * because it's killed in any way). No simcall are allowed here: your actor
+ * is dead already, so it cannot interact with its environment in any way
+ * (network, executions, disks, etc).
+ *
+ * Usually, the functions registered in sg_actor_on_exit() are in charge
+ * of releasing any memory allocated by the actor during its execution.
+ */
+
+#include <simgrid/actor.h>
+#include <simgrid/engine.h>
+#include <simgrid/host.h>
+
+#include <xbt/asserts.h>
+#include <xbt/log.h>
+
+XBT_LOG_NEW_DEFAULT_CATEGORY(actor_exiting, "Messages specific for this example");
+
+static int my_on_exit(XBT_ATTRIB_UNUSED int ignored1, XBT_ATTRIB_UNUSED void* ignored2)
+{
+  XBT_INFO("I stop now");
+  return 0;
+}
+
+static void actor_fun()
+{
+  // Register a lambda function to be executed once it stops
+  sg_actor_on_exit(&my_on_exit, NULL);
+
+  sg_actor_self_execute(1e9);
+}
+
+int main(int argc, char* argv[])
+{
+  simgrid_init(&argc, argv);
+  xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s ../platforms/small_platform.xml\n", argv[0], argv[0]);
+
+  simgrid_load_platform(argv[1]); /* - Load the platform description */
+
+  sg_actor_t actor = sg_actor_init("A", sg_host_by_name("Tremblay"));
+  sg_actor_start(actor, actor_fun, 0, NULL);
+
+  simgrid_run();
+
+  return 0;
+}
diff --git a/examples/c/actor-exiting/actor-exiting.tesh b/examples/c/actor-exiting/actor-exiting.tesh
new file mode 100644 (file)
index 0000000..0ed5c85
--- /dev/null
@@ -0,0 +1,4 @@
+#!/usr/bin/env tesh
+
+$ ${bindir:=.}/actor-exiting-c ${platfdir}/small_platform.xml "--log=root.fmt:[%10.6r]%e(%P@%h)%e%m%n"
+> [ 10.194200] (A@Tremblay) I stop now