Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines.
[simgrid.git] / examples / c / actor-kill / actor-kill.c
1 /* Copyright (c) 2007-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/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 void victim_on_exit(int ignored1, void* ignored2)
16 {
17   XBT_INFO("I have been killed!");
18 }
19
20 static void victimA_fun(int argc, char* argv[])
21 {
22   sg_actor_on_exit(&victim_on_exit, NULL);
23   XBT_INFO("Hello!");
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 */
29 }
30
31 static void victimB_fun(int argc, char* argv[])
32 {
33   XBT_INFO("Terminate before being killed");
34 }
35
36 static void killer_fun(int argc, char* argv[])
37 {
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);
40
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
43
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_unref(victimB); // Release the ref taken on victimB to avoid to leak memory
57   sg_actor_sleep_for(1.0);
58
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);
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 actor, that will create the victim actor  */
81   sg_actor_create("killer", sg_host_by_name("Tremblay"), killer_fun, 0, NULL);
82
83   simgrid_run();
84
85   return 0;
86 }