Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
dfcfc41f761490d683b5e8f1cb41f6d2037624a3
[simgrid.git] / examples / cpp / actor-kill / s4u-actor-kill.cpp
1 /* Copyright (c) 2017-2021. 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 #include <simgrid/s4u.hpp>
7 namespace sg4 = simgrid::s4u;
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_actor_kill, "Messages specific for this s4u example");
10
11 static void victimA_fun()
12 {
13   sg4::this_actor::on_exit([](bool /*failed*/) { XBT_INFO("I have been killed!"); });
14   XBT_INFO("Hello!");
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 */
20 }
21
22 static void victimB_fun()
23 {
24   XBT_INFO("Terminate before being killed");
25 }
26
27 static void killer()
28 {
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 */
33
34   XBT_INFO("Resume the victim A"); /* - Resume it from its suspended state */
35   victimA->resume();
36   sg4::this_actor::sleep_for(2);
37
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)
40
41   sg4::this_actor::sleep_for(1);
42
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
45
46   sg4::this_actor::sleep_for(1);
47
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);
50   victimC->kill();
51
52   sg4::this_actor::sleep_for(1);
53
54   XBT_INFO("Killing everybody but myself");
55   sg4::Actor::kill_all();
56
57   XBT_INFO("OK, goodbye now. I commit a suicide.");
58   sg4::this_actor::exit();
59
60   XBT_INFO("This line never gets displayed: I'm already dead since the previous line.");
61 }
62
63 int main(int argc, char* argv[])
64 {
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]);
67
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);
71
72   e.run(); /* - Run the simulation */
73
74   return 0;
75 }