Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
804affbf91ded975f05ecd056aae2d5bf0d9c2ae
[simgrid.git] / examples / msg / migration / migration.c
1 /* Copyright (c) 2009-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include "simgrid/msg.h"
8 #include "xbt/synchro_core.h"
9
10 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
11
12 /** @addtogroup MSG_examples
13  *  
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.
16  */
17
18 xbt_mutex_t mutex = NULL;
19 xbt_cond_t cond = NULL;
20 static msg_process_t process_to_migrate = NULL;
21
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[])
24 {
25   msg_task_t task;
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"));
28
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);
33   MSG_process_sleep(2);
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"));
37   MSG_process_sleep(4);
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");
46   return 0;
47 }
48
49 /* This function move the emigrant on Jacquelin */
50 static int policeman(int argc, char *argv[])
51 {
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);
59
60   return 0;
61 }
62
63 int main(int argc, char *argv[])
64 {
65   msg_error_t res = MSG_OK;
66
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]);
70
71   MSG_create_environment(argv[1]);
72
73   MSG_function_register("emigrant", emigrant);
74   MSG_function_register("policeman", policeman);
75   MSG_launch_application(argv[2]);
76
77   mutex = xbt_mutex_init();
78   cond = xbt_cond_init();
79   res = MSG_main();
80   XBT_INFO("Simulation time %g", MSG_get_clock());
81   xbt_cond_destroy(cond);
82   xbt_mutex_destroy(mutex);
83
84   return res != MSG_OK;
85 }