1 /* Copyright (c) 2007, 2009-2015. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include "simgrid/msg.h"
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_process_suspend, "Messages specific for this msg example");
11 #define SLEEP(duration) \
12 if (MSG_process_sleep(duration) != MSG_OK) \
13 xbt_die("What's going on??? I failed to sleep!");
15 /** @addtogroup MSG_examples
16 * @section msg_ex_process Acting on Processes
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.
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[])
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 ?");
29 XBT_INFO("Going to sleep..."); /** - Then repetitively go to sleep, but got awaken */
31 XBT_INFO("Mmm... waking up.");
33 XBT_INFO("Going to sleep one more time...");
35 XBT_INFO("Waking up once for all!");
37 XBT_INFO("Mmmh, goodbye now.");
41 /** The Dream master: */
42 static int dream_master(int argc, char *argv[])
44 msg_process_t lazy = NULL;
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 */
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 */
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.");
66 XBT_INFO("Wake up, lazy guy!");
67 MSG_process_resume(lazy);
69 XBT_INFO("OK, goodbye now.");
73 int main(int argc, char *argv[])
75 msg_error_t res = MSG_OK;
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]);
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 */
87 XBT_INFO("Simulation time %g", MSG_get_clock());