Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
merge
[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) break; //there is no destination, die
21     MSG_process_migrate(MSG_process_self(), MSG_host_by_name(destination));
22     MSG_process_sleep(2); // I am tired, have to sleep for 2 seconds
23     free (destination);
24     task = NULL;
25   }
26   return 0;
27 }
28
29 static int policeman(int argc, char *argv[])
30 {
31   msg_task_t task = NULL;
32
33   // I am the master of emigrant process,
34   // I tell it where it must emigrate to.
35   xbt_dynar_t destinations = xbt_dynar_new (sizeof(char*), &xbt_free_ref);
36   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Tremblay"));
37   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Jupiter"));
38   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Fafard"));
39   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Ginette"));
40   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Bourassa"));
41   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Fafard"));
42   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Tremblay"));
43   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Ginette"));
44   xbt_dynar_push_as (destinations, char*, NULL);
45
46   char *destination;
47   unsigned int i;
48   xbt_dynar_foreach(destinations, i, destination){
49     task = MSG_task_create("task", 0, 0, NULL);
50     if (destination){
51       MSG_task_set_data(task, xbt_strdup (destination));
52     }
53     MSG_task_set_category(task, "migration_order");
54     MSG_task_send (task, "master_mailbox");
55     task = NULL;
56   }
57   xbt_dynar_free (&destinations);
58   return 0;
59 }
60
61 int main(int argc, char *argv[])
62 {
63   MSG_init(&argc, argv);
64   xbt_assert(argc > 1, "Usage: %s platform_file\n\tExample: %s msg_platform.xml\n", argv[0], argv[0]);
65
66   MSG_create_environment(argv[1]);
67
68   TRACE_category ("migration_order");
69
70   MSG_process_create("emigrant", emigrant, NULL, MSG_get_host_by_name("Fafard"));
71   MSG_process_create("policeman", policeman, NULL, MSG_get_host_by_name("Tremblay"));
72
73   MSG_main();
74   return 0;
75 }