Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add sg_actor_parallel_execute and rename sg_actor_self_execute
[simgrid.git] / examples / c / actor-suspend / actor-suspend.c
1 /* Copyright (c) 2007-2020. 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 process. */
17 static void lazy_guy(XBT_ATTRIB_UNUSED int argc, XBT_ATTRIB_UNUSED 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(XBT_ATTRIB_UNUSED int argc, XBT_ATTRIB_UNUSED char* argv[])
39 {
40   XBT_INFO("Let's create a lazy guy."); /* - Create a lazy_guy process */
41   sg_actor_t lazy = sg_actor_init("Lazy", sg_host_self());
42   sg_actor_start(lazy, lazy_guy, 0, NULL);
43   XBT_INFO("Let's wait a little bit...");
44   sg_actor_sleep_for(10.0); /* - Wait for 10 seconds */
45   XBT_INFO("Let's wake the lazy guy up! >:) BOOOOOUUUHHH!!!!");
46   sg_actor_resume(lazy); /* - Then wake up the lazy_guy */
47
48   sg_actor_sleep_for(5.0); /* Repeat two times: */
49   XBT_INFO("Suspend the lazy guy while he's sleeping...");
50   sg_actor_suspend(lazy); /* - Suspend the lazy_guy while he's asleep */
51   XBT_INFO("Let him finish his siesta.");
52   sg_actor_sleep_for(10.0); /* - Wait for 10 seconds */
53   XBT_INFO("Wake up, lazy guy!");
54   sg_actor_resume(lazy); /* - Then wake up the lazy_guy again */
55
56   sg_actor_sleep_for(5.0);
57   XBT_INFO("Suspend again the lazy guy while he's sleeping...");
58   sg_actor_suspend(lazy);
59   XBT_INFO("This time, don't let him finish his siesta.");
60   sg_actor_sleep_for(2.0);
61   XBT_INFO("Wake up, lazy guy!");
62   sg_actor_resume(lazy);
63
64   sg_actor_sleep_for(5.0);
65   XBT_INFO("Give a 2 seconds break to the lazy guy while he's working...");
66   sg_actor_suspend(lazy);
67   sg_actor_sleep_for(2.0);
68   XBT_INFO("Back to work, lazy guy!");
69   sg_actor_resume(lazy);
70
71   XBT_INFO("OK, I'm done here.");
72 }
73
74 int main(int argc, char* argv[])
75 {
76   simgrid_init(&argc, argv);
77   xbt_assert(argc > 1, "Usage: %s platform_file\n\tExample: %s msg_platform.xml\n", argv[0], argv[0]);
78
79   simgrid_load_platform(argv[1]);
80   simgrid_register_function("dream_master", dream_master);
81
82   sg_actor_t actor = sg_actor_init("dream_master", sg_host_by_name("Boivin"));
83   sg_actor_start(actor, dream_master, 0, NULL);
84
85   simgrid_run();
86
87   return 0;
88 }