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_test, "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
17 * - <b>process-suspend/process-suspend.c: Process Suspend/Resume example</b>. Demonstrates how to suspend and resume
18 * processes using the @ref MSG_process_suspend and @ref MSG_process_resume functions.
21 /** Lazy guy function. This process suspends itself asap. */
22 static int lazy_guy(int argc, char *argv[])
24 XBT_INFO("Nobody's watching me ? Let's go to sleep.");
25 MSG_process_suspend(MSG_process_self());
26 XBT_INFO("Uuuh ? Did somebody call me ?");
28 XBT_INFO("Going to sleep...");
30 XBT_INFO("Mmm... waking up.");
32 XBT_INFO("Going to sleep one more time...");
34 XBT_INFO("Waking up once for all!");
36 XBT_INFO("Mmmh, goodbye now.");
40 /** Dream master function. This process creates a lazy_guy process and resumes it 10 seconds later. */
41 static int dream_master(int argc, char *argv[])
43 msg_process_t lazy = NULL;
45 XBT_INFO("Let's create a lazy guy.");
46 lazy = MSG_process_create("Lazy", lazy_guy, NULL, MSG_host_self());
47 XBT_INFO("Let's wait a little bit...");
49 XBT_INFO("Let's wake the lazy guy up! >:) BOOOOOUUUHHH!!!!");
50 MSG_process_resume(lazy);
53 XBT_INFO("Suspend the lazy guy while he's sleeping...");
54 MSG_process_suspend(lazy);
55 XBT_INFO("Let him finish his siesta.");
57 XBT_INFO("Wake up, lazy guy!");
58 MSG_process_resume(lazy);
61 XBT_INFO("Suspend again the lazy guy while he's sleeping...");
62 MSG_process_suspend(lazy);
63 XBT_INFO("This time, don't let him finish his siesta.");
65 XBT_INFO("Wake up, lazy guy!");
66 MSG_process_resume(lazy);
68 XBT_INFO("OK, goodbye now.");
70 } /* end_of_dream_master */
72 int main(int argc, char *argv[])
74 msg_error_t res = MSG_OK;
76 MSG_init(&argc, argv);
77 xbt_assert(argc > 1, "Usage: %s platform_file\n\tExample: %s msg_platform.xml\n", argv[0], argv[0]);
79 MSG_create_environment(argv[1]);
80 MSG_function_register("dream_master", dream_master);
81 xbt_dynar_t hosts = MSG_hosts_as_dynar();
82 MSG_process_create("dream_master", dream_master, NULL, xbt_dynar_getfirst_as(hosts, msg_host_t));
83 xbt_dynar_free(&hosts);
86 XBT_INFO("Simulation time %g", MSG_get_clock());