Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Kill unused includes, and put the remaining ones at the top of the file.
[simgrid.git] / examples / deprecated / msg / mc / bugged1_liveness.c
1 /* Copyright (c) 2012-2019. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 /***************** Centralized Mutual Exclusion Algorithm *******************/
7 /* This example implements a centralized mutual exclusion algorithm.        */
8 /* Bug : CS requests of client 1 not satisfied                              */
9 /* LTL property checked : G(r->F(cs)); (r=request of CS, cs=CS ok)          */
10 /****************************************************************************/
11
12 #ifdef GARBAGE_STACK
13 #include <sys/stat.h>
14 #include <fcntl.h>
15 #include <unistd.h>
16 #endif
17
18 #include <simgrid/modelchecker.h>
19 #include <simgrid/msg.h>
20 #include <xbt/dynar.h>
21
22 XBT_LOG_NEW_DEFAULT_CATEGORY(bugged1_liveness, "my log messages");
23
24 int r=0;
25 int cs=0;
26
27 #ifdef GARBAGE_STACK
28 /** Do not use a clean stack */
29 static void garbage_stack(void) {
30   const size_t size = 256;
31   int fd = open("/dev/urandom", O_RDONLY);
32   char foo[size];
33   read(fd, foo, size);
34   close(fd);
35 }
36 #endif
37
38 static int coordinator(XBT_ATTRIB_UNUSED int argc, XBT_ATTRIB_UNUSED char* argv[])
39 {
40   int CS_used          = 0;
41   msg_task_t task      = NULL;
42   msg_task_t answer    = NULL;
43   xbt_dynar_t requests = xbt_dynar_new(sizeof(char *), NULL);
44   char *req;
45
46   while(1){
47     MSG_task_receive(&task, "coordinator");
48     const char *kind = MSG_task_get_name(task);
49     if (!strcmp(kind, "request")) {
50       req = MSG_task_get_data(task);
51       if (CS_used) {
52         XBT_INFO("CS already used. Queue the request.");
53         xbt_dynar_push(requests, &req);
54       } else {
55         if(strcmp(req, "1") != 0){
56           XBT_INFO("CS idle. Grant immediatly");
57           answer = MSG_task_create("grant", 0, 1000, NULL);
58           MSG_task_send(answer, req);
59           CS_used = 1;
60           answer = NULL;
61         }
62       }
63     } else {
64       if (!xbt_dynar_is_empty(requests)) {
65         XBT_INFO("CS release. Grant to queued requests (queue size: %lu)", xbt_dynar_length(requests));
66         xbt_dynar_shift(requests, &req);
67         if(strcmp(req, "1") != 0){
68           MSG_task_send(MSG_task_create("grant", 0, 1000, NULL), req);
69         }else{
70           xbt_dynar_push(requests, &req);
71           CS_used = 0;
72         }
73       }else{
74         XBT_INFO("CS release. resource now idle");
75         CS_used = 0;
76       }
77     }
78     MSG_task_destroy(task);
79     task = NULL;
80     kind = NULL;
81     req = NULL;
82   }
83   return 0;
84 }
85
86 static int client(int argc, char *argv[])
87 {
88   xbt_assert(argc == 2);
89   int my_pid = MSG_process_get_PID(MSG_process_self());
90
91   char *my_mailbox = xbt_strdup(argv[1]);
92   msg_task_t grant   = NULL;
93   msg_task_t release = NULL;
94
95   while(1){
96     XBT_INFO("Ask the request");
97     MSG_task_send(MSG_task_create("request", 0, 1000, my_mailbox), "coordinator");
98
99     if(strcmp(my_mailbox, "1") == 0){
100       r = 1;
101       cs = 0;
102       XBT_INFO("Propositions changed : r=1, cs=0");
103     }
104
105     MSG_task_receive(&grant, my_mailbox);
106     const char *kind = MSG_task_get_name(grant);
107
108     if((strcmp(my_mailbox, "1") == 0) && (strcmp("grant", kind) == 0)){
109       cs = 1;
110       r = 0;
111       XBT_INFO("Propositions changed : r=0, cs=1");
112     }
113
114     MSG_task_destroy(grant);
115     grant = NULL;
116     kind = NULL;
117
118     XBT_INFO("%s got the answer. Sleep a bit and release it", argv[1]);
119
120     MSG_process_sleep(1);
121
122     release = MSG_task_create("release", 0, 1000, NULL);
123     MSG_task_send(release, "coordinator");
124
125     release = NULL;
126
127     MSG_process_sleep(my_pid);
128
129     if(strcmp(my_mailbox, "1") == 0){
130       cs=0;
131       r=0;
132       XBT_INFO("Propositions changed : r=0, cs=0");
133     }
134
135   }
136   return 0;
137 }
138
139 static int raw_client(int argc, char *argv[])
140 {
141 #ifdef GARBAGE_STACK
142   // At this point the stack of the callee (client) is probably filled with
143   // zeros and uninitialized variables will contain 0. This call will place
144   // random byes in the stack of the callee:
145   garbage_stack();
146 #endif
147   return client(argc, argv);
148 }
149
150 int main(int argc, char *argv[])
151 {
152   MSG_init(&argc, argv);
153
154   MC_automaton_new_propositional_symbol_pointer("r", &r);
155   MC_automaton_new_propositional_symbol_pointer("cs", &cs);
156
157   MSG_create_environment(argv[1]);
158
159   MSG_function_register("coordinator", coordinator);
160   MSG_function_register("client", raw_client);
161   MSG_launch_application(argv[2]);
162
163   MSG_main();
164
165   return 0;
166 }