1 /* Copyright (c) 2013-2021. 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/actor.h"
8 #include "simgrid/engine.h"
9 #include "simgrid/host.h"
10 #include "simgrid/semaphore.h"
15 XBT_LOG_NEW_DEFAULT_CATEGORY(semaphores, "Messages specific for this example");
19 static void peer(int argc, char* argv[])
23 double wait_time = xbt_str_parse_double(argv[i], "Invalid wait time: %s");
25 sg_actor_sleep_for(wait_time);
26 XBT_INFO("Trying to acquire %d (%sblocking)", i, sg_sem_would_block(sem) ? "" : "not ");
27 // cover the two cases: with and without timeout
29 while (sg_sem_acquire_timeout(sem, 3.0))
30 XBT_INFO("Timeout.. Try again %d", i);
34 XBT_INFO("Acquired %d", i);
36 wait_time = xbt_str_parse_double(argv[i], "Invalid wait time: %s");
38 sg_actor_sleep_for(wait_time);
39 XBT_INFO("Releasing %d", i);
41 XBT_INFO("Released %d", i);
43 sg_actor_sleep_for(50);
47 int main(int argc, char* argv[])
49 simgrid_init(&argc, argv);
50 simgrid_load_platform(argv[1]);
52 sg_host_t h = sg_host_by_name("Fafard");
55 XBT_INFO("Semaphore initialized with capacity = %d", sg_sem_get_capacity(sem));
56 const char* aliceTimes[] = {"0", "1", "3", "5", "1", "2", "5", "0"};
57 const char* bobTimes[] = {"0.9", "1", "1", "2", "2", "0", "0", "5"};
59 sg_actor_create("Alice", h, peer, 8, aliceTimes);
60 sg_actor_create("Bob", h, peer, 8, bobTimes);
64 XBT_INFO("Finished\n");