Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
37a287cdaa6f59402f1074fe025b249dadce7dd9
[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     XBT_INFO("task coordinator : %p", task);
35     const char *kind = MSG_task_get_name(task); 
36     XBT_INFO("kind coordinator : %p", kind);
37     if (!strcmp(kind, "request")) {    
38       char *req = MSG_task_get_data(task);
39       XBT_INFO("req coordinator : %p", req);
40       if (CS_used) {           
41         XBT_INFO("CS already used.");
42       } else {               
43         if(strcmp(req, "2") == 0){
44           XBT_INFO("CS idle. Grant immediatly");
45           MC_compare();
46           answer = MSG_task_create("grant", 0, 1000, NULL);
47           XBT_INFO("answer coordinator : %p", answer);
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 = bprintf("%s", argv[1]);
70   XBT_INFO("my mailbox client : %p", my_mailbox);
71   msg_task_t grant = NULL, release = NULL;
72
73
74   while(1) {
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     XBT_INFO("grant client : %p", grant);
87     const char *kind = MSG_task_get_name(grant);
88     XBT_INFO("kind client : %p", kind);
89
90     if((strcmp(my_mailbox, "1") == 0) && (strcmp("grant", kind) == 0)){
91       cs = 1;
92       r = 0;
93       XBT_INFO("Propositions changed : r=0, cs=1");
94     }
95
96     MSG_task_destroy(grant);
97     grant = NULL;
98     kind = NULL;
99
100     XBT_INFO("%s got the answer. Sleep a bit and release it", argv[1]);
101
102     MSG_process_sleep(1);
103
104     release = MSG_task_create("release", 0, 1000, NULL);
105     XBT_INFO("release coordinator : %p", release);
106     MSG_task_send(release, "coordinator");
107
108     release = NULL;
109
110     MSG_process_sleep(my_pid);
111     
112     if(strcmp(my_mailbox, "1") == 0){
113       cs=0;
114       r=0;
115       XBT_INFO("Propositions changed : r=0, cs=0");
116     }
117     
118   }
119
120   return 0;
121 }
122
123 int main(int argc, char *argv[])
124 {
125
126   MSG_init(&argc, argv);
127
128   MSG_config("model-check/property","promela_bugged1_liveness");
129   MC_automaton_new_propositional_symbol("r", &predR);
130   MC_automaton_new_propositional_symbol("cs", &predCS);
131   
132   MSG_create_environment("../msg_platform.xml");
133   MSG_function_register("coordinator", coordinator);
134   MSG_function_register("client", client);
135   MSG_launch_application("deploy_bugged1_liveness.xml");
136   MSG_main();
137
138   return 0;
139 }