1 /* Centralized Mutual Exclusion Algorithm
3 * This constitutes the answer to the exercice 2 of the practical
4 * lab on implementing mutual exclusion algorithms with SimGrid.
6 * YOU SHOULD TRY IMPLEMENTING IT YOURSELF BEFORE READING THE SOLUTION.
11 #define AMOUNT_OF_CLIENTS 5
12 #define CS_PER_PROCESS 2
13 XBT_LOG_NEW_DEFAULT_CATEGORY(centralized, "my log messages");
15 int coordinator(int argc, char** argv);
16 int client(int argc, char** argv);
18 int coordinator(int argc, char*argv[]) {
19 xbt_dynar_t requests=xbt_dynar_new(sizeof(char*),NULL); // dynamic vector storing requests (which are char*)
20 int CS_used=0; // initially the CS is idle
21 int todo= AMOUNT_OF_CLIENTS*CS_PER_PROCESS; // amount of releases we are expecting
24 MSG_task_receive(&task,"coordinator");
25 const char *kind = MSG_task_get_name(task); //is it a request or a release?
26 if (!strcmp(kind,"request")) { // that's a request
27 char *req = MSG_task_get_data(task);
28 if (CS_used) { // need to push the request in the vector
29 INFO0("CS already used. Queue the request");
30 xbt_dynar_push(requests, &req);
31 } else { // can serve it immediatly
32 INFO0("CS idle. Grant immediatly");
33 m_task_t answer = MSG_task_create("grant",0,1000,NULL);
34 MSG_task_send(answer,req);
37 } else { // that's a release. Check if someone was waiting for the lock
38 if (xbt_dynar_length(requests)>0) {
39 INFO1("CS release. Grant to queued requests (queue size: %lu)",xbt_dynar_length(requests));
41 xbt_dynar_pop(requests,&req);
42 MSG_task_send(MSG_task_create("grant",0,1000,NULL),req);
44 } else { // nobody wants it
45 INFO0("CS release. resource now idle");
50 MSG_task_destroy(task);
52 INFO0("Received all releases, quit now");
56 int client(int argc, char *argv[]) {
57 int my_pid=MSG_process_get_PID(MSG_process_self());
58 // use my pid as name of mailbox to contact me
59 char *my_mailbox=bprintf("%d",my_pid);
60 // request the CS 3 times, sleeping a bit in between
62 for (i=0; i<CS_PER_PROCESS;i++) {
63 INFO0("Ask the request");
64 MSG_task_send(MSG_task_create("request",0,1000,my_mailbox),"coordinator");
66 m_task_t grant = NULL;
67 MSG_task_receive(&grant,my_mailbox);
68 MSG_task_destroy(grant);
69 INFO0("got the answer. Sleep a bit and release it");
71 MSG_task_send(MSG_task_create("release",0,1000,NULL),"coordinator");
72 MSG_process_sleep(my_pid);
74 INFO0("Got all the CS I wanted, quit now");
78 int main(int argc, char*argv[]) {
79 MSG_global_init(&argc,argv);
80 MSG_create_environment("../msg_platform.xml");
81 MSG_function_register("coordinator", coordinator);
82 MSG_function_register("client", client);
83 MSG_launch_application("deploy_mutex.xml");