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 / mc / bugged1_liveness.c
1 /* Copyright (c) 2012-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 /***************** Centralized Mutual Exclusion Algorithm *********************/
8 /* This example implements a centralized mutual exclusion algorithm.          */
9 /* Bug : CS requests of client 1 not satisfied                                      */
10 /* LTL property checked : G(r->F(cs)); (r=request of CS, cs=CS ok)            */
11 /******************************************************************************/
12
13 #ifdef GARBAGE_STACK
14 #include <sys/stat.h>
15 #include <fcntl.h>
16 #include <unistd.h>
17 #endif
18
19 #include "simgrid/msg.h"
20 #include "mc/mc.h"
21 #include "bugged1_liveness.h"
22
23 XBT_LOG_NEW_DEFAULT_CATEGORY(bugged1_liveness, "my log messages");
24
25 int r=0; 
26 int cs=0;
27
28 #ifdef GARBAGE_STACK
29 /** Do not use a clean stack */
30 static void garbage_stack(void) {
31   const size_t size = 256;
32   int fd = open("/dev/urandom", O_RDONLY);
33   char foo[size];
34   read(fd, foo, size);
35   close(fd);
36 }
37 #endif
38
39 static int coordinator(int argc, char *argv[])
40 {
41   int CS_used = 0;   
42   msg_task_t task = NULL, answer = NULL; 
43   xbt_dynar_t requests = xbt_dynar_new(sizeof(char *), NULL);
44   char *req;
45
46   while(1){  
47     MSG_task_receive(&task, "coordinator");
48     const char *kind = MSG_task_get_name(task); 
49     if (!strcmp(kind, "request")) {    
50       req = MSG_task_get_data(task);
51       if (CS_used) {           
52         XBT_INFO("CS already used. Queue the request.");
53         xbt_dynar_push(requests, &req);
54       } else {               
55         if(strcmp(req, "1") != 0){
56           XBT_INFO("CS idle. Grant immediatly");
57           answer = MSG_task_create("grant", 0, 1000, NULL);
58           MSG_task_send(answer, req);
59           CS_used = 1;
60           answer = NULL;
61         }
62       }
63     } else {
64       if (!xbt_dynar_is_empty(requests)) {
65         XBT_INFO("CS release. Grant to queued requests (queue size: %lu)", xbt_dynar_length(requests));
66         xbt_dynar_shift(requests, &req);
67         if(strcmp(req, "1") != 0){
68           MSG_task_send(MSG_task_create("grant", 0, 1000, NULL), req);
69         }else{
70           xbt_dynar_push(requests, &req);
71           CS_used = 0;
72         }
73       }else{
74         XBT_INFO("CS release. resource now idle");
75         CS_used = 0;
76       }
77     }
78     MSG_task_destroy(task);
79     task = NULL;
80     kind = NULL;
81     req = NULL;
82   }
83   return 0;
84 }
85
86 static int client(int argc, char *argv[])
87 {
88   int my_pid = MSG_process_get_PID(MSG_process_self());
89
90   char *my_mailbox = xbt_strdup(argv[1]);
91   msg_task_t grant = NULL, release = NULL;
92
93   while(1){
94     XBT_INFO("Ask the request");
95     MSG_task_send(MSG_task_create("request", 0, 1000, my_mailbox), "coordinator");
96
97     if(strcmp(my_mailbox, "1") == 0){
98       r = 1;
99       cs = 0;
100       XBT_INFO("Propositions changed : r=1, cs=0");
101     }
102
103     MSG_task_receive(&grant, my_mailbox);
104     const char *kind = MSG_task_get_name(grant);
105
106     if((strcmp(my_mailbox, "1") == 0) && (strcmp("grant", kind) == 0)){
107       cs = 1;
108       r = 0;
109       XBT_INFO("Propositions changed : r=0, cs=1");
110     }
111
112     MSG_task_destroy(grant);
113     grant = NULL;
114     kind = NULL;
115
116     XBT_INFO("%s got the answer. Sleep a bit and release it", argv[1]);
117
118     MSG_process_sleep(1);
119
120     release = MSG_task_create("release", 0, 1000, NULL);
121     MSG_task_send(release, "coordinator");
122
123     release = NULL;
124
125     MSG_process_sleep(my_pid);
126
127     if(strcmp(my_mailbox, "1") == 0){
128       cs=0;
129       r=0;
130       XBT_INFO("Propositions changed : r=0, cs=0");
131     }
132     
133   }
134   return 0;
135 }
136
137 static int raw_client(int argc, char *argv[])
138 {
139 #ifdef GARBAGE_STACK
140   // At this point the stack of the callee (client) is probably filled with
141   // zeros and unitialized variables will contain 0. This call will place
142   // random byes in the stack of the callee:
143   garbage_stack();
144 #endif
145   return client(argc, argv);
146 }
147
148 int main(int argc, char *argv[])
149 {
150   MSG_init(&argc, argv);
151
152   MC_automaton_new_propositional_symbol_pointer("r", &r);
153   MC_automaton_new_propositional_symbol_pointer("cs", &cs);
154
155   MSG_create_environment(argv[1]);
156
157   MSG_function_register("coordinator", coordinator);
158   MSG_function_register("client", raw_client);
159   MSG_launch_application(argv[2]);
160
161   MSG_main();
162
163   return 0;
164 }