1 /* Copyright (c) 2010-2020. 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"
11 #include "xbt/asserts.h"
17 XBT_LOG_NEW_DEFAULT_CATEGORY(async_wait, "Messages specific for this example");
19 /* Main function of the Sender process */
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: %s"); /* - number of messages */
24 double message_size = xbt_str_parse_double(argv[2], "Invalid message size: %s"); /* - communication cost */
25 double sleep_start_time = xbt_str_parse_double(argv[3], "Invalid sleep start time: %s"); /* - start time */
26 double sleep_test_time = xbt_str_parse_double(argv[4], "Invalid test time: %s"); /* - 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 sg_comm_t comm = sg_mailbox_put_async(mailbox, xbt_strdup("finalize"), 0);
49 XBT_INFO("Send 'finalize' to 'receiver'");
51 if (sleep_test_time > 0) {
52 while (sg_comm_test(comm) == 0) {
53 sg_actor_sleep_for(sleep_test_time);
60 /* Receiver process expects 3 arguments: */
61 static void receiver(int argc, char* argv[])
63 xbt_assert(argc == 3, "The relay_runner function does not accept any parameter from the XML deployment file");
64 double sleep_start_time = xbt_str_parse_double(argv[1], "Invalid sleep start parameter: %s"); /* - start time */
65 double sleep_test_time = xbt_str_parse_double(argv[2], "Invalid sleep test parameter: %s"); /* - test time */
66 XBT_INFO("sleep_start_time : %f , sleep_test_time : %f", sleep_start_time, sleep_test_time);
68 sg_actor_sleep_for(sleep_start_time); /* This actor first sleeps for "start time" seconds. */
70 sg_mailbox_t mailbox = sg_mailbox_by_name("receiver");
71 void* received = NULL;
73 XBT_INFO("Wait for my first message");
75 /* Then it posts asynchronous receives (@ref sg_mailbox_get_async) and*/
76 sg_comm_t comm = sg_mailbox_get_async(mailbox, &received);
78 if (sleep_test_time > 0) { /* - if "test_time" is set to 0, wait on @ref sg_comm_wait */
79 while (sg_comm_test(comm) == 0) { /* - Call @ref sg_comm_test every "sleep_test_time" otherwise */
80 sg_actor_sleep_for(sleep_test_time);
85 XBT_INFO("I got a '%s'.", (char*)received);
87 if (strcmp((char*)received, "finalize") == 0) { /* If the received task is "finalize", the actor ends */
95 int main(int argc, char* argv[])
97 simgrid_init(&argc, argv);
99 "Usage: %s platform_file deployment_file\n"
100 "\tExample: %s msg_platform.xml msg_deployment.xml\n",
103 simgrid_load_platform(argv[1]); /* - Load the platform description */
105 simgrid_register_function("sender", sender);
106 simgrid_register_function("receiver", receiver);
107 simgrid_load_deployment(argv[2]); /* - Deploy the sender and receiver actors */
109 simgrid_run(); /* - Run the simulation */