1 /* Copyright (c) 2013-2022. 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");
25 sg_actor_sleep_for(wait_time);
26 // cover the two cases: with and without timeout
28 XBT_INFO("Trying for 1 sec to acquire #%d (that is %sfree)", i, sg_sem_would_block(sem) ? "not " : "");
29 while (sg_sem_acquire_timeout(sem, 1.)) {
30 XBT_INFO("Timeout.. Try #%d for another second.", i);
33 XBT_INFO("Acquire #%d (that is %sfree)", i, sg_sem_would_block(sem) ? "not " : "");
36 XBT_INFO("Acquired #%d", i);
38 wait_time = xbt_str_parse_double(argv[i], "Invalid wait time");
40 sg_actor_sleep_for(wait_time);
41 XBT_INFO("Releasing #%d", i);
43 XBT_INFO("Released #%d", i);
45 sg_actor_sleep_for(50);
49 int main(int argc, char* argv[])
51 simgrid_init(&argc, argv);
52 simgrid_load_platform(argv[1]);
54 sg_host_t h = sg_host_by_name("Fafard");
57 XBT_INFO("Semaphore initialized with capacity = %d", sg_sem_get_capacity(sem));
58 const char* aliceTimes[] = {"0", "1", "3", "5", "1", "2", "5", "0"};
59 const char* bobTimes[] = {"0.9", "1", "1", "2", "2", "0", "0", "5"};
61 sg_actor_create_("Alice", h, peer, 8, aliceTimes);
62 sg_actor_create_("Bob", h, peer, 8, bobTimes);
66 XBT_INFO("Finished\n");