1 /* Copyright (c) 2019-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/engine.h"
8 #include "simgrid/exec.h"
9 #include "simgrid/host.h"
11 #include "xbt/asserts.h"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(exec_waitany, "Messages specific for this example");
16 static void worker(int argc, char* argv[])
18 int with_timeout = !strcmp(argv[1], "true");
20 /* Vector in which we store all pending executions*/
21 sg_exec_t* pending_execs = malloc(sizeof(sg_exec_t) * 3);
22 int pending_execs_count = 0;
24 for (int i = 0; i < 3; i++) {
25 char* name = bprintf("Exec-%d", i);
26 double amount = (6 * (i % 2) + i + 1) * sg_host_speed(sg_host_self());
28 sg_exec_t exec = sg_actor_exec_init(amount);
29 sg_exec_set_name(exec, name);
30 pending_execs[pending_execs_count++] = exec;
33 XBT_INFO("Activity %s has started for %.0f seconds", name, amount / sg_host_speed(sg_host_self()));
37 /* Now that executions were initiated, wait for their completion, in order of termination.
39 * This loop waits for first terminating execution with wait_any() and remove it with erase(), until all execs are
42 while (pending_execs_count > 0) {
45 pos = sg_exec_wait_any_for(pending_execs, pending_execs_count, 4);
47 pos = sg_exec_wait_any(pending_execs, pending_execs_count);
50 XBT_INFO("Do not wait any longer for an activity");
51 pending_execs_count = 0;
53 XBT_INFO("Activity at position %d is complete", pos);
54 memmove(pending_execs + pos, pending_execs + pos + 1, sizeof(sg_exec_t) * (pending_execs_count - pos - 1));
55 pending_execs_count--;
57 XBT_INFO("%d activities remain pending", pending_execs_count);
63 int main(int argc, char* argv[])
65 simgrid_init(&argc, argv);
66 simgrid_load_platform(argv[1]);
68 const char* worker_argv[] = {"worker", "false"};
69 sg_actor_create("worker", sg_host_by_name("Tremblay"), worker, 2, worker_argv);
71 worker_argv[1] = "true";
72 sg_actor_create("worker_timeout", sg_host_by_name("Tremblay"), worker, 2, worker_argv);