Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of github.com:simgrid/simgrid
[simgrid.git] / teshsuite / msg / host_on_off / host_on_off.c
1 /* Copyright (c) 2010-2018. 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     if (res == MSG_HOST_FAILURE) {
21       XBT_DEBUG("The host has been turned off, this was expected");
22       return 1;
23     }
24     xbt_assert(res == MSG_OK, "MSG_task_get failed");
25
26     if (!strcmp(MSG_task_get_name(task), "finalize")) {
27       MSG_task_destroy(task);
28       break;
29     }
30     MSG_task_execute(task);
31     XBT_INFO("Task \"%s\" done", MSG_task_get_name(task));
32
33     MSG_task_destroy(task);
34     task = NULL;
35     id--;
36   }
37   XBT_INFO("I'm done. See you!");
38   return 0;
39 }
40
41 static int master(int argc, char *argv[])
42 {
43   double task_comp_size = 5E7;
44   double task_comm_size = 1E6;
45
46   const char * mailbox = "jupi";
47   msg_host_t jupiter = MSG_host_by_name("Jupiter");
48
49   msg_task_t 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
64   xbt_dynar_t jupi_processes = xbt_dynar_new(sizeof(msg_process_t), NULL);
65   MSG_host_get_process_list(jupiter, jupi_processes);
66   msg_process_t process = NULL;
67   unsigned int cursor;
68   xbt_dynar_foreach (jupi_processes, cursor, process) {
69     MSG_process_kill(process);
70   }
71   xbt_dynar_free(&jupi_processes);
72
73   task = MSG_task_create("task on without proc", task_comp_size, task_comm_size, NULL);
74   XBT_INFO("Sending \"%s\"", task->name);
75   if (MSG_task_send_with_timeout(task, mailbox, 1) != MSG_OK)
76     MSG_task_destroy(task);
77
78   char **argvF = xbt_new(char*, 2);
79   argvF[0] = xbt_strdup("slave");
80   MSG_process_create_with_arguments("slave", slave, NULL, MSG_host_by_name("Jupiter"), 1, argvF);
81
82   task = MSG_task_create("task on with proc", task_comp_size, task_comm_size, NULL);
83   XBT_INFO("Sending \"%s\"", task->name);
84   if (MSG_task_send_with_timeout(task, mailbox, 1) != MSG_OK)
85     MSG_task_destroy(task);
86
87   task = MSG_task_create("finalize", 0, 0, 0);
88   XBT_INFO("Sending \"%s\"", task->name);
89   if (MSG_task_send_with_timeout(task, mailbox, 1) != MSG_OK)
90     MSG_task_destroy(task);
91
92   XBT_INFO("Goodbye now!");
93   return 0;
94 }
95
96 int main(int argc, char *argv[])
97 {
98   msg_error_t res;
99
100   MSG_init(&argc, argv);
101   xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s msg_platform.xml\n", argv[0], argv[0]);
102
103   MSG_create_environment(argv[1]);
104
105   MSG_process_create("master", master, NULL, MSG_get_host_by_name("Tremblay"));
106   MSG_process_create("slave", slave, NULL, MSG_get_host_by_name("Jupiter"));
107
108   res = MSG_main();
109
110   XBT_INFO("Simulation time %g", MSG_get_clock());
111
112   return res != MSG_OK;
113 }