Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines.
[simgrid.git] / examples / c / actor-suspend / actor-suspend.c
1 /* Copyright (c) 2007-2021. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "simgrid/actor.h"
8 #include "simgrid/engine.h"
9 #include "simgrid/host.h"
10
11 #include "xbt/asserts.h"
12 #include "xbt/log.h"
13
14 XBT_LOG_NEW_DEFAULT_CATEGORY(actor_suspend, "Messages specific for this example");
15
16 /* The Lazy guy only wants to sleep, but can be awaken by the dream_master actor. */
17 static void lazy_guy(int argc, char* argv[])
18 {
19   XBT_INFO("Nobody's watching me ? Let's go to sleep.");
20   sg_actor_suspend(sg_actor_self()); /* - Start by suspending itself */
21   XBT_INFO("Uuuh ? Did somebody call me ?");
22
23   XBT_INFO("Going to sleep..."); /* - Then repetitively go to sleep, but got awaken */
24   sg_actor_sleep_for(10.0);
25   XBT_INFO("Mmm... waking up.");
26
27   XBT_INFO("Going to sleep one more time (for 10 sec)...");
28   sg_actor_sleep_for(10.0);
29   XBT_INFO("Waking up once for all!");
30
31   XBT_INFO("Ok, let's do some work, then (for 10 sec on Boivin).");
32   sg_actor_execute(980.95e6);
33
34   XBT_INFO("Mmmh, I'm done now. Goodbye.");
35 }
36
37 /* The Dream master: */
38 static void dream_master(int argc, char* argv[])
39 {
40   XBT_INFO("Let's create a lazy guy."); /* - Create a lazy_guy actor */
41   sg_actor_t lazy = sg_actor_create("Lazy", sg_host_self(), lazy_guy, 0, NULL);
42   XBT_INFO("Let's wait a little bit...");
43   sg_actor_sleep_for(10.0); /* - Wait for 10 seconds */
44   XBT_INFO("Let's wake the lazy guy up! >:) BOOOOOUUUHHH!!!!");
45   sg_actor_resume(lazy); /* - Then wake up the lazy_guy */
46
47   sg_actor_sleep_for(5.0); /* Repeat two times: */
48   XBT_INFO("Suspend the lazy guy while he's sleeping...");
49   sg_actor_suspend(lazy); /* - Suspend the lazy_guy while he's asleep */
50   XBT_INFO("Let him finish his siesta.");
51   sg_actor_sleep_for(10.0); /* - Wait for 10 seconds */
52   XBT_INFO("Wake up, lazy guy!");
53   sg_actor_resume(lazy); /* - Then wake up the lazy_guy again */
54
55   sg_actor_sleep_for(5.0);
56   XBT_INFO("Suspend again the lazy guy while he's sleeping...");
57   sg_actor_suspend(lazy);
58   XBT_INFO("This time, don't let him finish his siesta.");
59   sg_actor_sleep_for(2.0);
60   XBT_INFO("Wake up, lazy guy!");
61   sg_actor_resume(lazy);
62
63   sg_actor_sleep_for(5.0);
64   XBT_INFO("Give a 2 seconds break to the lazy guy while he's working...");
65   sg_actor_suspend(lazy);
66   sg_actor_sleep_for(2.0);
67   XBT_INFO("Back to work, lazy guy!");
68   sg_actor_resume(lazy);
69
70   XBT_INFO("OK, I'm done here.");
71 }
72
73 int main(int argc, char* argv[])
74 {
75   simgrid_init(&argc, argv);
76   xbt_assert(argc > 1, "Usage: %s platform_file\n\tExample: %s msg_platform.xml\n", argv[0], argv[0]);
77
78   simgrid_load_platform(argv[1]);
79   simgrid_register_function("dream_master", dream_master);
80
81   sg_actor_create("dream_master", sg_host_by_name("Boivin"), dream_master, 0, NULL);
82
83   simgrid_run();
84
85   return 0;
86 }