Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cdafaf106cd9f1cc489886d70bf734ce874c3313
[simgrid.git] / examples / msg / process-suspend / process-suspend.c
1 /* Copyright (c) 2007, 2009-2015. 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/msg.h"
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_process_suspend, "Messages specific for this msg example");
10
11 #define SLEEP(duration)                                 \
12   if (MSG_process_sleep(duration) != MSG_OK)            \
13     xbt_die("What's going on??? I failed to sleep!");
14
15 /** @addtogroup MSG_examples
16  *  @section MSG_ex_process Acting on Processes
17  * 
18  * - <b>Suspend/Resume: process-suspend/process-suspend.c</b>. Processes can be suspended and resumed during
19  *   their executions thanks to the @ref MSG_process_suspend and @ref MSG_process_resume functions.
20  */
21
22 /** The Lazy guy only wants to sleep, but can be awaken by the @ref dream_master. */
23 static int lazy_guy(int argc, char *argv[])
24 {
25   XBT_INFO("Nobody's watching me ? Let's go to sleep.");
26   MSG_process_suspend(MSG_process_self());       /** - Start by suspending itself */
27   XBT_INFO("Uuuh ? Did somebody call me ?");
28
29   XBT_INFO("Going to sleep...");                 /** - Then repetitively go to sleep, but got awaken */
30   SLEEP(10.0);
31   XBT_INFO("Mmm... waking up.");
32
33   XBT_INFO("Going to sleep one more time...");
34   SLEEP(10.0);
35   XBT_INFO("Waking up once for all!");
36
37   XBT_INFO("Mmmh, goodbye now.");
38   return 0;
39 }
40
41 /** The Dream master: */
42 static int dream_master(int argc, char *argv[])
43 {
44   msg_process_t lazy = NULL;
45
46   XBT_INFO("Let's create a lazy guy."); /** - Create a @ref lazy_guy process */
47   lazy = MSG_process_create("Lazy", lazy_guy, NULL, MSG_host_self());
48   XBT_INFO("Let's wait a little bit...");
49   SLEEP(10.0);                          /** - Wait for 10 seconds */
50   XBT_INFO("Let's wake the lazy guy up! >:) BOOOOOUUUHHH!!!!");
51   MSG_process_resume(lazy);             /** - Then wake up the @ref lazy_guy */
52
53   SLEEP(5.0);                           /** Repeat two times: */
54   XBT_INFO("Suspend the lazy guy while he's sleeping...");
55   MSG_process_suspend(lazy);            /** - Suspend the @ref lazy_guy while he's asleep */
56   XBT_INFO("Let him finish his siesta.");
57   SLEEP(10.0);                          /** - Wait for 10 seconds */
58   XBT_INFO("Wake up, lazy guy!");
59   MSG_process_resume(lazy);             /** - Then wake up the @ref lazy_guy again */
60
61   SLEEP(5.0);
62   XBT_INFO("Suspend again the lazy guy while he's sleeping...");
63   MSG_process_suspend(lazy);
64   XBT_INFO("This time, don't let him finish his siesta.");
65   SLEEP(2.0);
66   XBT_INFO("Wake up, lazy guy!");
67   MSG_process_resume(lazy);
68
69   XBT_INFO("OK, goodbye now.");
70   return 0;
71 }
72
73 int main(int argc, char *argv[])
74 {
75   msg_error_t res = MSG_OK;
76
77   MSG_init(&argc, argv);
78   xbt_assert(argc > 1, "Usage: %s platform_file\n\tExample: %s msg_platform.xml\n", argv[0], argv[0]);
79
80   MSG_create_environment(argv[1]);  /** - Load the platform description */
81   MSG_function_register("dream_master", dream_master); /** - Create and deploy the @ref dream_master */
82   xbt_dynar_t hosts = MSG_hosts_as_dynar();
83   MSG_process_create("dream_master", dream_master, NULL, xbt_dynar_getfirst_as(hosts, msg_host_t));
84   xbt_dynar_free(&hosts);
85   res = MSG_main();                 /** - Run the simulation */
86
87   XBT_INFO("Simulation time %g", MSG_get_clock());
88   return res != MSG_OK;
89 }