Logo AND Algorithmique Numérique Distribuée

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