1 /* Copyright (c) 2017-2021. The SimGrid Team. All rights reserved. */
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. */
6 #include <simgrid/s4u.hpp>
7 namespace sg4 = simgrid::s4u;
9 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_actor_kill, "Messages specific for this s4u example");
11 static void victimA_fun()
13 sg4::this_actor::on_exit([](bool /*failed*/) { XBT_INFO("I have been killed!"); });
15 XBT_INFO("Suspending myself");
16 sg4::this_actor::suspend(); /* - Start by suspending itself */
17 XBT_INFO("OK, OK. Let's work"); /* - Then is resumed and start to execute some flops */
18 sg4::this_actor::execute(1e9);
19 XBT_INFO("Bye!"); /* - But will never reach the end of it */
22 static void victimB_fun()
24 XBT_INFO("Terminate before being killed");
29 XBT_INFO("Hello!"); /* - First start a victim actor */
30 sg4::ActorPtr victimA = sg4::Actor::create("victim A", sg4::Host::by_name("Fafard"), victimA_fun);
31 sg4::ActorPtr victimB = sg4::Actor::create("victim B", sg4::Host::by_name("Jupiter"), victimB_fun);
32 sg4::this_actor::sleep_for(10); /* - Wait for 10 seconds */
34 XBT_INFO("Resume the victim A"); /* - Resume it from its suspended state */
36 sg4::this_actor::sleep_for(2);
38 XBT_INFO("Kill the victim A"); /* - and then kill it */
39 sg4::Actor::by_pid(victimA->get_pid())->kill(); // You can retrieve an actor from its PID (and then kill it)
41 sg4::this_actor::sleep_for(1);
43 XBT_INFO("Kill victimB, even if it's already dead"); /* that's a no-op, there is no zombies in SimGrid */
44 victimB->kill(); // the actor is automatically garbage-collected after this last reference
46 sg4::this_actor::sleep_for(1);
48 XBT_INFO("Start a new actor, and kill it right away");
49 sg4::ActorPtr victimC = sg4::Actor::create("victim C", sg4::Host::by_name("Jupiter"), victimA_fun);
52 sg4::this_actor::sleep_for(1);
54 XBT_INFO("Killing everybody but myself");
55 sg4::Actor::kill_all();
57 XBT_INFO("OK, goodbye now. I commit a suicide.");
58 sg4::this_actor::exit();
60 XBT_INFO("This line never gets displayed: I'm already dead since the previous line.");
63 int main(int argc, char* argv[])
65 sg4::Engine e(&argc, argv);
66 xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s ../platforms/small_platform.xml\n", argv[0], argv[0]);
68 e.load_platform(argv[1]); /* - Load the platform description */
69 /* - Create and deploy killer actor, that will create the victim actors */
70 sg4::Actor::create("killer", e.host_by_name("Tremblay"), killer);
72 e.run(); /* - Run the simulation */