Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
51e572fc6febeabf947b5c2be5717f5ddc1b7a5c
[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 "simgrid/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 coordinator(int argc, char **argv);
23 int client(int argc, char **argv);
24
25 int coordinator(int argc, char *argv[])
26 {
27   int CS_used = 0;              // initially the CS is idle
28   
29   while (1) {
30     msg_task_t task = NULL;
31     MSG_task_receive(&task, "coordinator");
32     const char *kind = MSG_task_get_name(task); //is it a request or a release?
33     if (!strcmp(kind, "request")) {     // that's a request
34       char *req = MSG_task_get_data(task);
35       if (CS_used) { 
36         XBT_INFO("CS already used.");
37         msg_task_t answer = MSG_task_create("not grant", 0, 1000, NULL);
38         MSG_task_send(answer, req);
39       } else {                  // can serve it immediatly
40         XBT_INFO("CS idle. Grant immediatly");
41         msg_task_t answer = MSG_task_create("grant", 0, 1000, NULL);
42         MSG_task_send(answer, req);
43         CS_used = 1;
44       }
45     } else {                    // that's a release. Check if someone was waiting for the lock
46       XBT_INFO("CS release. resource now idle");
47       CS_used = 0;
48     }
49     MSG_task_destroy(task);
50     kind = NULL;
51   }
52   
53   return 0;
54 }
55
56 int client(int argc, char *argv[])
57 {
58   int my_pid = MSG_process_get_PID(MSG_process_self());
59   char *my_mailbox = xbt_strdup(argv[1]);
60   const char* kind;
61  
62   while(1){
63
64     XBT_INFO("Client (%s) asks the request", my_mailbox);
65     MSG_task_send(MSG_task_create("request", 0, 1000, my_mailbox),
66                   "coordinator");
67
68     msg_task_t answer = NULL;
69     MSG_task_receive(&answer, my_mailbox);
70
71     kind = MSG_task_get_name(answer);
72     
73     if (!strcmp(kind, "grant")) {
74
75       XBT_INFO("Client (%s) got the answer (grant). Sleep a bit and release it", my_mailbox);
76
77       if(!strcmp(my_mailbox, "1"))
78         cs = 1;
79       
80     }else{
81       
82       XBT_INFO("Client (%s) got the answer (not grant). Try again", my_mailbox);
83       
84     }
85
86     MSG_task_destroy(answer);
87     kind = NULL;
88     
89     MSG_process_sleep(my_pid);
90   }
91
92   return 0;
93 }
94
95 int main(int argc, char *argv[])
96 {
97   
98   MSG_init(&argc, argv);
99
100   MC_automaton_new_propositional_symbol_pointer("cs", &cs);
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 }