Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / examples / msg / synchro / synchro.c
1 /* Copyright (c) 2013-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include "simgrid/msg.h"
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_semaphore_example, "Messages specific for this msg example");
10
11 msg_sem_t sem;
12
13 static int peer(int argc, char* argv[]){
14   int i = 0; 
15   while(i < argc) {
16     double wait_time = xbt_str_parse_double(argv[i++],"Invalid wait time: %s");
17     MSG_process_sleep(wait_time);
18     XBT_INFO("Trying to acquire %d", i);
19     MSG_sem_acquire(sem);
20     XBT_INFO("Acquired %d", i);
21
22     wait_time = xbt_str_parse_double(argv[i++], "Invalid wait time: %s");
23     MSG_process_sleep(wait_time);
24     XBT_INFO("Releasing %d", i);
25     MSG_sem_release(sem);
26     XBT_INFO("Released %d", i);
27   }
28   MSG_process_sleep(50);
29   XBT_INFO("Done");
30
31   return 0;
32 }
33
34 int main(int argc, char* argv[])
35 {
36   MSG_init(&argc, argv);
37   MSG_create_environment(argv[1]);
38
39   xbt_dynar_t hosts = MSG_hosts_as_dynar();
40   msg_host_t h = xbt_dynar_get_as(hosts,0,msg_host_t);
41
42   sem = MSG_sem_init(1);
43   char** aliceTimes = xbt_new(char*, 9);
44   int nbAlice = 0;
45   aliceTimes[nbAlice++] = xbt_strdup("0"); 
46   aliceTimes[nbAlice++] = xbt_strdup("1");
47   aliceTimes[nbAlice++] = xbt_strdup("3");
48   aliceTimes[nbAlice++] = xbt_strdup("5");
49   aliceTimes[nbAlice++] = xbt_strdup("1");
50   aliceTimes[nbAlice++] = xbt_strdup("2");
51   aliceTimes[nbAlice++] = xbt_strdup("5");
52   aliceTimes[nbAlice++] = xbt_strdup("0");
53   aliceTimes[nbAlice++] = NULL;
54
55   char** bobTimes = xbt_new(char*, 9);
56   int nbBob = 0;
57   bobTimes[nbBob++] = xbt_strdup("0.9"); 
58   bobTimes[nbBob++] = xbt_strdup("1");
59   bobTimes[nbBob++] = xbt_strdup("1");
60   bobTimes[nbBob++] = xbt_strdup("2");
61   bobTimes[nbBob++] = xbt_strdup("2");
62   bobTimes[nbBob++] = xbt_strdup("0");
63   bobTimes[nbBob++] = xbt_strdup("0");
64   bobTimes[nbBob++] = xbt_strdup("5");
65   bobTimes[nbBob++] = NULL;
66
67   MSG_process_create_with_arguments(xbt_strdup("Alice"), peer, NULL, h, 8, aliceTimes);
68   MSG_process_create_with_arguments(xbt_strdup("Bob"), peer, NULL, h, 8, bobTimes);
69
70   msg_error_t res = MSG_main();
71   XBT_INFO("Finished\n");
72   return (res != MSG_OK);
73 }