Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
please my old friend SonarQube now that it's back
[simgrid.git] / examples / msg / trace-process-migration / trace-process-migration.c
1 /* Copyright (c) 2010-2016. 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 #include "simgrid/msg.h"
7
8 /* The guy we will move from host to host. It move alone and then is moved by policeman back  */
9 static int emigrant(int argc, char *argv[])
10 {
11   msg_task_t task = NULL;
12   char *destination = NULL;
13
14   MSG_process_sleep(2);
15
16   while (1){ // I am an eternal emigrant
17     MSG_task_receive(&(task), "master_mailbox");
18     destination = (char*)MSG_task_get_data (task);
19     MSG_task_destroy (task);
20     if (destination == NULL)
21       break; //there is no destination, die
22     MSG_process_migrate(MSG_process_self(), MSG_host_by_name(destination));
23     MSG_process_sleep(2); // I am tired, have to sleep for 2 seconds
24     free (destination);
25     task = NULL;
26   }
27   return 0;
28 }
29
30 static int policeman(int argc, char *argv[])
31 {
32   // I am the master of emigrant process,
33   // I tell it where it must emigrate to.
34   xbt_dynar_t destinations = xbt_dynar_new (sizeof(char*), &xbt_free_ref);
35   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Tremblay"));
36   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Jupiter"));
37   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Fafard"));
38   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Ginette"));
39   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Bourassa"));
40   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Fafard"));
41   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Tremblay"));
42   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Ginette"));
43   xbt_dynar_push_as (destinations, char*, NULL);
44
45   char *destination;
46   unsigned int i;
47   xbt_dynar_foreach(destinations, i, destination){
48     msg_task_t task = MSG_task_create("task", 0, 0, NULL);
49     if (destination != NULL){
50       MSG_task_set_data(task, xbt_strdup (destination));
51     }
52     MSG_task_set_category(task, "migration_order");
53     MSG_task_send (task, "master_mailbox");
54   }
55   xbt_dynar_free (&destinations);
56   return 0;
57 }
58
59 int main(int argc, char *argv[])
60 {
61   MSG_init(&argc, argv);
62   xbt_assert(argc > 1, "Usage: %s platform_file\n\tExample: %s msg_platform.xml\n", argv[0], argv[0]);
63
64   MSG_create_environment(argv[1]);
65
66   TRACE_category ("migration_order");
67
68   MSG_process_create("emigrant", emigrant, NULL, MSG_get_host_by_name("Fafard"));
69   MSG_process_create("policeman", policeman, NULL, MSG_get_host_by_name("Tremblay"));
70
71   MSG_main();
72   return 0;
73 }