Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / teshsuite / msg / host_on_off / host_on_off.c
1 /* Copyright (c) 2010-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 static int slave(int argc, char *argv[])
12 {
13   msg_task_t task = NULL;
14   XBT_ATTRIB_UNUSED int res;
15   int id = -1;
16   const char * mailbox = "jupi";
17
18   while (1) {
19     res = MSG_task_receive(&(task), mailbox);
20     xbt_assert(res == MSG_OK, "MSG_task_get failed");
21
22     if (!strcmp(MSG_task_get_name(task), "finalize")) {
23       MSG_task_destroy(task);
24       break;
25     }
26     MSG_task_execute(task);
27     XBT_INFO("Task \"%s\" done", MSG_task_get_name(task));
28
29     MSG_task_destroy(task);
30     task = NULL;
31     id--;
32   }
33   XBT_INFO("I'm done. See you!");
34   return 0;
35 }
36
37 static int master(int argc, char *argv[])
38 {
39   double task_comp_size = 5E7;
40   double task_comm_size = 1E6;
41
42   const char * mailbox = "jupi";
43   msg_task_t task = NULL;
44   msg_host_t jupiter = MSG_host_by_name("Jupiter");
45
46   task = MSG_task_create("task on", task_comp_size, task_comm_size, NULL);
47   XBT_INFO("Sending \"%s\"", task->name);
48   if (MSG_task_send_with_timeout(task, mailbox, 1) != MSG_OK)
49     MSG_task_destroy(task);
50
51   MSG_process_sleep(1);
52   MSG_host_off(jupiter);
53
54   task = MSG_task_create("task off", task_comp_size, task_comm_size, NULL);
55   XBT_INFO("Sending \"%s\"", task->name);
56   if (MSG_task_send_with_timeout(task, mailbox, 1) != MSG_OK)
57     MSG_task_destroy(task);
58
59   MSG_host_on(jupiter);
60   xbt_swag_t jupi_processes = MSG_host_get_process_list(jupiter);
61   void *process;
62   xbt_swag_foreach(process, jupi_processes) {
63     MSG_process_kill(process);
64   }
65
66   task = MSG_task_create("task on without proc", task_comp_size, task_comm_size, NULL);
67   XBT_INFO("Sending \"%s\"", task->name);
68   if (MSG_task_send_with_timeout(task, mailbox, 1) != MSG_OK)
69     MSG_task_destroy(task);
70
71   char **argvF = xbt_new(char*, 2);
72   argvF[0] = xbt_strdup("slave");
73   MSG_process_create_with_arguments("slave", slave, NULL, MSG_host_by_name("Jupiter"), 1, argvF);
74
75   task = MSG_task_create("task on with proc", task_comp_size, task_comm_size, NULL);
76   XBT_INFO("Sending \"%s\"", task->name);
77   if (MSG_task_send_with_timeout(task, mailbox, 1) != MSG_OK)
78     MSG_task_destroy(task);
79
80   task = MSG_task_create("finalize", 0, 0, 0);
81   XBT_INFO("Sending \"%s\"", task->name);
82   if (MSG_task_send_with_timeout(task, mailbox, 1) != MSG_OK)
83     MSG_task_destroy(task);
84
85   XBT_INFO("Goodbye now!");
86   return 0;
87 }
88
89 int main(int argc, char *argv[])
90 {
91   msg_error_t res;
92
93   MSG_init(&argc, argv);
94   xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s msg_platform.xml\n", argv[0], argv[0]);
95
96   MSG_create_environment(argv[1]);
97
98   MSG_process_create("master", master, NULL, MSG_get_host_by_name("Tremblay"));
99   MSG_process_create("slave", slave, NULL, MSG_get_host_by_name("Jupiter"));
100
101   res = MSG_main();
102
103   XBT_INFO("Simulation time %g", MSG_get_clock());
104
105   return res != MSG_OK;
106 }