1 /* Copyright (c) 2009, 2010. 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 #include "msg/msg.h" /* core library */
8 #include "xbt/sysdep.h" /* calloc */
10 /* Create a log channel to have nice outputs. */
12 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
13 "Messages specific for this msg example");
15 /** @addtogroup MSG_examples
17 * - <b>migration/migration.c</b> Demonstrates how to use the @ref
18 * MSG_process_migrate function to let processes change the host they
19 * run on after their start.
22 xbt_mutex_t mutex = NULL;
23 xbt_cond_t cond = NULL;
24 static msg_process_t process_to_migrate = NULL;
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[])
31 ("I'll look for a new job on another machine where the grass is greener.");
32 MSG_process_migrate(MSG_process_self(), MSG_get_host_by_name("Boivin"));
34 XBT_INFO("Yeah, found something to do");
35 task = MSG_task_create("job", 98095000, 0, NULL);
36 MSG_task_execute(task);
37 MSG_task_destroy(task);
39 XBT_INFO("Moving back home after work");
40 MSG_process_migrate(MSG_process_self(), MSG_get_host_by_name("Jacquelin"));
41 MSG_process_migrate(MSG_process_self(), MSG_get_host_by_name("Boivin"));
43 xbt_mutex_acquire(mutex);
44 process_to_migrate = MSG_process_self();
45 xbt_cond_broadcast(cond);
46 xbt_mutex_release(mutex);
47 MSG_process_suspend(MSG_process_self());
48 msg_host_t h = MSG_process_get_host(MSG_process_self());
49 XBT_INFO("I've been moved on this new host: %s", MSG_host_get_name(h));
50 XBT_INFO("Uh, nothing to do here. Stopping now");
52 } /* end_of_emigrant */
55 /* This function move the emigrant on Jacquelin */
56 static int policeman(int argc, char *argv[])
59 xbt_mutex_acquire(mutex);
60 XBT_INFO("Wait a bit before migrating the emigrant.");
61 while (process_to_migrate == NULL) xbt_cond_wait(cond, mutex);
62 MSG_process_migrate(process_to_migrate, MSG_get_host_by_name("Jacquelin"));
63 XBT_INFO("I moved the emigrant");
64 MSG_process_resume(process_to_migrate);
65 xbt_mutex_release(mutex);
68 } /* end_of_policeman */
72 int main(int argc, char *argv[])
74 msg_error_t res = MSG_OK;
76 /* Argument checking */
77 MSG_init(&argc, argv);
79 XBT_CRITICAL("Usage: %s platform_file deployment_file\n", argv[0]);
80 XBT_CRITICAL("example: %s msg_platform.xml msg_deployment_suspend.xml\n",
85 /* Simulation setting */
86 MSG_create_environment(argv[1]);
88 /* Application deployment */
89 MSG_function_register("emigrant", emigrant);
90 MSG_function_register("policeman", policeman);
91 MSG_launch_application(argv[2]);
93 /* Run the simulation */
94 mutex = xbt_mutex_init();
95 cond = xbt_cond_init();
97 XBT_INFO("Simulation time %g", MSG_get_clock());
98 xbt_cond_destroy(cond);
99 xbt_mutex_destroy(mutex);