Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2d450bbbc6066c41cabe8735fe4561100a67ea00
[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_self_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_init("victim A", sg_host_by_name("Fafard"));
41   sg_actor_start(victimA, victimA_fun, 0, NULL);
42   sg_actor_t victimB = sg_actor_init("victim B", sg_host_by_name("Jupiter"));
43   sg_actor_start(victimB, victimB_fun, 0, NULL);
44   sg_actor_sleep_for(10.0);
45
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);
49
50   XBT_INFO("Kill the victim A"); /* - and then kill it */
51   sg_actor_kill(victimA);
52   sg_actor_sleep_for(1.0);
53
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_sleep_for(1.0);
57
58   XBT_INFO("Start a new actor, and kill it right away");
59   sg_actor_t victimC = sg_actor_init("victim C", sg_host_by_name("Jupiter"));
60   sg_actor_start(victimC, victimA_fun, 0, NULL);
61   sg_actor_kill(victimC);
62   sg_actor_sleep_for(1.0);
63
64   XBT_INFO("Killing everybody but myself");
65   sg_actor_kill_all();
66
67   XBT_INFO("OK, goodbye now. I commit a suicide.");
68   sg_actor_exit();
69
70   XBT_INFO("This line will never get displayed: I'm already dead since the previous line.");
71 }
72
73 int main(int argc, char* argv[])
74 {
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]);
77
78   simgrid_load_platform(argv[1]);
79
80   /* - Create and deploy killer process, that will create the victim process  */
81   sg_actor_t killer = sg_actor_init("killer", sg_host_by_name("Tremblay"));
82   sg_actor_start(killer, killer_fun, 0, NULL);
83
84   simgrid_run();
85
86   return 0;
87 }