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_process_migration, "Messages specific for this msg example");
12 /** @addtogroup MSG_examples
14 * - <b>Process migration: process-migration/process-migration.c</b>. Processes can move or be moved from a host to
15 * another while they are running thanks to the @ref MSG_process_migrate function.
18 xbt_mutex_t checkpoint = NULL;
19 xbt_cond_t identification = NULL;
20 static msg_process_t controlled_process = NULL;
22 /** The Emigrant will be moved from host to host. */
23 static int emigrant(int argc, char *argv[])
25 XBT_INFO("I'll look for a new job on another machine ('Boivin') where the grass is greener.");
26 MSG_process_migrate(MSG_process_self(), MSG_host_by_name("Boivin")); /** - First, move to another host by myself */
28 XBT_INFO("Yeah, found something to do");
29 msg_task_t task = MSG_task_create("job", 98095000, 0, NULL); /** - Execute some work there */
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")); /** - Move back to original location */
35 MSG_process_migrate(MSG_process_self(), MSG_host_by_name("Boivin")); /** - Go back to the other host to sleep*/
37 xbt_mutex_acquire(checkpoint); /** - Get controlled at checkpoint */
38 controlled_process = MSG_process_self(); /** - and get moved back by @ref policeman */
39 xbt_cond_broadcast(identification);
40 xbt_mutex_release(checkpoint);
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 /** The policeman check for emigrants and move them back to 'Jacquelin' */
49 static int policeman(int argc, char *argv[])
51 xbt_mutex_acquire(checkpoint);
52 XBT_INFO("Wait at the checkpoint."); /** - Wait at @ref checkpoint to control the @ref identification of processes */
53 while (controlled_process == NULL) xbt_cond_wait(identification, checkpoint);
54 MSG_process_migrate(controlled_process, MSG_host_by_name("Jacquelin")); /** - Move an emigrant to Jacquelin */
55 XBT_INFO("I moved the emigrant");
56 MSG_process_resume(controlled_process);
57 xbt_mutex_release(checkpoint);
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\n\tExample: %s msg_platform.xml\n", argv[0], argv[0]);
69 MSG_create_environment(argv[1]); /** - Load the platform description */
70 /** - Create and deploy @ref emigrant and @ref policeman processes */
71 MSG_process_create("emigrant", emigrant, NULL, MSG_get_host_by_name("Jacquelin"));
72 MSG_process_create("policeman", policeman, NULL, MSG_get_host_by_name("Boivin"));
74 checkpoint = xbt_mutex_init(); /** - Initiate @ref checkpoint and @ref identification*/
75 identification = xbt_cond_init();
76 res = MSG_main(); /** - Run the simulation */
77 XBT_INFO("Simulation time %g", MSG_get_clock());
78 xbt_cond_destroy(identification);
79 xbt_mutex_destroy(checkpoint);