Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b363faf496f27194c3d9faf758db4ae73f3b4ef9
[simgrid.git] / examples / msg / mc / bugged2_liveness.c
1 /* Copyright (c) 2012-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 /***************************** Bugged2 ****************************************/
8 /* This example implements a centralized mutual exclusion algorithm.          */
9 /* One client stay always in critical section                                 */
10 /* LTL property checked : !(GFcs)                                             */
11 /******************************************************************************/
12
13 #include "simgrid/msg.h"
14 #include "mc/mc.h"
15
16 XBT_LOG_NEW_DEFAULT_CATEGORY(bugged3, "my log messages");
17
18 int cs = 0;
19
20 static int coordinator(int argc, char *argv[])
21 {
22   int CS_used = 0;              // initially the CS is idle
23
24   while (1) {
25     msg_task_t task = NULL;
26     MSG_task_receive(&task, "coordinator");
27     const char *kind = MSG_task_get_name(task); //is it a request or a release?
28     if (!strcmp(kind, "request")) {     // that's a request
29       char *req = MSG_task_get_data(task);
30       if (CS_used) {
31         XBT_INFO("CS already used.");
32         msg_task_t answer = MSG_task_create("not grant", 0, 1000, NULL);
33         MSG_task_send(answer, req);
34       } else {                  // can serve it immediately
35         XBT_INFO("CS idle. Grant immediately");
36         msg_task_t answer = MSG_task_create("grant", 0, 1000, NULL);
37         MSG_task_send(answer, req);
38         CS_used = 1;
39       }
40     } else {                    // that's a release. Check if someone was waiting for the lock
41       XBT_INFO("CS release. resource now idle");
42       CS_used = 0;
43     }
44     MSG_task_destroy(task);
45     kind = NULL;
46   }
47
48   return 0;
49 }
50
51 static int client(int argc, char *argv[])
52 {
53   int my_pid = MSG_process_get_PID(MSG_process_self());
54   char *my_mailbox = xbt_strdup(argv[1]);
55
56   while(1){
57     XBT_INFO("Client (%s) asks the request", my_mailbox);
58     MSG_task_send(MSG_task_create("request", 0, 1000, my_mailbox), "coordinator");
59
60     msg_task_t answer = NULL;
61     MSG_task_receive(&answer, my_mailbox);
62
63     const char* kind = MSG_task_get_name(answer);
64
65     if (!strcmp(kind, "grant")) {
66       XBT_INFO("Client (%s) got the answer (grant). Sleep a bit and release it", my_mailbox);
67       if(!strcmp(my_mailbox, "1"))
68         cs = 1;
69     }else{
70       XBT_INFO("Client (%s) got the answer (not grant). Try again", my_mailbox);
71     }
72
73     MSG_task_destroy(answer);
74     kind = NULL;
75
76     MSG_process_sleep(my_pid);
77   }
78   return 0;
79 }
80
81 int main(int argc, char *argv[])
82 {
83   MSG_init(&argc, argv);
84
85   MC_automaton_new_propositional_symbol_pointer("cs", &cs);
86
87   MSG_create_environment("../msg_platform.xml");
88   MSG_function_register("coordinator", coordinator);
89   MSG_function_register("client", client);
90   MSG_launch_application("deploy_bugged2_liveness.xml");
91   MSG_main();
92
93   return 0;
94
95 }