X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/84e824746638f2b4c32c315bcfce655098d4e80b..3c6d61c2bd982819b041235ef2d5fa141c35e7f2:/examples/s4u/exec-waitany/s4u-exec-waitany.cpp diff --git a/examples/s4u/exec-waitany/s4u-exec-waitany.cpp b/examples/s4u/exec-waitany/s4u-exec-waitany.cpp new file mode 100644 index 0000000000..6597dc6d21 --- /dev/null +++ b/examples/s4u/exec-waitany/s4u-exec-waitany.cpp @@ -0,0 +1,62 @@ +/* Copyright (c) 2019. The SimGrid Team. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#include "simgrid/s4u.hpp" +#include +#include +#include + +XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_exec_waitany, "Messages specific for this s4u example"); + +static void worker(bool with_timeout) +{ + /* Vector in which we store all pending executions*/ + std::vector pending_executions; + + for (int i = 0; i < 3; i++) { + std::string name = std::string("Exec-") + std::to_string(i); + double amount = (6 * (i % 2) + i + 1) * simgrid::s4u::this_actor::get_host()->get_speed(); + + simgrid::s4u::ExecPtr exec = simgrid::s4u::this_actor::exec_init(amount)->set_name(name); + pending_executions.push_back(exec); + exec->start(); + + XBT_INFO("Activity %s has started for %.0f seconds", name.c_str(), + amount / simgrid::s4u::this_actor::get_host()->get_speed()); + } + + /* Now that executions were initiated, wait for their completion, in order of termination. + * + * This loop waits for first terminating execution with wait_any() and remove it with erase(), until all execs are + * terminated. + */ + while (not pending_executions.empty()) { + int pos; + if (with_timeout) + pos = simgrid::s4u::Exec::wait_any_for(&pending_executions, 4); + else + pos = simgrid::s4u::Exec::wait_any(&pending_executions); + + if (pos < 0) { + XBT_INFO("Do not wait any longer for an activity"); + pending_executions.clear(); + } else { + XBT_INFO("Activity '%s' (at position %d) is complete", pending_executions[pos]->get_cname(), pos); + pending_executions.erase(pending_executions.begin() + pos); + } + XBT_INFO("%lu activities remain pending", pending_executions.size()); + } +} + +int main(int argc, char* argv[]) +{ + simgrid::s4u::Engine e(&argc, argv); + e.load_platform(argv[1]); + simgrid::s4u::Actor::create("worker", simgrid::s4u::Host::by_name("Tremblay"), worker, false); + simgrid::s4u::Actor::create("worker_timeout", simgrid::s4u::Host::by_name("Tremblay"), worker, true); + e.run(); + + return 0; +}