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 void victim_on_exit(int ignored1, void* ignored2)
17 XBT_INFO("I have been killed!");
20 static void victimA_fun(int argc, char* argv[])
22 sg_actor_on_exit(&victim_on_exit, NULL);
24 XBT_INFO("Suspending myself");
25 sg_actor_suspend(sg_actor_self()); /* - First suspend itself */
26 XBT_INFO("OK, OK. Let's work"); /* - Then is resumed and start to execute a task */
27 sg_actor_execute(1e9);
28 XBT_INFO("Bye!"); /* - But will never reach the end of it */
31 static void victimB_fun(int argc, char* argv[])
33 XBT_INFO("Terminate before being killed");
36 static void killer_fun(int argc, char* argv[])
38 XBT_INFO("Hello!"); /* - First start a victim actor */
39 sg_actor_t victimA = sg_actor_create("victim A", sg_host_by_name("Fafard"), victimA_fun, 0, NULL);
41 sg_actor_t victimB = sg_actor_create("victim B", sg_host_by_name("Jupiter"), victimB_fun, 0, NULL);
42 sg_actor_ref(victimB); // We have to take that ref because victimB will end before we try to kill it
44 sg_actor_sleep_for(10.0);
46 XBT_INFO("Resume the victim A"); /* - Resume it from its suspended state */
47 sg_actor_resume(victimA);
48 sg_actor_sleep_for(2.0);
50 XBT_INFO("Kill the victim A"); /* - and then kill it */
51 sg_actor_kill(victimA);
52 sg_actor_sleep_for(1.0);
54 XBT_INFO("Kill victimB, even if it's already dead"); /* that's a no-op, there is no zombies in SimGrid */
55 sg_actor_kill(victimB); // the actor is automatically garbage-collected after this last reference
56 sg_actor_unref(victimB); // Release the ref taken on victimB to avoid to leak memory
57 sg_actor_sleep_for(1.0);
59 XBT_INFO("Start a new actor, and kill it right away");
60 sg_actor_t victimC = sg_actor_create("victim C", sg_host_by_name("Jupiter"), victimA_fun, 0, NULL);
61 sg_actor_kill(victimC);
62 sg_actor_sleep_for(1.0);
64 XBT_INFO("Killing everybody but myself");
67 XBT_INFO("OK, goodbye now. I commit a suicide.");
70 XBT_INFO("This line will never get displayed: I'm already dead since the previous line.");
73 int main(int argc, char* argv[])
75 simgrid_init(&argc, argv);
76 xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s msg_platform.xml\n", argv[0], argv[0]);
78 simgrid_load_platform(argv[1]);
80 /* - Create and deploy killer actor, that will create the victim actor */
81 sg_actor_create("killer", sg_host_by_name("Tremblay"), killer_fun, 0, NULL);