1 /* Copyright (c) 2007-2020. 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/actor.h"
7 #include "simgrid/engine.h"
8 #include "simgrid/host.h"
11 #include "xbt/sysdep.h"
13 XBT_LOG_NEW_DEFAULT_CATEGORY(actor_kill, "Messages specific for this example");
15 static int victim_on_exit(XBT_ATTRIB_UNUSED int ignored1, XBT_ATTRIB_UNUSED void* ignored2)
17 XBT_INFO("I have been killed!");
21 static void victimA_fun(XBT_ATTRIB_UNUSED int argc, XBT_ATTRIB_UNUSED char* argv[])
23 sg_actor_on_exit(&victim_on_exit, NULL);
25 XBT_INFO("Suspending myself");
26 sg_actor_suspend(sg_actor_self()); /* - First suspend itself */
27 XBT_INFO("OK, OK. Let's work"); /* - Then is resumed and start to execute a task */
28 sg_actor_execute(1e9);
29 XBT_INFO("Bye!"); /* - But will never reach the end of it */
32 static void victimB_fun(XBT_ATTRIB_UNUSED int argc, XBT_ATTRIB_UNUSED char* argv[])
34 XBT_INFO("Terminate before being killed");
37 static void killer_fun(XBT_ATTRIB_UNUSED int argc, XBT_ATTRIB_UNUSED char* argv[])
39 XBT_INFO("Hello!"); /* - First start a victim process */
40 sg_actor_t victimA = sg_actor_init("victim A", sg_host_by_name("Fafard"));
41 sg_actor_start(victimA, victimA_fun, 0, NULL);
43 sg_actor_t victimB = sg_actor_init("victim B", sg_host_by_name("Jupiter"));
44 sg_actor_ref(victimB); // We have to take that ref because victimB will end before we try to kill it
45 sg_actor_start(victimB, victimB_fun, 0, NULL);
47 sg_actor_sleep_for(10.0);
49 XBT_INFO("Resume the victim A"); /* - Resume it from its suspended state */
50 sg_actor_resume(victimA);
51 sg_actor_sleep_for(2.0);
53 XBT_INFO("Kill the victim A"); /* - and then kill it */
54 sg_actor_kill(victimA);
55 sg_actor_sleep_for(1.0);
57 XBT_INFO("Kill victimB, even if it's already dead"); /* that's a no-op, there is no zombies in SimGrid */
58 sg_actor_kill(victimB); // the actor is automatically garbage-collected after this last reference
59 sg_actor_unref(victimB); // Release the ref taken on victimB to avoid to leak memory
60 sg_actor_sleep_for(1.0);
62 XBT_INFO("Start a new actor, and kill it right away");
63 sg_actor_t victimC = sg_actor_init("victim C", sg_host_by_name("Jupiter"));
64 sg_actor_start(victimC, victimA_fun, 0, NULL);
65 sg_actor_kill(victimC);
66 sg_actor_sleep_for(1.0);
68 XBT_INFO("Killing everybody but myself");
71 XBT_INFO("OK, goodbye now. I commit a suicide.");
74 XBT_INFO("This line will never get displayed: I'm already dead since the previous line.");
77 int main(int argc, char* argv[])
79 simgrid_init(&argc, argv);
80 xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s msg_platform.xml\n", argv[0], argv[0]);
82 simgrid_load_platform(argv[1]);
84 /* - Create and deploy killer process, that will create the victim process */
85 sg_actor_t killer = sg_actor_init("killer", sg_host_by_name("Tremblay"));
86 sg_actor_start(killer, killer_fun, 0, NULL);