1 /* Copyright (c) 2010-2021. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "simgrid/actor.h"
7 #include "simgrid/comm.h"
8 #include "simgrid/engine.h"
9 #include "simgrid/mailbox.h"
13 #include "xbt/sysdep.h"
17 XBT_LOG_NEW_DEFAULT_CATEGORY(comm_wait, "Messages specific for this example");
19 /* Main function of the Sender actor */
20 static void sender(int argc, char* argv[])
22 xbt_assert(argc == 5, "The sender function expects 4 arguments from the XML deployment file");
23 long messages_count = xbt_str_parse_int(argv[1], "Invalid amount of messages"); /* - number of messages */
24 long message_size = xbt_str_parse_int(argv[2], "Invalid message size"); /* - communication cost */
25 double sleep_start_time = xbt_str_parse_double(argv[3], "Invalid sleep start time"); /* - start time */
26 double sleep_test_time = xbt_str_parse_double(argv[4], "Invalid test time"); /* - test time */
28 XBT_INFO("sleep_start_time : %f , sleep_test_time : %f", sleep_start_time, sleep_test_time);
29 sg_mailbox_t mailbox = sg_mailbox_by_name("receiver");
31 sg_actor_sleep_for(sleep_start_time);
32 for (int i = 0; i < messages_count; i++) {
33 char* payload = bprintf("Message %d", i);
35 /* This actor first sends a message asynchronously with @ref sg_mailbox_put_async. Then, if: */
36 sg_comm_t comm = sg_mailbox_put_async(mailbox, payload, message_size);
37 XBT_INFO("Send '%s' to 'receiver'", payload);
39 if (sleep_test_time > 0) { /* - "test_time" is set to 0, wait on @ref sg_comm_wait */
40 while (sg_comm_test(comm) == 0) { /* - Call @ref sg_comm_test every "sleep_test_time" otherwise */
41 sg_actor_sleep_for(sleep_test_time);
48 XBT_INFO("Send 'finalize' to 'receiver'");
49 sg_mailbox_put(mailbox, xbt_strdup("finalize"), 0);
52 /* Receiver actor expects 3 arguments: */
53 static void receiver(int argc, char* argv[])
55 xbt_assert(argc == 3, "The relay_runner function does not accept any parameter from the XML deployment file");
56 double sleep_start_time = xbt_str_parse_double(argv[1], "Invalid sleep start parameter"); /* - start time */
57 double sleep_test_time = xbt_str_parse_double(argv[2], "Invalid sleep test parameter"); /* - test time */
58 XBT_INFO("sleep_start_time : %f , sleep_test_time : %f", sleep_start_time, sleep_test_time);
60 sg_actor_sleep_for(sleep_start_time); /* This actor first sleeps for "start time" seconds. */
62 sg_mailbox_t mailbox = sg_mailbox_by_name("receiver");
63 void* received = NULL;
65 XBT_INFO("Wait for my first message");
67 /* Then it posts asynchronous receives (@ref sg_mailbox_get_async) and*/
68 sg_comm_t comm = sg_mailbox_get_async(mailbox, &received);
70 if (sleep_test_time > 0) { /* - if "test_time" is set to 0, wait on @ref sg_comm_wait */
71 while (sg_comm_test(comm) == 0) { /* - Call @ref sg_comm_test every "sleep_test_time" otherwise */
72 sg_actor_sleep_for(sleep_test_time);
77 XBT_INFO("I got a '%s'.", (char*)received);
79 if (strcmp((char*)received, "finalize") == 0) { /* If the received task is "finalize", the actor ends */
87 int main(int argc, char* argv[])
89 simgrid_init(&argc, argv);
91 "Usage: %s platform_file deployment_file\n"
92 "\tExample: %s msg_platform.xml msg_deployment.xml\n",
95 simgrid_load_platform(argv[1]); /* - Load the platform description */
97 simgrid_register_function("sender", sender);
98 simgrid_register_function("receiver", receiver);
99 simgrid_load_deployment(argv[2]); /* - Deploy the sender and receiver actors */
101 simgrid_run(); /* - Run the simulation */