Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
more pythonic example
[simgrid.git] / examples / msg / mc / bugged2_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 /***************************** Bugged2 ****************************************/
7 /* This example implements a centralized mutual exclusion algorithm.          */
8 /* One client stay always in critical section                                 */
9 /* LTL property checked : !(GFcs)                                             */
10 /******************************************************************************/
11
12 #include <simgrid/modelchecker.h>
13 #include <simgrid/msg.h>
14
15 XBT_LOG_NEW_DEFAULT_CATEGORY(bugged3, "my log messages");
16
17 int cs = 0;
18
19 static int coordinator(int argc, char *argv[])
20 {
21   int CS_used = 0;              // initially the CS is idle
22
23   while (1) {
24     msg_task_t task = NULL;
25     MSG_task_receive(&task, "coordinator");
26     const char *kind = MSG_task_get_name(task); //is it a request or a release?
27     if (!strcmp(kind, "request")) {     // that's a request
28       char *req = MSG_task_get_data(task);
29       if (CS_used) {
30         XBT_INFO("CS already used.");
31         msg_task_t answer = MSG_task_create("not grant", 0, 1000, NULL);
32         MSG_task_send(answer, req);
33       } else {                  // can serve it immediately
34         XBT_INFO("CS idle. Grant immediately");
35         msg_task_t answer = MSG_task_create("grant", 0, 1000, NULL);
36         MSG_task_send(answer, req);
37         CS_used = 1;
38       }
39     } else {                    // that's a release. Check if someone was waiting for the lock
40       XBT_INFO("CS release. resource now idle");
41       CS_used = 0;
42     }
43     MSG_task_destroy(task);
44     kind = NULL;
45   }
46
47   return 0;
48 }
49
50 static int client(int argc, char *argv[])
51 {
52   int my_pid = MSG_process_get_PID(MSG_process_self());
53   char *my_mailbox = xbt_strdup(argv[1]);
54
55   while(1){
56     XBT_INFO("Client (%s) asks the request", my_mailbox);
57     MSG_task_send(MSG_task_create("request", 0, 1000, my_mailbox), "coordinator");
58
59     msg_task_t answer = NULL;
60     MSG_task_receive(&answer, my_mailbox);
61
62     const char* kind = MSG_task_get_name(answer);
63
64     if (!strcmp(kind, "grant")) {
65       XBT_INFO("Client (%s) got the answer (grant). Sleep a bit and release it", my_mailbox);
66       if(!strcmp(my_mailbox, "1"))
67         cs = 1;
68     }else{
69       XBT_INFO("Client (%s) got the answer (not grant). Try again", my_mailbox);
70     }
71
72     MSG_task_destroy(answer);
73     kind = NULL;
74
75     MSG_process_sleep(my_pid);
76   }
77   return 0;
78 }
79
80 int main(int argc, char *argv[])
81 {
82   MSG_init(&argc, argv);
83
84   MC_automaton_new_propositional_symbol_pointer("cs", &cs);
85
86   MSG_create_environment("../msg_platform.xml");
87   MSG_function_register("coordinator", coordinator);
88   MSG_function_register("client", client);
89   MSG_launch_application("deploy_bugged2_liveness.xml");
90   MSG_main();
91
92   return 0;
93
94 }