Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
coverage madness in examples
[simgrid.git] / examples / msg / suspend / 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"            /* Yeah! If you want to use msg, you need to include simgrid/msg.h */
8 #include "xbt/sysdep.h"         /* calloc */
9
10 /* Create a log channel to have nice outputs. */
11 #include "xbt/log.h"
12 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
13                              "Messages specific for this msg example");
14
15 #define SLEEP(duration)                                 \
16   if (MSG_process_sleep(duration) != MSG_OK)            \
17     xbt_die("What's going on??? I failed to sleep!");   \
18   else                                                  \
19     (void)0
20
21 /** @addtogroup MSG_examples
22  * 
23  * - <b>suspend/suspend.c</b>: Demonstrates how to suspend and resume processes using @ref MSG_process_suspend and @ref MSG_process_resume.
24  */
25
26 /** Lazy guy function. This process suspends itself asap.  */
27 static int lazy_guy(int argc, char *argv[])
28 {
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 ?");
32
33   XBT_INFO("Going to sleep...");
34   SLEEP(10.0);
35   XBT_INFO("Mmm... waking up.");
36
37   XBT_INFO("Going to sleep one more time...");
38   SLEEP(10.0);
39   XBT_INFO("Waking up once for all!");
40
41   XBT_INFO("Mmmh, goodbye now.");
42   return 0;
43 }                               /* end_of_lazy_guy */
44
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[])
48 {
49   msg_process_t lazy = NULL;
50
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...");
54   SLEEP(10.0);
55   XBT_INFO("Let's wake the lazy guy up! >:) BOOOOOUUUHHH!!!!");
56   MSG_process_resume(lazy);
57
58   SLEEP(5.0);
59   XBT_INFO("Suspend the lazy guy while he's sleeping...");
60   MSG_process_suspend(lazy);
61   XBT_INFO("Let him finish his siesta.");
62   SLEEP(10.0);
63   XBT_INFO("Wake up, lazy guy!");
64   MSG_process_resume(lazy);
65
66   SLEEP(5.0);
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.");
70   SLEEP(2.0);
71   XBT_INFO("Wake up, lazy guy!");
72   MSG_process_resume(lazy);
73
74   XBT_INFO("OK, goodbye now.");
75   return 0;
76 }                               /* end_of_dram_master */
77
78 /** Test function */
79 static msg_error_t test_all(const char *platform_file,
80                             const char *application_file)
81 {
82   msg_error_t res = MSG_OK;
83
84   {                             /*  Simulation setting */
85     MSG_create_environment(platform_file);
86   }
87   {                             /*   Application deployment */
88     MSG_function_register("dream_master", dream_master);
89     MSG_launch_application(application_file);
90   }
91   res = MSG_main();
92
93   XBT_INFO("Simulation time %g", MSG_get_clock());
94   return res;
95 }                               /* end_of_test_all */
96
97
98 /** Main function */
99 int main(int argc, char *argv[])
100 {
101   msg_error_t res = MSG_OK;
102
103   MSG_init(&argc, argv);
104   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
105                   "\tExample: %s msg_platform.xml msg_deployment_suspend.xml\n", 
106                   argv[0], argv[0]);
107
108   test_all(argv[1], argv[2]);
109
110   return res != MSG_OK;
111 }