Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : memset 0 on free block/fragment
[simgrid.git] / examples / msg / mc / bugged1_liveness.c
1 /***************** Centralized Mutual Exclusion Algorithm *********************/
2 /* This example implements a centralized mutual exclusion algorithm.          */
3 /* CS requests of client 1 not satisfied                                      */
4 /* LTL property checked : G(r->F(cs)); (r=request of CS, cs=CS ok)            */
5 /******************************************************************************/
6
7 #include "msg/msg.h"
8 #include "mc/mc.h"
9 #include "xbt/automaton.h"
10 #include "bugged1_liveness.h"
11
12 XBT_LOG_NEW_DEFAULT_CATEGORY(bugged1_liveness, "my log messages");
13
14 int r=0; 
15 int cs=0;
16
17 int predR(){
18   return r;
19 }
20
21 int predCS(){
22   return cs;
23 }
24
25
26 int coordinator(int argc, char *argv[])
27 {
28
29   int CS_used = 0;           
30
31   while (1) {
32     msg_task_t task = NULL;
33     MSG_task_receive(&task, "coordinator");
34     const char *kind = MSG_task_get_name(task); 
35     if (!strcmp(kind, "request")) {    
36       char *req = MSG_task_get_data(task);
37       if (CS_used) {           
38         XBT_INFO("CS already used.");
39       } else {               
40         if(strcmp(req, "2") == 0){
41           XBT_INFO("CS idle. Grant immediatly");
42           msg_task_t answer = MSG_task_create("grant", 0, 1000, NULL);
43           MSG_task_send(answer, req);
44           CS_used = 1;
45         }
46       }
47     } else {         
48       XBT_INFO("CS release. resource now idle");
49       CS_used = 0;
50     }
51     MSG_task_destroy(task);
52   }
53  
54   return 0;
55 }
56
57 int client(int argc, char *argv[])
58 {
59   int my_pid = MSG_process_get_PID(MSG_process_self());
60
61   char *my_mailbox = bprintf("%s", argv[1]);
62
63
64   while(1) {
65       
66     XBT_INFO("Ask the request");
67     MSG_task_send(MSG_task_create("request", 0, 1000, my_mailbox), "coordinator");
68
69     if(strcmp(my_mailbox, "1") == 0){
70       r = 1;
71       cs = 0;
72       XBT_INFO("Propositions changed : r=1, cs=0");
73     }
74
75
76     msg_task_t grant = NULL;
77     MSG_task_receive(&grant, my_mailbox);
78     const char *kind = MSG_task_get_name(grant);
79
80     if((strcmp(my_mailbox, "1") == 0) && (strcmp("grant", kind) == 0)){
81       cs = 1;
82       r = 0;
83       XBT_INFO("Propositions changed : r=0, cs=1");
84     }
85
86
87     MSG_task_destroy(grant);
88     XBT_INFO("%s got the answer. Sleep a bit and release it", argv[1]);
89     MSG_process_sleep(1);
90     MSG_task_send(MSG_task_create("release", 0, 1000, NULL), "coordinator");
91
92     MSG_process_sleep(my_pid);
93     
94     if(strcmp(my_mailbox, "1") == 0){
95       cs=0;
96       r=0;
97       XBT_INFO("Propositions changed : r=0, cs=0");
98     }
99     
100   }
101
102   return 0;
103 }
104
105 int main(int argc, char *argv[])
106 {
107
108   MSG_init(&argc, argv);
109
110   MSG_config("model-check/property","promela1_bugged1_liveness");
111   MC_automaton_new_propositional_symbol("r", &predR);
112   MC_automaton_new_propositional_symbol("cs", &predCS);
113   
114   MSG_create_environment("../msg_platform.xml");
115   MSG_function_register("coordinator", coordinator);
116   MSG_function_register("client", client);
117   MSG_launch_application("deploy_bugged1_liveness.xml");
118   MSG_main();
119
120   return 0;
121 }