1 /* Copyright (c) 2012-2014. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 /***************** Centralized Mutual Exclusion Algorithm *********************/
8 /* This example implements a centralized mutual exclusion algorithm. */
9 /* Bug : CS requests of client 1 not satisfied */
10 /* LTL property checked : G(r->F(cs)); (r=request of CS, cs=CS ok) */
11 /******************************************************************************/
15 #include "xbt/automaton.h"
16 #include "bugged1_liveness.h"
18 XBT_LOG_NEW_DEFAULT_CATEGORY(bugged1_liveness, "my log messages");
32 int coordinator(int argc, char *argv[])
36 msg_task_t task = NULL, answer = NULL;
37 xbt_dynar_t requests = xbt_dynar_new(sizeof(char *), NULL);
41 MSG_task_receive(&task, "coordinator");
42 const char *kind = MSG_task_get_name(task);
43 if (!strcmp(kind, "request")) {
44 req = MSG_task_get_data(task);
46 XBT_INFO("CS already used. Queue the request.");
47 xbt_dynar_push(requests, &req);
49 if(strcmp(req, "1") != 0){
50 XBT_INFO("CS idle. Grant immediatly");
51 answer = MSG_task_create("grant", 0, 1000, NULL);
52 MSG_task_send(answer, req);
58 if (!xbt_dynar_is_empty(requests)) {
59 XBT_INFO("CS release. Grant to queued requests (queue size: %lu)", xbt_dynar_length(requests));
60 xbt_dynar_shift(requests, &req);
61 if(strcmp(req, "1") != 0){
62 MSG_task_send(MSG_task_create("grant", 0, 1000, NULL), req);
64 xbt_dynar_push(requests, &req);
68 XBT_INFO("CS release. resource now idle");
72 MSG_task_destroy(task);
81 int client(int argc, char *argv[])
83 int my_pid = MSG_process_get_PID(MSG_process_self());
85 char *my_mailbox = xbt_strdup(argv[1]);
86 msg_task_t grant = NULL, release = NULL;
89 XBT_INFO("Ask the request");
90 MSG_task_send(MSG_task_create("request", 0, 1000, my_mailbox), "coordinator");
92 if(strcmp(my_mailbox, "1") == 0){
95 XBT_INFO("Propositions changed : r=1, cs=0");
98 MSG_task_receive(&grant, my_mailbox);
99 const char *kind = MSG_task_get_name(grant);
101 if((strcmp(my_mailbox, "1") == 0) && (strcmp("grant", kind) == 0)){
104 XBT_INFO("Propositions changed : r=0, cs=1");
107 MSG_task_destroy(grant);
111 XBT_INFO("%s got the answer. Sleep a bit and release it", argv[1]);
113 MSG_process_sleep(1);
115 release = MSG_task_create("release", 0, 1000, NULL);
116 MSG_task_send(release, "coordinator");
120 MSG_process_sleep(my_pid);
122 if(strcmp(my_mailbox, "1") == 0){
125 XBT_INFO("Propositions changed : r=0, cs=0");
133 int main(int argc, char *argv[])
136 MSG_init(&argc, argv);
138 char **options = &argv[1];
140 MSG_config("model-check/property","promela_bugged1_liveness");
141 MC_automaton_new_propositional_symbol("r", &predR);
142 MC_automaton_new_propositional_symbol("cs", &predCS);
144 const char* platform_file = options[0];
145 const char* application_file = options[1];
147 MSG_create_environment(platform_file);
148 MSG_function_register("coordinator", coordinator);
149 MSG_function_register("client", client);
150 MSG_launch_application(application_file);