Logo AND Algorithmique Numérique Distribuée

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