Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
introduce sg_actor_create and use it in C examples
[simgrid.git] / examples / c / actor-kill / actor-kill.c
1 /* Copyright (c) 2007-2020. 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/actor.h"
7 #include "simgrid/engine.h"
8 #include "simgrid/host.h"
9
10 #include "xbt/log.h"
11 #include "xbt/sysdep.h"
12
13 XBT_LOG_NEW_DEFAULT_CATEGORY(actor_kill, "Messages specific for this example");
14
15 static int victim_on_exit(XBT_ATTRIB_UNUSED int ignored1, XBT_ATTRIB_UNUSED void* ignored2)
16 {
17   XBT_INFO("I have been killed!");
18   return 0;
19 }
20
21 static void victimA_fun(XBT_ATTRIB_UNUSED int argc, XBT_ATTRIB_UNUSED char* argv[])
22 {
23   sg_actor_on_exit(&victim_on_exit, NULL);
24   XBT_INFO("Hello!");
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 */
30 }
31
32 static void victimB_fun(XBT_ATTRIB_UNUSED int argc, XBT_ATTRIB_UNUSED char* argv[])
33 {
34   XBT_INFO("Terminate before being killed");
35 }
36
37 static void killer_fun(XBT_ATTRIB_UNUSED int argc, XBT_ATTRIB_UNUSED char* argv[])
38 {
39   XBT_INFO("Hello!"); /* - First start a victim process */
40   sg_actor_t victimA = sg_actor_create("victim A", sg_host_by_name("Fafard"), victimA_fun, 0, NULL);
41
42   sg_actor_t victimB = sg_actor_create("victim B", sg_host_by_name("Jupiter"), victimB_fun, 0, NULL);
43   sg_actor_ref(victimB); // We have to take that ref because victimB will end before we try to kill it
44
45   sg_actor_sleep_for(10.0);
46
47   XBT_INFO("Resume the victim A"); /* - Resume it from its suspended state */
48   sg_actor_resume(victimA);
49   sg_actor_sleep_for(2.0);
50
51   XBT_INFO("Kill the victim A"); /* - and then kill it */
52   sg_actor_kill(victimA);
53   sg_actor_sleep_for(1.0);
54
55   XBT_INFO("Kill victimB, even if it's already dead"); /* that's a no-op, there is no zombies in SimGrid */
56   sg_actor_kill(victimB); // the actor is automatically garbage-collected after this last reference
57   sg_actor_unref(victimB); // Release the ref taken on victimB to avoid to leak memory
58   sg_actor_sleep_for(1.0);
59
60   XBT_INFO("Start a new actor, and kill it right away");
61   sg_actor_t victimC = sg_actor_create("victim C", sg_host_by_name("Jupiter"), victimA_fun, 0, NULL);
62   sg_actor_kill(victimC);
63   sg_actor_sleep_for(1.0);
64
65   XBT_INFO("Killing everybody but myself");
66   sg_actor_kill_all();
67
68   XBT_INFO("OK, goodbye now. I commit a suicide.");
69   sg_actor_exit();
70
71   XBT_INFO("This line will never get displayed: I'm already dead since the previous line.");
72 }
73
74 int main(int argc, char* argv[])
75 {
76   simgrid_init(&argc, argv);
77   xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s msg_platform.xml\n", argv[0], argv[0]);
78
79   simgrid_load_platform(argv[1]);
80
81   /* - Create and deploy killer process, that will create the victim process  */
82   sg_actor_create("killer", sg_host_by_name("Tremblay"), killer_fun, 0, NULL);
83
84   simgrid_run();
85
86   return 0;
87 }