From fbc2afa9f040b65a0e8b5703e8eae55d26ba0691 Mon Sep 17 00:00:00 2001 From: Frederic Suter Date: Tue, 11 Feb 2020 06:22:23 +0100 Subject: [PATCH 1/1] add actor-exiting in C 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 | 2 + examples/c/CMakeLists.txt | 4 +- examples/c/actor-exiting/actor-exiting.c | 55 +++++++++++++++++++++ examples/c/actor-exiting/actor-exiting.tesh | 4 ++ 4 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 examples/c/actor-exiting/actor-exiting.c create mode 100644 examples/c/actor-exiting/actor-exiting.tesh diff --git a/MANIFEST.in b/MANIFEST.in index e0d409e0ef..4264bf37ad 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -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 diff --git a/examples/c/CMakeLists.txt b/examples/c/CMakeLists.txt index 13f924c71c..4d0ea2de86 100644 --- a/examples/c/CMakeLists.txt +++ b/examples/c/CMakeLists.txt @@ -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 index 0000000000..23017e7796 --- /dev/null +++ b/examples/c/actor-exiting/actor-exiting.c @@ -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 +#include +#include + +#include +#include + +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 index 0000000000..0ed5c85a7a --- /dev/null +++ b/examples/c/actor-exiting/actor-exiting.tesh @@ -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 -- 2.20.1