Logo AND Algorithmique Numérique Distribuée

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