Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
bfbae35aef8cfc766b96aa7aed1dad66a71afa45
[simgrid.git] / examples / msg / synchro-semaphore / synchro-semaphore.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   msg_host_t h = MSG_host_by_name("Fafard");
40
41   sem = MSG_sem_init(1);
42   char** aliceTimes = xbt_new(char*, 9);
43   int nbAlice = 0;
44   aliceTimes[nbAlice++] = xbt_strdup("0"); 
45   aliceTimes[nbAlice++] = xbt_strdup("1");
46   aliceTimes[nbAlice++] = xbt_strdup("3");
47   aliceTimes[nbAlice++] = xbt_strdup("5");
48   aliceTimes[nbAlice++] = xbt_strdup("1");
49   aliceTimes[nbAlice++] = xbt_strdup("2");
50   aliceTimes[nbAlice++] = xbt_strdup("5");
51   aliceTimes[nbAlice++] = xbt_strdup("0");
52   aliceTimes[nbAlice++] = NULL;
53
54   char** bobTimes = xbt_new(char*, 9);
55   int nbBob = 0;
56   bobTimes[nbBob++] = xbt_strdup("0.9"); 
57   bobTimes[nbBob++] = xbt_strdup("1");
58   bobTimes[nbBob++] = xbt_strdup("1");
59   bobTimes[nbBob++] = xbt_strdup("2");
60   bobTimes[nbBob++] = xbt_strdup("2");
61   bobTimes[nbBob++] = xbt_strdup("0");
62   bobTimes[nbBob++] = xbt_strdup("0");
63   bobTimes[nbBob++] = xbt_strdup("5");
64   bobTimes[nbBob++] = NULL;
65
66   MSG_process_create_with_arguments(xbt_strdup("Alice"), peer, NULL, h, 8, aliceTimes);
67   MSG_process_create_with_arguments(xbt_strdup("Bob"), peer, NULL, h, 8, bobTimes);
68
69   msg_error_t res = MSG_main();
70   XBT_INFO("Finished\n");
71   return (res != MSG_OK);
72 }