Logo AND Algorithmique Numérique Distribuée

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