Logo AND Algorithmique Numérique Distribuée

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