1 /* Copyright (c) 2009-2016. 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 #include "simgrid/msg.h"
7 #include "xbt/synchro.h"
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_process_migration, "Messages specific for this msg example");
11 xbt_mutex_t checkpoint = NULL;
12 xbt_cond_t identification = NULL;
13 static msg_process_t controlled_process = NULL;
15 /* The Emigrant process will be moved from host to host. */
16 static int emigrant(int argc, char *argv[])
18 XBT_INFO("I'll look for a new job on another machine ('Boivin') where the grass is greener.");
19 MSG_process_migrate(MSG_process_self(), MSG_host_by_name("Boivin")); /* - First, move to another host by myself */
21 XBT_INFO("Yeah, found something to do");
22 msg_task_t task = MSG_task_create("job", 98095000, 0, NULL); /* - Execute some work there */
23 MSG_task_execute(task);
24 MSG_task_destroy(task);
26 XBT_INFO("Moving back home after work");
27 MSG_process_migrate(MSG_process_self(), MSG_host_by_name("Jacquelin")); /* - Move back to original location */
28 MSG_process_migrate(MSG_process_self(), MSG_host_by_name("Boivin")); /* - Go back to the other host to sleep*/
30 xbt_mutex_acquire(checkpoint); /* - Get controlled at checkpoint */
31 controlled_process = MSG_process_self(); /* - and get moved back by the policeman process */
32 xbt_cond_broadcast(identification);
33 xbt_mutex_release(checkpoint);
34 MSG_process_suspend(MSG_process_self());
35 msg_host_t h = MSG_process_get_host(MSG_process_self());
36 XBT_INFO("I've been moved on this new host: %s", MSG_host_get_name(h));
37 XBT_INFO("Uh, nothing to do here. Stopping now");
41 /* The policeman check for emigrants and move them back to 'Jacquelin' */
42 static int policeman(int argc, char *argv[])
44 xbt_mutex_acquire(checkpoint);
45 XBT_INFO("Wait at the checkpoint."); /* - block on the mutex+condition */
46 while (controlled_process == NULL) xbt_cond_wait(identification, checkpoint);
47 MSG_process_migrate(controlled_process, MSG_host_by_name("Jacquelin")); /* - Move an emigrant to Jacquelin */
48 XBT_INFO("I moved the emigrant");
49 MSG_process_resume(controlled_process);
50 xbt_mutex_release(checkpoint);
55 int main(int argc, char *argv[])
57 msg_error_t res = MSG_OK;
59 MSG_init(&argc, argv);
60 xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s msg_platform.xml\n", argv[0], argv[0]);
62 MSG_create_environment(argv[1]); /* - Load the platform description */
63 /* - Create and deploy the emigrant and policeman processes */
64 MSG_process_create("emigrant", emigrant, NULL, MSG_get_host_by_name("Jacquelin"));
65 MSG_process_create("policeman", policeman, NULL, MSG_get_host_by_name("Boivin"));
67 checkpoint = xbt_mutex_init(); /* - Initiate the mutex and conditions */
68 identification = xbt_cond_init();
69 res = MSG_main(); /* - Run the simulation */
70 XBT_INFO("Simulation time %g", MSG_get_clock());
71 xbt_cond_destroy(identification);
72 xbt_mutex_destroy(checkpoint);