1 /* Copyright (c) 2010-2019. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include "simgrid/msg.h"
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
11 static int slave(int argc, char *argv[])
13 msg_task_t task = NULL;
14 XBT_ATTRIB_UNUSED int res;
16 const char * mailbox = "jupi";
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");
24 xbt_assert(res == MSG_OK, "MSG_task_get failed");
26 if (!strcmp(MSG_task_get_name(task), "finalize")) {
27 MSG_task_destroy(task);
30 MSG_task_execute(task);
31 XBT_INFO("Task \"%s\" done", MSG_task_get_name(task));
33 MSG_task_destroy(task);
37 XBT_INFO("I'm done. See you!");
41 static int master(int argc, char *argv[])
43 double task_comp_size = 5E7;
44 double task_comm_size = 1E6;
46 const char * mailbox = "jupi";
47 msg_host_t jupiter = MSG_host_by_name("Jupiter");
49 msg_task_t task = MSG_task_create("task on", task_comp_size, task_comm_size, NULL);
50 XBT_INFO("Sending \"%s\"", MSG_task_get_name(task));
51 if (MSG_task_send_with_timeout(task, mailbox, 1) != MSG_OK)
52 MSG_task_destroy(task);
55 MSG_host_off(jupiter);
57 task = MSG_task_create("task off", task_comp_size, task_comm_size, NULL);
58 XBT_INFO("Sending \"%s\"", MSG_task_get_name(task));
59 if (MSG_task_send_with_timeout(task, mailbox, 1) != MSG_OK)
60 MSG_task_destroy(task);
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;
68 xbt_dynar_foreach (jupi_processes, cursor, process) {
69 MSG_process_kill(process);
71 xbt_dynar_free(&jupi_processes);
73 task = MSG_task_create("task on without proc", task_comp_size, task_comm_size, NULL);
74 XBT_INFO("Sending \"%s\"", MSG_task_get_name(task));
75 if (MSG_task_send_with_timeout(task, mailbox, 1) != MSG_OK)
76 MSG_task_destroy(task);
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);
82 task = MSG_task_create("task on with proc", task_comp_size, task_comm_size, NULL);
83 XBT_INFO("Sending \"%s\"", MSG_task_get_name(task));
84 if (MSG_task_send_with_timeout(task, mailbox, 1) != MSG_OK)
85 MSG_task_destroy(task);
87 task = MSG_task_create("finalize", 0, 0, 0);
88 XBT_INFO("Sending \"%s\"", MSG_task_get_name(task));
89 if (MSG_task_send_with_timeout(task, mailbox, 1) != MSG_OK)
90 MSG_task_destroy(task);
92 XBT_INFO("Goodbye now!");
96 int main(int argc, char *argv[])
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]);
103 MSG_create_environment(argv[1]);
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"));
110 XBT_INFO("Simulation time %g", MSG_get_clock());
112 return res != MSG_OK;