1 /***************** Producer/Consumer Algorithm *************************/
2 /* This example implements a producer/consumer algorithm. */
3 /* If consumer work before producer, message is empty */
4 /***********************************************************************/
9 #include "xbt/automaton.h"
10 #include "xbt/automatonparse_promela.h"
11 #include "bugged2_liveness.h"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(bugged2_liveness, "my log messages");
39 int coordinator(int argc, char *argv[])
41 xbt_dynar_t requests = xbt_dynar_new(sizeof(char *), 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);
51 XBT_INFO("CS already used. Queue the request");
52 xbt_dynar_push(requests, &req);
54 m_task_t answer = MSG_task_create("grant", 0, 1000, NULL);
55 MSG_task_send(answer, req);
57 XBT_INFO("CS idle. Grant immediatly");
60 if (xbt_dynar_length(requests) > 0) {
61 XBT_INFO("CS release. Grant to queued requests");
63 xbt_dynar_pop(requests, &req);
64 MSG_task_send(MSG_task_create("grant", 0, 1000, NULL), req);
66 XBT_INFO("CS_realase, ressource now idle");
71 MSG_task_destroy(task);
79 int producer(int argc, char *argv[])
82 char * my_mailbox = bprintf("%s", argv[1]);
87 const char *mess = "message";
92 XBT_INFO("Producer ask the request");
93 MSG_task_send(MSG_task_create("request", 0, 1000, my_mailbox), "coordinator");
96 m_task_t grant = NULL;
97 MSG_task_receive(&grant, my_mailbox);
98 MSG_task_destroy(grant);
100 /* Push message (size of buffer = 1) */
101 buffer = strdup(mess);
106 MSG_task_send(MSG_task_create("release", 0, 1000, my_mailbox), "coordinator");
117 int consumer(int argc, char *argv[])
120 char * my_mailbox = bprintf("%s", argv[1]);
127 XBT_INFO("Consumer ask the request");
128 MSG_task_send(MSG_task_create("request", 0, 1000, my_mailbox), "coordinator");
132 /* Wait the answer */
133 m_task_t grant = NULL;
134 MSG_task_receive(&grant, my_mailbox);
135 MSG_task_destroy(grant);
138 mess = malloc(8*sizeof(char));
139 mess = strdup(buffer);
142 /* Display message */
143 XBT_INFO("Message : %s", mess);
144 if(strcmp(mess, "") != 0)
148 MSG_task_send(MSG_task_create("release", 0, 1000, my_mailbox), "coordinator");
162 int main(int argc, char *argv[])
165 buffer = malloc(8*sizeof(char));
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);
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]);