Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #65 from fabienchaix/master
[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"
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "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  * 
17  * - <b>suspend/suspend.c</b>: Demonstrates how to suspend and resume processes using
18  *   @ref MSG_process_suspend and @ref MSG_process_resume.
19  */
20
21 /** Lazy guy function. This process suspends itself asap.  */
22 static int lazy_guy(int argc, char *argv[])
23 {
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 ?");
27
28   XBT_INFO("Going to sleep...");
29   SLEEP(10.0);
30   XBT_INFO("Mmm... waking up.");
31
32   XBT_INFO("Going to sleep one more time...");
33   SLEEP(10.0);
34   XBT_INFO("Waking up once for all!");
35
36   XBT_INFO("Mmmh, goodbye now.");
37   return 0;
38 }                               /* end_of_lazy_guy */
39
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[])
42 {
43   msg_process_t lazy = NULL;
44
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...");
48   SLEEP(10.0);
49   XBT_INFO("Let's wake the lazy guy up! >:) BOOOOOUUUHHH!!!!");
50   MSG_process_resume(lazy);
51
52   SLEEP(5.0);
53   XBT_INFO("Suspend the lazy guy while he's sleeping...");
54   MSG_process_suspend(lazy);
55   XBT_INFO("Let him finish his siesta.");
56   SLEEP(10.0);
57   XBT_INFO("Wake up, lazy guy!");
58   MSG_process_resume(lazy);
59
60   SLEEP(5.0);
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.");
64   SLEEP(2.0);
65   XBT_INFO("Wake up, lazy guy!");
66   MSG_process_resume(lazy);
67
68   XBT_INFO("OK, goodbye now.");
69   return 0;
70 }                               /* end_of_dream_master */
71
72 int main(int argc, char *argv[])
73 {
74   msg_error_t res = MSG_OK;
75
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]);
78
79   MSG_create_environment(argv[1]);
80   MSG_function_register("dream_master", dream_master);
81   MSG_process_create("dream_master", dream_master, NULL, xbt_dynar_getfirst_as(MSG_hosts_as_dynar(), msg_host_t));
82   res = MSG_main();
83
84   XBT_INFO("Simulation time %g", MSG_get_clock());
85   return res != MSG_OK;
86 }