Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add reduce scatter collectives from openmpi, and fix existing one
[simgrid.git] / examples / msg / semaphores / synchro.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "msg/msg.h"
4 #include "xbt/log.h"
5
6 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_semaphore_example,
7                              "Messages specific for this msg example");
8
9 msg_sem_t sem;
10
11 int peer(int argc, char* argv[]){
12
13   int i = 0; 
14   
15   while(i < argc) {
16     double wait_time = atof(argv[i++]);
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 = atof(argv[i++]);
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
32 int main(int argc, char* argv[]) {
33
34   MSG_init(&argc, argv);
35   MSG_create_environment(argv[1]);
36   
37   xbt_dynar_t hosts = MSG_hosts_as_dynar();
38   msg_host_t h = xbt_dynar_get_as(hosts,0,msg_host_t);
39
40   sem = MSG_sem_init(1);
41   char** aliceTimes = xbt_new(char*, 9);
42   int nbAlice = 0;
43   aliceTimes[nbAlice++] = xbt_strdup("0"); 
44   aliceTimes[nbAlice++] = xbt_strdup("1");
45   aliceTimes[nbAlice++] = xbt_strdup("3");
46   aliceTimes[nbAlice++] = xbt_strdup("5");
47   aliceTimes[nbAlice++] = xbt_strdup("1");
48   aliceTimes[nbAlice++] = xbt_strdup("2");
49   aliceTimes[nbAlice++] = xbt_strdup("5");
50   aliceTimes[nbAlice++] = xbt_strdup("0");
51   aliceTimes[nbAlice++] = NULL;
52
53   char** bobTimes = xbt_new(char*, 9);
54   int nbBob = 0;
55   bobTimes[nbBob++] = xbt_strdup("0.9"); 
56   bobTimes[nbBob++] = xbt_strdup("1");
57   bobTimes[nbBob++] = xbt_strdup("1");
58   bobTimes[nbBob++] = xbt_strdup("2");
59   bobTimes[nbBob++] = xbt_strdup("2");
60   bobTimes[nbBob++] = xbt_strdup("0");
61   bobTimes[nbBob++] = xbt_strdup("0");
62   bobTimes[nbBob++] = xbt_strdup("5");
63   bobTimes[nbBob++] = NULL;
64  
65
66   MSG_process_create_with_arguments(xbt_strdup("Alice"), peer, NULL, 
67                                     h, 8, aliceTimes);
68   MSG_process_create_with_arguments(xbt_strdup("Bob"), peer, NULL, 
69                                     h, 8, bobTimes);
70
71   msg_error_t res = MSG_main();
72   printf("Finished\n");
73   return 0;
74 }