Logo AND Algorithmique Numérique Distribuée

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