Logo AND Algorithmique Numérique Distribuée

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