1 /* Copyright (c) 2009-2015. 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 "simgrid/msg.h"
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
11 /** @addtogroup MSG_examples
13 * - <b>migration/migration.c</b> Demonstrates how to use the @ref MSG_process_migrate function to let processes
14 * change the host they run on after their start.
17 xbt_mutex_t mutex = NULL;
18 xbt_cond_t cond = NULL;
19 static msg_process_t process_to_migrate = NULL;
21 /** The guy we will move from host to host. It move alone and then is moved by policeman back */
22 static int emigrant(int argc, char *argv[])
25 XBT_INFO("I'll look for a new job on another machine where the grass is greener.");
26 MSG_process_migrate(MSG_process_self(), MSG_host_by_name("Boivin"));
28 XBT_INFO("Yeah, found something to do");
29 task = MSG_task_create("job", 98095000, 0, NULL);
30 MSG_task_execute(task);
31 MSG_task_destroy(task);
33 XBT_INFO("Moving back home after work");
34 MSG_process_migrate(MSG_process_self(), MSG_host_by_name("Jacquelin"));
35 MSG_process_migrate(MSG_process_self(), MSG_host_by_name("Boivin"));
37 xbt_mutex_acquire(mutex);
38 process_to_migrate = MSG_process_self();
39 xbt_cond_broadcast(cond);
40 xbt_mutex_release(mutex);
41 MSG_process_suspend(MSG_process_self());
42 msg_host_t h = MSG_process_get_host(MSG_process_self());
43 XBT_INFO("I've been moved on this new host: %s", MSG_host_get_name(h));
44 XBT_INFO("Uh, nothing to do here. Stopping now");
48 /* This function move the emigrant on Jacquelin */
49 static int policeman(int argc, char *argv[])
51 xbt_mutex_acquire(mutex);
52 XBT_INFO("Wait a bit before migrating the emigrant.");
53 while (process_to_migrate == NULL) xbt_cond_wait(cond, mutex);
54 MSG_process_migrate(process_to_migrate, MSG_host_by_name("Jacquelin"));
55 XBT_INFO("I moved the emigrant");
56 MSG_process_resume(process_to_migrate);
57 xbt_mutex_release(mutex);
62 int main(int argc, char *argv[])
64 msg_error_t res = MSG_OK;
66 MSG_init(&argc, argv);
67 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
68 "\tExample: %s msg_platform.xml msg_deployment_suspend.xml\n", argv[0], argv[0]);
70 MSG_create_environment(argv[1]);
72 MSG_function_register("emigrant", emigrant);
73 MSG_function_register("policeman", policeman);
74 MSG_launch_application(argv[2]);
76 mutex = xbt_mutex_init();
77 cond = xbt_cond_init();
79 XBT_INFO("Simulation time %g", MSG_get_clock());
80 xbt_cond_destroy(cond);
81 xbt_mutex_destroy(mutex);