Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Bugfix
[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 #define NB_REQUESTS 3
15 #define NB_CLIENTS 3
16
17 int r=0; 
18 int cs=0;
19
20 int predR(){
21   return r;
22 }
23
24 int predCS(){
25   return cs;
26 }
27
28
29 int coordinator(int argc, char *argv[])
30 {
31
32   int CS_used = 0;   
33   msg_task_t task = NULL, answer = NULL;        
34
35   int i;
36
37   for(i=0; i<(NB_REQUESTS * NB_CLIENTS); i++){
38     MSG_task_receive(&task, "coordinator");
39     const char *kind = MSG_task_get_name(task); 
40     if (!strcmp(kind, "request")) {    
41       char *req = MSG_task_get_data(task);
42       if (CS_used) {           
43         //XBT_INFO("CS already used.");
44       } else {               
45         if(strcmp(req, "1") != 0){
46           //XBT_INFO("CS idle. Grant immediatly");
47           answer = MSG_task_create("grant", 0, 1000, NULL);
48           MSG_task_send(answer, req);
49           CS_used = 1;
50           answer = NULL;
51         }
52       }
53     } else {         
54       //XBT_INFO("CS release. resource now idle");
55       CS_used = 0;
56     }
57     MSG_task_destroy(task);
58     task = NULL;
59     kind = NULL;
60   }
61  
62   return 0;
63 }
64
65 int client(int argc, char *argv[])
66 {
67   int my_pid = MSG_process_get_PID(MSG_process_self());
68
69   char *my_mailbox = xbt_strdup(argv[1]);
70   msg_task_t grant = NULL, release = NULL;
71   
72   int i;
73
74   for(i=0; i<NB_REQUESTS; i++){
75       
76     //XBT_INFO("Ask the request");
77     MSG_task_send(MSG_task_create("request", 0, 1000, my_mailbox), "coordinator");
78
79     if(strcmp(my_mailbox, "1") == 0){
80       r = 1;
81       cs = 0;
82       //XBT_INFO("Propositions changed : r=1, cs=0");
83     }
84
85     MSG_task_receive(&grant, my_mailbox);
86     const char *kind = MSG_task_get_name(grant);
87
88     if((strcmp(my_mailbox, "1") == 0) && (strcmp("grant", kind) == 0)){
89       cs = 1;
90       r = 0;
91       //XBT_INFO("Propositions changed : r=0, cs=1");
92     }
93
94     MSG_task_destroy(grant);
95     grant = NULL;
96     kind = NULL;
97
98     //XBT_INFO("%s got the answer. Sleep a bit and release it", argv[1]);
99
100     MSG_process_sleep(1);
101
102     release = MSG_task_create("release", 0, 1000, NULL);
103     MSG_task_send(release, "coordinator");
104
105     release = NULL;
106
107     MSG_process_sleep(my_pid);
108     
109     if(strcmp(my_mailbox, "1") == 0){
110       cs=0;
111       r=0;
112       //XBT_INFO("Propositions changed : r=0, cs=0");
113     }
114     
115   }
116
117   return 0;
118 }
119
120 int main(int argc, char *argv[])
121 {
122
123   MSG_init(&argc, argv);
124
125   MSG_config("model-check/property","promela_bugged1_liveness");
126   MC_automaton_new_propositional_symbol("r", &predR);
127   MC_automaton_new_propositional_symbol("cs", &predCS);
128   
129   MSG_create_environment("../msg_platform.xml");
130   MSG_function_register("coordinator", coordinator);
131   MSG_function_register("client", client);
132   MSG_launch_application("deploy_bugged1_liveness.xml");
133   MSG_main();
134
135   return 0;
136 }