1 /* Copyright (c) 2008-2015. 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_app_token_ring, "Messages specific for this msg example");
11 /** @addtogroup MSG_examples
13 * - <b>Token Ring: app-token-ring/app-token-ring.c</b>. Classical token ring communication, where a token is exchanged
14 * along a ring to reach every participant. The tesh file laying in the directory shows how to run the same example on
15 * different platforms.
18 /** Single function for all hosts */
19 static int foo(int argc, char *argv[])
21 unsigned int task_comm_size = 1000000; /** The token is 1MB long*/
22 int rank = xbt_str_parse_int(MSG_process_get_name(MSG_process_self()), "Invalid process name: %s");
24 msg_task_t task = NULL;
25 XBT_ATTRIB_UNUSED int res;
26 if (rank == 0){ /** - The root (rank 0) first sends the token then waits to receive it back */
27 sprintf(mailbox, "%d", rank+1);
28 task = MSG_task_create("Token", 0, task_comm_size, NULL);
29 XBT_INFO("Host \"%d\" send '%s' to Host \"%s\"", rank, task->name,mailbox);
30 MSG_task_send(task, mailbox);
32 res = MSG_task_receive(&(task), MSG_process_get_name(MSG_process_self()));
33 xbt_assert(res == MSG_OK, "MSG_task_get failed");
34 XBT_INFO("Host \"%d\" received \"%s\"", rank, MSG_task_get_name(task));
35 MSG_task_destroy(task);
36 } else{ /** - The others receive from their left neighbor (rank-1) and send to their right neighbor (rank+1) */
37 res = MSG_task_receive(&(task), MSG_process_get_name(MSG_process_self()));
38 xbt_assert(res == MSG_OK, "MSG_task_get failed");
39 XBT_INFO("Host \"%d\" received \"%s\"",rank, MSG_task_get_name(task));
41 if(rank+1 == MSG_get_host_number()) /** - Except for the last one which sends the token back to rank 0 */
42 sprintf(mailbox, "0");
44 sprintf(mailbox, "%d", rank+1);
45 XBT_INFO("Host \"%d\" send '%s' to Host \"%s\"",rank,task->name,mailbox);
46 MSG_task_send(task, mailbox);
51 int main(int argc, char *argv[])
54 MSG_init(&argc, argv);
55 MSG_create_environment(argv[1]); /** - Load the platform description */
56 xbt_dynar_t hosts = MSG_hosts_as_dynar();
59 XBT_INFO("Number of host '%d'",MSG_get_host_number());
60 xbt_dynar_foreach (hosts, i, h){ /** - Give a unique rank to each host and create a @ref foo process on each */
61 char* name_host = bprintf("%u",i);
62 MSG_process_create(name_host, foo, NULL, h);
65 xbt_dynar_free(&hosts);
67 int res = MSG_main(); /** - Run the simulation */
68 XBT_INFO("Simulation time %g", MSG_get_clock());