Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ignore some new stuff
[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   msg_task_t task = NULL, answer = NULL;        
31
32   while (1) {
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           MC_compare();
43           answer = MSG_task_create("grant", 0, 1000, NULL);
44           MSG_task_send(answer, req);
45           CS_used = 1;
46           answer = NULL;
47         }
48       }
49     } else {         
50       XBT_INFO("CS release. resource now idle");
51       CS_used = 0;
52     }
53     MSG_task_destroy(task);
54     task = NULL;
55     kind = NULL;
56   }
57  
58   return 0;
59 }
60
61 int client(int argc, char *argv[])
62 {
63   int my_pid = MSG_process_get_PID(MSG_process_self());
64
65   char *my_mailbox = bprintf("%s", argv[1]);
66   msg_task_t grant = NULL, release = NULL;
67
68
69   while(1) {
70       
71     XBT_INFO("Ask the request");
72     MSG_task_send(MSG_task_create("request", 0, 1000, my_mailbox), "coordinator");
73
74     if(strcmp(my_mailbox, "1") == 0){
75       r = 1;
76       cs = 0;
77       XBT_INFO("Propositions changed : r=1, cs=0");
78     }
79
80     MSG_task_receive(&grant, my_mailbox);
81     const char *kind = MSG_task_get_name(grant);
82
83     if((strcmp(my_mailbox, "1") == 0) && (strcmp("grant", kind) == 0)){
84       cs = 1;
85       r = 0;
86       XBT_INFO("Propositions changed : r=0, cs=1");
87     }
88
89     MSG_task_destroy(grant);
90     grant = NULL;
91     kind = NULL;
92
93     XBT_INFO("%s got the answer. Sleep a bit and release it", argv[1]);
94
95     MSG_process_sleep(1);
96
97     release = MSG_task_create("release", 0, 1000, NULL);
98     MSG_task_send(release, "coordinator");
99
100     release = NULL;
101
102     MSG_process_sleep(my_pid);
103     
104     if(strcmp(my_mailbox, "1") == 0){
105       cs=0;
106       r=0;
107       XBT_INFO("Propositions changed : r=0, cs=0");
108     }
109     
110   }
111
112   return 0;
113 }
114
115 int main(int argc, char *argv[])
116 {
117
118   MSG_init(&argc, argv);
119
120   MSG_config("model-check/property","promela_bugged1_liveness");
121   MC_automaton_new_propositional_symbol("r", &predR);
122   MC_automaton_new_propositional_symbol("cs", &predCS);
123   
124   MSG_create_environment("../msg_platform.xml");
125   MSG_function_register("coordinator", coordinator);
126   MSG_function_register("client", client);
127   MSG_launch_application("deploy_bugged1_liveness.xml");
128   MSG_main();
129
130   return 0;
131 }