Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update tesh files with process autorestart.
[simgrid.git] / examples / msg / migration / migration.c
1 /* Copyright (c) 2009, 2010. 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 "msg/msg.h"            /* core library */
8 #include "xbt/sysdep.h"         /* calloc */
9
10 /* Create a log channel to have nice outputs. */
11 #include "xbt/log.h"
12 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
13                              "Messages specific for this msg example");
14
15 /** @addtogroup MSG_examples
16  *  
17  *  - <b>migration/migration.c</b> Demonstrates how to use the @ref
18  *    MSG_process_migrate function to let processes change the host they 
19  *    run on after their start. 
20  */
21
22 xbt_mutex_t mutex = NULL;
23 xbt_cond_t cond = NULL;
24 static m_process_t process_to_migrate = NULL;
25
26 /** The guy we will move from host to host. It move alone and then is moved by policeman back  */
27 static int emigrant(int argc, char *argv[])
28 {
29   m_task_t task;
30   XBT_INFO
31       ("I'll look for a new job on another machine where the grass is greener.");
32   MSG_process_migrate(MSG_process_self(), MSG_get_host_by_name("Boivin"));
33   
34   XBT_INFO("Yeah, found something to do");
35   task = MSG_task_create("job", 98095000, 0, NULL);
36   MSG_task_execute(task);
37   MSG_task_destroy(task);
38   MSG_process_sleep(2);
39   XBT_INFO("Moving back home after work");
40   MSG_process_migrate(MSG_process_self(), MSG_get_host_by_name("Jacquelin"));
41   MSG_process_migrate(MSG_process_self(), MSG_get_host_by_name("Boivin"));
42   MSG_process_sleep(4);
43   xbt_mutex_acquire(mutex);
44   process_to_migrate = MSG_process_self();
45   xbt_cond_broadcast(cond);
46   xbt_mutex_release(mutex);
47   MSG_process_suspend(MSG_process_self());
48   m_host_t h = MSG_process_get_host(MSG_process_self());
49   XBT_INFO("I've been moved on this new host: %s", MSG_host_get_name(h));
50   XBT_INFO("Uh, nothing to do here. Stopping now");
51   return 0;
52 }                               /* end_of_emigrant */
53
54
55 /* This function move the emigrant on Jacquelin */
56 static int policeman(int argc, char *argv[])
57 {
58
59   xbt_mutex_acquire(mutex);
60   XBT_INFO("Wait a bit before migrating the emigrant.");
61   while (process_to_migrate == NULL) xbt_cond_wait(cond, mutex);
62   MSG_process_migrate(process_to_migrate, MSG_get_host_by_name("Jacquelin"));
63   XBT_INFO("I moved the emigrant");
64   MSG_process_resume(process_to_migrate);
65   xbt_mutex_release(mutex);
66
67   return 0;
68 }                               /* end_of_policeman */
69
70
71 /** Main function */
72 int main(int argc, char *argv[])
73 {
74   MSG_error_t res = MSG_OK;
75
76   /* Argument checking */
77   MSG_init(&argc, argv);
78   if (argc < 3) {
79     XBT_CRITICAL("Usage: %s platform_file deployment_file\n", argv[0]);
80     XBT_CRITICAL("example: %s msg_platform.xml msg_deployment_suspend.xml\n",
81               argv[0]);
82     exit(1);
83   }
84
85   /* Simulation setting */
86   MSG_create_environment(argv[1]);
87
88   /* Application deployment */
89   MSG_function_register("emigrant", emigrant);
90   MSG_function_register("policeman", policeman);
91   MSG_launch_application(argv[2]);
92
93   /* Run the simulation */
94   mutex = xbt_mutex_init();
95   cond = xbt_cond_init();
96   res = MSG_main();
97   XBT_INFO("Simulation time %g", MSG_get_clock());
98   xbt_cond_destroy(cond);
99   xbt_mutex_destroy(mutex);
100
101   if (res == MSG_OK)
102     res = MSG_clean();
103
104   if (res == MSG_OK)
105     return 0;
106   else
107     return 1;
108 }                               /* end_of_main */