Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge remote-tracking branch 'origin/master'
[simgrid.git] / examples / msg / suspend / suspend.c
1 /* Copyright (c) 2007, 2009, 2010. 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 "msg/msg.h"            /* Yeah! If you want to use msg, you need to include msg/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 /** @addtogroup MSG_examples
16  * 
17  * - <b>suspend/suspend.c</b>: Demonstrates how to suspend and resume processes using @ref MSG_process_suspend and @ref MSG_process_resume.
18  */
19
20 /** Lazy guy function. This process suspends itself asap.  */
21 static int lazy_guy(int argc, char *argv[])
22 {
23   XBT_INFO("Nobody's watching me ? Let's go to sleep.");
24   MSG_process_suspend(MSG_process_self());
25   XBT_INFO("Uuuh ? Did somebody call me ?");
26   XBT_INFO("Mmmh, goodbye now.");
27   return 0;
28 }                               /* end_of_lazy_guy */
29
30 /** Dream master function. This process creates a lazy_guy process and
31     resumes it 10 seconds later. */
32 static int dream_master(int argc, char *argv[])
33 {
34   msg_process_t lazy = NULL;
35
36   XBT_INFO("Let's create a lazy guy.");
37   lazy = MSG_process_create("Lazy", lazy_guy, NULL, MSG_host_self());
38   XBT_INFO("Let's wait a little bit...");
39   MSG_process_sleep(10.0);
40   XBT_INFO("Let's wake the lazy guy up! >:) BOOOOOUUUHHH!!!!");
41   MSG_process_resume(lazy);
42   XBT_INFO("OK, goodbye now.");
43   return 0;
44 }                               /* end_of_dram_master */
45
46 /** Test function */
47 static msg_error_t test_all(const char *platform_file,
48                             const char *application_file)
49 {
50   msg_error_t res = MSG_OK;
51
52   {                             /*  Simulation setting */
53     MSG_create_environment(platform_file);
54   }
55   {                             /*   Application deployment */
56     MSG_function_register("dream_master", dream_master);
57     MSG_launch_application(application_file);
58   }
59   res = MSG_main();
60
61   XBT_INFO("Simulation time %g", MSG_get_clock());
62   return res;
63 }                               /* end_of_test_all */
64
65
66 /** Main function */
67 int main(int argc, char *argv[])
68 {
69   msg_error_t res = MSG_OK;
70
71   MSG_init(&argc, argv);
72   if (argc < 3) {
73     XBT_CRITICAL("Usage: %s platform_file deployment_file\n", argv[0]);
74     XBT_CRITICAL("example: %s msg_platform.xml msg_deployment_suspend.xml\n",
75               argv[0]);
76     exit(1);
77   }
78   test_all(argv[1], argv[2]);
79
80   if (res == MSG_OK)
81     return 0;
82   else
83     return 1;
84 }                               /* end_of_main */