1 /* Copyright (c) 2007, 2009-2014. 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" /* Yeah! If you want to use msg, you need to include simgrid/msg.h */
8 #include "xbt/sysdep.h" /* calloc */
10 /* Create a log channel to have nice outputs. */
12 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
13 "Messages specific for this msg example");
15 #define SLEEP(duration) \
16 if (MSG_process_sleep(duration) != MSG_OK) \
17 xbt_die("What's going on??? I failed to sleep!"); \
21 /** @addtogroup MSG_examples
23 * - <b>suspend/suspend.c</b>: Demonstrates how to suspend and resume processes using @ref MSG_process_suspend and @ref MSG_process_resume.
26 /** Lazy guy function. This process suspends itself asap. */
27 static int lazy_guy(int argc, char *argv[])
29 XBT_INFO("Nobody's watching me ? Let's go to sleep.");
30 MSG_process_suspend(MSG_process_self());
31 XBT_INFO("Uuuh ? Did somebody call me ?");
33 XBT_INFO("Going to sleep...");
35 XBT_INFO("Mmm... waking up.");
37 XBT_INFO("Going to sleep one more time...");
39 XBT_INFO("Waking up once for all!");
41 XBT_INFO("Mmmh, goodbye now.");
43 } /* end_of_lazy_guy */
45 /** Dream master function. This process creates a lazy_guy process and
46 resumes it 10 seconds later. */
47 static int dream_master(int argc, char *argv[])
49 msg_process_t lazy = NULL;
51 XBT_INFO("Let's create a lazy guy.");
52 lazy = MSG_process_create("Lazy", lazy_guy, NULL, MSG_host_self());
53 XBT_INFO("Let's wait a little bit...");
55 XBT_INFO("Let's wake the lazy guy up! >:) BOOOOOUUUHHH!!!!");
56 MSG_process_resume(lazy);
59 XBT_INFO("Suspend the lazy guy while he's sleeping...");
60 MSG_process_suspend(lazy);
61 XBT_INFO("Let him finish his siesta.");
63 XBT_INFO("Wake up, lazy guy!");
64 MSG_process_resume(lazy);
67 XBT_INFO("Suspend again the lazy guy while he's sleeping...");
68 MSG_process_suspend(lazy);
69 XBT_INFO("This time, don't let him finish his siesta.");
71 XBT_INFO("Wake up, lazy guy!");
72 MSG_process_resume(lazy);
74 XBT_INFO("OK, goodbye now.");
76 } /* end_of_dram_master */
79 static msg_error_t test_all(const char *platform_file,
80 const char *application_file)
82 msg_error_t res = MSG_OK;
84 { /* Simulation setting */
85 MSG_create_environment(platform_file);
87 { /* Application deployment */
88 MSG_function_register("dream_master", dream_master);
89 MSG_launch_application(application_file);
93 XBT_INFO("Simulation time %g", MSG_get_clock());
95 } /* end_of_test_all */
99 int main(int argc, char *argv[])
101 msg_error_t res = MSG_OK;
103 MSG_init(&argc, argv);
105 XBT_CRITICAL("Usage: %s platform_file deployment_file\n", argv[0]);
106 XBT_CRITICAL("example: %s msg_platform.xml msg_deployment_suspend.xml\n",
110 test_all(argv[1], argv[2]);