Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fef6142aef3081b6f299061b6412d23d36c96b65
[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");
19     MSG_sem_acquire(sem);
20     XBT_INFO("Acquired");
21
22     wait_time = atof(argv[i++]);
23     MSG_process_sleep(wait_time);
24     XBT_INFO("Releasing");
25     MSG_sem_release(sem);
26     XBT_INFO("Released");
27   }
28
29 }
30
31 int main(int argc, char* argv[]) {
32
33   MSG_init(&argc, argv);
34   MSG_create_environment(argv[1]);
35   
36   xbt_dynar_t hosts = MSG_hosts_as_dynar();
37   msg_host_t h = xbt_dynar_get_as(hosts,0,msg_host_t);
38
39
40   char* aliceTimes[] = {"0", "1", "3", "5", "1", "2", "5", "0"};
41   char* bobTimes[] = {"1", "1", "1", "2", "2", "0", "0", "5"};
42
43
44   MSG_process_create_with_arguments("Alice", peer, NULL, 
45                                     h, 8, aliceTimes);
46   MSG_process_create_with_arguments("Bob", peer, NULL, 
47                                     h, 8, bobTimes);
48
49   MSG_main();
50
51 }