Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot//simgrid/simgrid
[simgrid.git] / examples / msg / mc / bugged2_liveness.c
1 /***************** Producer/Consumer Algorithm *************************/
2 /* This example implements a producer/consumer algorithm.              */
3 /* If consumer work before producer, message is empty                  */
4 /***********************************************************************/
5
6
7 #include "msg/msg.h"
8 #include "mc/mc.h"
9 #include "xbt/automaton.h"
10 #include "xbt/automatonparse_promela.h"
11 #include "bugged2_liveness.h"
12 #include "y.tab.c"
13
14 XBT_LOG_NEW_DEFAULT_CATEGORY(bugged2_liveness, "my log messages");
15
16 char* buffer;
17
18 int consume = 0;
19 int produce = 0;
20 int cready = 0;
21 int pready = 0;
22
23 int predPready(){
24   return pready;
25 }
26
27 int predCready(){
28   return cready;
29 }
30
31 int predConsume(){
32   return consume;
33 }
34
35 int predProduce(){
36   return produce;
37 }
38
39 int coordinator(int argc, char *argv[])
40 {
41   xbt_dynar_t requests = xbt_dynar_new(sizeof(char *), NULL);
42   int CS_used = 0;
43
44   while(1) {
45     m_task_t task = NULL;
46     MSG_task_receive(&task, "coordinator");
47     const char *kind = MSG_task_get_name(task);
48     if (!strcmp(kind, "request")) {
49       char *req = MSG_task_get_data(task);
50       if (CS_used) {
51         XBT_INFO("CS already used. Queue the request");
52         xbt_dynar_push(requests, &req);
53       } else {
54         m_task_t answer = MSG_task_create("grant", 0, 1000, NULL);
55         MSG_task_send(answer, req);
56         CS_used = 1;
57         XBT_INFO("CS idle. Grant immediatly");
58       }
59     } else {
60       if (xbt_dynar_length(requests) > 0) {
61         XBT_INFO("CS release. Grant to queued requests");
62         char *req;
63         xbt_dynar_pop(requests, &req);
64         MSG_task_send(MSG_task_create("grant", 0, 1000, NULL), req);
65       } else {
66         XBT_INFO("CS_realase, ressource now idle");
67         CS_used = 0;
68       }
69     }
70
71     MSG_task_destroy(task);
72
73   }
74
75   return 0;
76
77 }
78
79 int producer(int argc, char *argv[])
80 {
81
82   char * my_mailbox = bprintf("%s", argv[1]);
83   
84   while(1) {
85     
86     /* Create message */
87     const char *mess = "message";
88
89     pready = 1;
90     
91     /* CS request */
92     XBT_INFO("Producer ask the request");
93     MSG_task_send(MSG_task_create("request", 0, 1000, my_mailbox), "coordinator");
94
95     /* Wait the answer */
96     m_task_t grant = NULL;
97     MSG_task_receive(&grant, my_mailbox);
98     MSG_task_destroy(grant);
99
100     /* Push message (size of buffer = 1) */
101     buffer = strdup(mess);
102
103     produce = 1;
104
105     /* CS release */
106     MSG_task_send(MSG_task_create("release", 0, 1000, my_mailbox), "coordinator");
107
108     produce = 0;
109     pready = 0;
110
111   }
112
113   return 0;
114
115 }
116
117 int consumer(int argc, char *argv[])
118 {
119
120   char * my_mailbox = bprintf("%s", argv[1]);
121   char *mess;
122
123
124   while(1) {
125     
126     /* CS request */
127     XBT_INFO("Consumer ask the request");
128     MSG_task_send(MSG_task_create("request", 0, 1000, my_mailbox), "coordinator");
129
130     cready = 1;
131
132     /* Wait the answer */
133     m_task_t grant = NULL;
134     MSG_task_receive(&grant, my_mailbox);
135     MSG_task_destroy(grant);
136
137     /* Pop message  */
138     mess = malloc(8*sizeof(char));
139     mess = strdup(buffer);
140     buffer[0] = '\0'; 
141
142      /* Display message */
143     XBT_INFO("Message : %s", mess);
144     if(strcmp(mess, "") != 0)
145       consume = 1;
146
147     /* CS release */
148     MSG_task_send(MSG_task_create("release", 0, 1000, my_mailbox), "coordinator");
149
150     free(mess);
151
152     consume = 0;
153     cready = 0;
154
155   }
156
157   return 0;
158
159 }
160
161
162 int main(int argc, char *argv[])
163 {
164
165   buffer = malloc(8*sizeof(char));
166   buffer[0]='\0';
167
168   init();
169   yyparse();
170   automaton = get_automaton();
171   xbt_new_propositional_symbol(automaton,"pready", &predPready); 
172   xbt_new_propositional_symbol(automaton,"cready", &predCready); 
173   xbt_new_propositional_symbol(automaton,"consume", &predConsume);
174   xbt_new_propositional_symbol(automaton,"produce", &predProduce); 
175   
176   MSG_global_init(&argc, argv);
177   MSG_create_environment("../msg_platform.xml");
178   MSG_function_register("coordinator", coordinator);
179   MSG_function_register("consumer", consumer);
180   MSG_function_register("producer", producer);
181   MSG_launch_application("deploy_bugged2_liveness.xml");
182   MSG_main_liveness(automaton, argv[0]);
183
184   return 0;
185
186 }