Logo AND Algorithmique Numérique Distribuée

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