Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright notices
[simgrid.git] / examples / msg / mc / bugged1_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 /***************** Centralized Mutual Exclusion Algorithm *********************/
8 /* This example implements a centralized mutual exclusion algorithm.          */
9 /* Bug : CS requests of client 1 not satisfied                                      */
10 /* LTL property checked : G(r->F(cs)); (r=request of CS, cs=CS ok)            */
11 /******************************************************************************/
12
13 #ifdef GARBAGE_STACK
14 #include <sys/stat.h>
15 #include <fcntl.h>
16 #include <unistd.h>
17 #endif
18
19 #include "simgrid/msg.h"
20 #include "mc/mc.h"
21 #include "xbt/automaton.h"
22 #include "bugged1_liveness.h"
23
24 XBT_LOG_NEW_DEFAULT_CATEGORY(bugged1_liveness, "my log messages");
25
26 int r=0; 
27 int cs=0;
28
29 #ifdef GARBAGE_STACK
30 /** Do not use a clean stack */
31 static void garbage_stack(void) {
32   const size_t size = 256;
33   int fd = open("/dev/urandom", O_RDONLY);
34   char foo[size];
35   read(fd, foo, size);
36   close(fd);
37 }
38 #endif
39
40 int coordinator(int argc, char *argv[])
41 {
42   int CS_used = 0;   
43   msg_task_t task = NULL, answer = NULL; 
44   xbt_dynar_t requests = xbt_dynar_new(sizeof(char *), NULL);
45   char *req;
46
47   while(1){  
48     MSG_task_receive(&task, "coordinator");
49     const char *kind = MSG_task_get_name(task); 
50     if (!strcmp(kind, "request")) {    
51       req = MSG_task_get_data(task);
52       if (CS_used) {           
53         XBT_INFO("CS already used. Queue the request.");
54         xbt_dynar_push(requests, &req);
55       } else {               
56         if(strcmp(req, "1") != 0){
57           XBT_INFO("CS idle. Grant immediatly");
58           answer = MSG_task_create("grant", 0, 1000, NULL);
59           MSG_task_send(answer, req);
60           CS_used = 1;
61           answer = NULL;
62         }
63       }
64     } else {      
65       if (!xbt_dynar_is_empty(requests)) {
66         XBT_INFO("CS release. Grant to queued requests (queue size: %lu)", xbt_dynar_length(requests));
67         xbt_dynar_shift(requests, &req);
68         if(strcmp(req, "1") != 0){
69           MSG_task_send(MSG_task_create("grant", 0, 1000, NULL), req);
70         }else{
71           xbt_dynar_push(requests, &req);
72           CS_used = 0;
73         }
74       }else{
75         XBT_INFO("CS release. resource now idle");
76         CS_used = 0;
77       }
78     }
79     MSG_task_destroy(task);
80     task = NULL;
81     kind = NULL;
82     req = NULL;
83   }
84  
85   return 0;
86 }
87
88 int client(int argc, char *argv[])
89 {
90   int my_pid = MSG_process_get_PID(MSG_process_self());
91
92   char *my_mailbox = xbt_strdup(argv[1]);
93   msg_task_t grant = NULL, 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
137   return 0;
138 }
139
140 static int raw_client(int argc, char *argv[])
141 {
142 #ifdef GARBAGE_STACK
143   // At this point the stack of the callee (client) is probably filled with
144   // zeros and unitialized variables will contain 0. This call will place
145   // random byes in the stack of the callee:
146   garbage_stack();
147 #endif
148   return client(argc, argv);
149 }
150
151 int main(int argc, char *argv[])
152 {
153   MSG_init(&argc, argv);
154   char **options = &argv[1];
155
156   MC_automaton_new_propositional_symbol_pointer("r", &r);
157   MC_automaton_new_propositional_symbol_pointer("cs", &cs);
158
159   const char* platform_file = options[0];
160   const char* application_file = options[1];
161
162   MSG_create_environment(platform_file);
163
164   MSG_function_register("coordinator", coordinator);
165   MSG_function_register("client", raw_client);
166   MSG_launch_application(application_file);
167
168   MSG_main();
169
170   return 0;
171 }