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