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