1 /* Copyright (c) 2010-2014. The SimGrid Team.
2 * All rights reserved. */
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. */
7 /** @addtogroup MSG_examples
9 * - <b>tracing/procmig.c</b> This program shows a process migration. Tracing
10 * this program with the options below enables a gantt-chart visualization
11 * of where the process has been during its execution. Migrations are represented by
12 * arrows from the origin to the destination host.
13 * You might want to run this program with the following parameters:
15 * --cfg=tracing/msg/process:yes
16 * (See \ref tracing_tracing_options for details)
19 #include "msg/msg.h" /* core library */
20 #include "xbt/sysdep.h" /* calloc */
22 /* Create a log channel to have nice outputs. */
24 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
25 "Messages specific for this msg example");
27 /** The guy we will move from host to host. It move alone and then is moved by policeman back */
28 static int emigrant(int argc, char *argv[])
30 msg_task_t task = NULL;
31 char *destination = NULL;
35 while (1){ // I am an eternal emigrant
36 MSG_task_receive(&(task), "master_mailbox");
37 destination = (char*)MSG_task_get_data (task);
38 MSG_task_destroy (task);
39 if (!destination) break; //there is no destination, die
40 MSG_process_migrate(MSG_process_self(), MSG_get_host_by_name(destination));
41 MSG_process_sleep(2); // I am tired, have to sleep for 2 seconds
48 static int master(int argc, char *argv[])
50 msg_task_t task = NULL;
52 // I am the master of emigrant process,
53 // I tell it where it must emigrate to.
54 xbt_dynar_t destinations = xbt_dynar_new (sizeof(char*), &xbt_free_ref);
55 xbt_dynar_push_as (destinations, char*, xbt_strdup ("Tremblay"));
56 xbt_dynar_push_as (destinations, char*, xbt_strdup ("Jupiter"));
57 xbt_dynar_push_as (destinations, char*, xbt_strdup ("Fafard"));
58 xbt_dynar_push_as (destinations, char*, xbt_strdup ("Ginette"));
59 xbt_dynar_push_as (destinations, char*, xbt_strdup ("Bourassa"));
60 xbt_dynar_push_as (destinations, char*, xbt_strdup ("Fafard"));
61 xbt_dynar_push_as (destinations, char*, xbt_strdup ("Tremblay"));
62 xbt_dynar_push_as (destinations, char*, xbt_strdup ("Ginette"));
63 xbt_dynar_push_as (destinations, char*, NULL);
67 xbt_dynar_foreach(destinations, i, destination){
68 task = MSG_task_create("task", 0, 0, NULL);
70 MSG_task_set_data(task, xbt_strdup (destination));
72 MSG_task_set_category(task, "migration_order");
73 MSG_task_send (task, "master_mailbox");
76 xbt_dynar_free (&destinations);
81 int main(int argc, char *argv[])
83 /* Argument checking */
84 MSG_init(&argc, argv);
86 XBT_CRITICAL("Usage: %s platform_file deployment_file\n", argv[0]);
90 char *platform_file = argv[1];
91 char *deployment_file = argv[2];
92 MSG_create_environment(platform_file);
94 TRACE_category ("migration_order");
96 /* Application deployment */
97 MSG_function_register("emigrant", emigrant);
98 MSG_function_register("master", master);
99 MSG_launch_application(deployment_file);