Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
MC: complete workaround in the error msg seen on modern systems
[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
21 XBT_LOG_NEW_DEFAULT_CATEGORY(bugged1_liveness, "my log messages");
22
23 int r=0;
24 int cs=0;
25
26 #ifdef GARBAGE_STACK
27 /** Do not use a clean stack */
28 static void garbage_stack(void) {
29   const size_t size = 256;
30   int fd = open("/dev/urandom", O_RDONLY);
31   char foo[size];
32   read(fd, foo, size);
33   close(fd);
34 }
35 #endif
36
37 static int coordinator(XBT_ATTRIB_UNUSED int argc, XBT_ATTRIB_UNUSED char* argv[])
38 {
39   int CS_used          = 0;
40   msg_task_t task      = NULL;
41   msg_task_t answer    = NULL;
42   xbt_dynar_t requests = xbt_dynar_new(sizeof(char *), NULL);
43   char *req;
44
45   while(1){
46     MSG_task_receive(&task, "coordinator");
47     const char *kind = MSG_task_get_name(task);
48     if (!strcmp(kind, "request")) {
49       req = MSG_task_get_data(task);
50       if (CS_used) {
51         XBT_INFO("CS already used. Queue the request.");
52         xbt_dynar_push(requests, &req);
53       } else {
54         if(strcmp(req, "1") != 0){
55           XBT_INFO("CS idle. Grant immediatly");
56           answer = MSG_task_create("grant", 0, 1000, NULL);
57           MSG_task_send(answer, req);
58           CS_used = 1;
59           answer = NULL;
60         }
61       }
62     } else {
63       if (!xbt_dynar_is_empty(requests)) {
64         XBT_INFO("CS release. Grant to queued requests (queue size: %lu)", xbt_dynar_length(requests));
65         xbt_dynar_shift(requests, &req);
66         if(strcmp(req, "1") != 0){
67           MSG_task_send(MSG_task_create("grant", 0, 1000, NULL), req);
68         }else{
69           xbt_dynar_push(requests, &req);
70           CS_used = 0;
71         }
72       }else{
73         XBT_INFO("CS release. resource now idle");
74         CS_used = 0;
75       }
76     }
77     MSG_task_destroy(task);
78     task = NULL;
79     kind = NULL;
80     req = NULL;
81   }
82   return 0;
83 }
84
85 static int client(int argc, char *argv[])
86 {
87   xbt_assert(argc == 2);
88   int my_pid = MSG_process_get_PID(MSG_process_self());
89
90   char *my_mailbox = xbt_strdup(argv[1]);
91   msg_task_t grant   = NULL;
92   msg_task_t release = NULL;
93
94   while(1){
95     XBT_INFO("Ask the request");
96     MSG_task_send(MSG_task_create("request", 0, 1000, my_mailbox), "coordinator");
97
98     if(strcmp(my_mailbox, "1") == 0){
99       r = 1;
100       cs = 0;
101       XBT_INFO("Propositions changed : r=1, cs=0");
102     }
103
104     MSG_task_receive(&grant, my_mailbox);
105     const char *kind = MSG_task_get_name(grant);
106
107     if((strcmp(my_mailbox, "1") == 0) && (strcmp("grant", kind) == 0)){
108       cs = 1;
109       r = 0;
110       XBT_INFO("Propositions changed : r=0, cs=1");
111     }
112
113     MSG_task_destroy(grant);
114     grant = NULL;
115     kind = NULL;
116
117     XBT_INFO("%s got the answer. Sleep a bit and release it", argv[1]);
118
119     MSG_process_sleep(1);
120
121     release = MSG_task_create("release", 0, 1000, NULL);
122     MSG_task_send(release, "coordinator");
123
124     release = NULL;
125
126     MSG_process_sleep(my_pid);
127
128     if(strcmp(my_mailbox, "1") == 0){
129       cs=0;
130       r=0;
131       XBT_INFO("Propositions changed : r=0, cs=0");
132     }
133
134   }
135   return 0;
136 }
137
138 static int raw_client(int argc, char *argv[])
139 {
140 #ifdef GARBAGE_STACK
141   // At this point the stack of the callee (client) is probably filled with
142   // zeros and uninitialized variables will contain 0. This call will place
143   // random byes in the stack of the callee:
144   garbage_stack();
145 #endif
146   return client(argc, argv);
147 }
148
149 int main(int argc, char *argv[])
150 {
151   MSG_init(&argc, argv);
152
153   MC_automaton_new_propositional_symbol_pointer("r", &r);
154   MC_automaton_new_propositional_symbol_pointer("cs", &cs);
155
156   MSG_create_environment(argv[1]);
157
158   MSG_function_register("coordinator", coordinator);
159   MSG_function_register("client", raw_client);
160   MSG_launch_application(argv[2]);
161
162   MSG_main();
163
164   return 0;
165 }