From: Millian Poquet Date: Wed, 21 Feb 2018 17:00:22 +0000 (+0100) Subject: [examples] add s4u-actor-kill-pid example X-Git-Tag: v3.19~181^2 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/2dcd0715d4d0b4f3e4774c36fd0207af8591e3f1 [examples] add s4u-actor-kill-pid example This example is very similar to s4u-actor-kill, but tests the static s4u::Actor::kill(aid_t) method instead of s4u::Actor::kill(void). --- diff --git a/examples/s4u/CMakeLists.txt b/examples/s4u/CMakeLists.txt index 22ef7d36cd..29d75ac84e 100644 --- a/examples/s4u/CMakeLists.txt +++ b/examples/s4u/CMakeLists.txt @@ -1,4 +1,5 @@ -foreach (example actor-create actor-daemon actor-join actor-kill actor-lifetime actor-migration actor-suspend actor-yield +foreach (example actor-create actor-daemon actor-join actor-kill actor-kill-pid + actor-lifetime actor-migration actor-suspend actor-yield app-chainsend app-masterworker app-pingpong app-token-ring async-wait async-waitany async-waitall cloud-capping cloud-migration cloud-simple @@ -83,7 +84,8 @@ set(txt_files ${txt_files} ${CMAKE_CURRENT_SOURCE_DIR}/replay-comm/s4u-re ${CMAKE_CURRENT_SOURCE_DIR}/replay-storage/s4u-replay-storage.txt ${CMAKE_CURRENT_SOURCE_DIR}/README.doc PARENT_SCOPE) -foreach(example actor-create actor-daemon actor-join actor-kill actor-lifetime actor-migration actor-suspend actor-yield +foreach(example actor-create actor-daemon actor-join actor-kill actor-kill-pid + actor-lifetime actor-migration actor-suspend actor-yield app-bittorrent app-chainsend app-masterworker app-pingpong app-token-ring async-wait async-waitall async-waitany cloud-capping cloud-migration cloud-simple diff --git a/examples/s4u/README.doc b/examples/s4u/README.doc index cafaae8e98..1780dde6c5 100644 --- a/examples/s4u/README.doc +++ b/examples/s4u/README.doc @@ -58,7 +58,12 @@ TODO: document here the examples about plugins - Kill actors. @ref examples/s4u/actor-kill/s4u-actor-kill.cpp \n Actors can forcefully stop other actors with the @ref - simgrid::s4u::Actor::kill() method. + simgrid::s4u::Actor::kill(void) method. + + - Kill actors (other function). + @ref examples/s4u/actor-kill-pid/s4u-actor-kill-pid.cpp \n + Actors can forcefully stop other actors with the @ref + simgrid::s4u::Actor::kill(aid_t) method. - Controling the actor life cycle from the XML. @ref examples/s4u/actor-lifetime/s4u-actor-lifetime.cpp @@ -344,4 +349,4 @@ than the previous examples. @example examples/platforms/energy_platform.xml @example examples/platforms/prop.xml -*/ \ No newline at end of file +*/ diff --git a/examples/s4u/actor-kill-pid/s4u-actor-kill-pid.cpp b/examples/s4u/actor-kill-pid/s4u-actor-kill-pid.cpp new file mode 100644 index 0000000000..3b8d5a3017 --- /dev/null +++ b/examples/s4u/actor-kill-pid/s4u-actor-kill-pid.cpp @@ -0,0 +1,83 @@ +/* Copyright (c) 2017 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. */ + +#include + +XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_actor_kill_pid, "Messages specific for this s4u example"); + +static int on_exit(void*, void*) +{ + XBT_INFO("I have been killed!"); + return 0; +} + +static void victimA_fun() +{ + simgrid::s4u::this_actor::onExit(on_exit, nullptr); + XBT_INFO("Hello!"); + XBT_INFO("Suspending myself"); + simgrid::s4u::this_actor::suspend(); /* - Start by suspending itself */ + XBT_INFO("OK, OK. Let's work"); /* - Then is resumed and start to execute a task */ + simgrid::s4u::this_actor::execute(1e9); + XBT_INFO("Bye!"); /* - But will never reach the end of it */ +} + +static void victimB_fun() +{ + XBT_INFO("Terminate before being killed"); +} + +static void killer() +{ + XBT_INFO("Hello!"); /* - First start a victim process */ + simgrid::s4u::ActorPtr victimA = + simgrid::s4u::Actor::createActor("victim A", simgrid::s4u::Host::by_name("Fafard"), victimA_fun); + simgrid::s4u::ActorPtr victimB = + simgrid::s4u::Actor::createActor("victim B", simgrid::s4u::Host::by_name("Jupiter"), victimB_fun); + simgrid::s4u::this_actor::sleep_for(10); /* - Wait for 10 seconds */ + + XBT_INFO("Resume the victim A"); /* - Resume it from its suspended state */ + victimA->resume(); + simgrid::s4u::this_actor::sleep_for(2); + + aid_t pidA = victimA->getPid(); + XBT_INFO("Kill the victim A (pid=%lu)", pidA); /* - and then kill it */ + simgrid::s4u::Actor::kill(pidA); + + aid_t pidB = victimB->getPid(); + XBT_INFO("Kill victimB (pid=%lu), even if it's already dead", pidB); /* that's a no-op, there is no zombies in SimGrid */ + try + { + simgrid::s4u::Actor::kill(pidB); + } + catch (const std::runtime_error &) + {} + simgrid::s4u::this_actor::sleep_for(1); + + XBT_INFO("Killing everybody but myself"); + simgrid::s4u::Actor::killAll(); + + aid_t pidMe = simgrid::s4u::this_actor::getPid(); + XBT_INFO("OK, goodbye now. I commit a suicide (pid=%lu).", pidMe); + simgrid::s4u::Actor::kill(pidMe); + + XBT_INFO("This line will never get displayed: I'm already dead since the previous line."); +} + +int main(int argc, char* argv[]) +{ + simgrid::s4u::Engine e(&argc, argv); + xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s msg_platform.xml\n", argv[0], argv[0]); + + e.loadPlatform(argv[1]); /* - Load the platform description */ + /* - Create and deploy killer process, that will create the victim actors */ + simgrid::s4u::Actor::createActor("killer", simgrid::s4u::Host::by_name("Tremblay"), killer); + + e.run(); /* - Run the simulation */ + + XBT_INFO("Simulation time %g", e.getClock()); + + return 0; +} diff --git a/examples/s4u/actor-kill-pid/s4u-actor-kill-pid.tesh b/examples/s4u/actor-kill-pid/s4u-actor-kill-pid.tesh new file mode 100644 index 0000000000..e6171fc36f --- /dev/null +++ b/examples/s4u/actor-kill-pid/s4u-actor-kill-pid.tesh @@ -0,0 +1,15 @@ +#!/usr/bin/env tesh + +$ $SG_TEST_EXENV ${bindir:=.}/s4u-actor-kill-pid ${platfdir}/small_platform.xml "--log=root.fmt:[%10.6r]%e(%P@%h)%e%m%n" +>[ 0.000000] (killer@Tremblay) Hello! +>[ 0.000000] (victim A@Fafard) Hello! +>[ 0.000000] (victim A@Fafard) Suspending myself +>[ 0.000000] (victim B@Jupiter) Terminate before being killed +>[ 10.000000] (killer@Tremblay) Resume the victim A +>[ 10.000000] (victim A@Fafard) OK, OK. Let's work +>[ 12.000000] (killer@Tremblay) Kill the victim A (pid=2) +>[ 12.000000] (killer@Tremblay) Kill victimB (pid=3), even if it's already dead +>[ 13.000000] (killer@Tremblay) Killing everybody but myself +>[ 13.000000] (victim A@Fafard) I have been killed! +>[ 13.000000] (killer@Tremblay) OK, goodbye now. I commit a suicide (pid=1). +>[ 13.000000] (maestro@) Simulation time 13