Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9fdc4a8642daeff49b90ed7323f02cc55fa8fe05
[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
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
10
11 /** @addtogroup MSG_examples
12  *  
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.
15  */
16
17 xbt_mutex_t mutex = NULL;
18 xbt_cond_t cond = NULL;
19 static msg_process_t process_to_migrate = NULL;
20
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[])
23 {
24   msg_task_t task;
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"));
27
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);
32   MSG_process_sleep(2);
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"));
36   MSG_process_sleep(4);
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");
45   return 0;
46 }
47
48 /* This function move the emigrant on Jacquelin */
49 static int policeman(int argc, char *argv[])
50 {
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);
58
59   return 0;
60 }
61
62 int main(int argc, char *argv[])
63 {
64   msg_error_t res = MSG_OK;
65
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]);
69
70   MSG_create_environment(argv[1]);
71
72   MSG_function_register("emigrant", emigrant);
73   MSG_function_register("policeman", policeman);
74   MSG_launch_application(argv[2]);
75
76   mutex = xbt_mutex_init();
77   cond = xbt_cond_init();
78   res = MSG_main();
79   XBT_INFO("Simulation time %g", MSG_get_clock());
80   xbt_cond_destroy(cond);
81   xbt_mutex_destroy(mutex);
82
83   return res != MSG_OK;
84 }