Logo AND Algorithmique Numérique Distribuée

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