Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2fc714db07ce76af788d9ec53c0f50998196afc6
[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 static m_process_t process_to_migrate = NULL;
16
17 /** The guy we will move from host to host. It move alone and then is moved by policeman back  */
18 static int emigrant(int argc, char *argv[])
19 {
20   m_task_t task;
21   XBT_INFO
22       ("I'll look for a new job on another machine where the grass is greener.");
23   MSG_process_migrate(MSG_process_self(), MSG_get_host_by_name("Boivin"));
24   
25   XBT_INFO("Yeah, found something to do");
26   task = MSG_task_create("job", 98095000, 0, NULL);
27   MSG_task_execute(task);
28   MSG_task_destroy(task);
29   MSG_process_sleep(2);
30   XBT_INFO("Moving back home after work");
31   MSG_process_migrate(MSG_process_self(), MSG_get_host_by_name("Jacquelin"));
32   MSG_process_migrate(MSG_process_self(), MSG_get_host_by_name("Boivin"));
33   MSG_process_sleep(4);
34   process_to_migrate = MSG_process_self();
35   MSG_process_suspend(MSG_process_self());
36   m_host_t h = MSG_process_get_host(MSG_process_self());
37   XBT_INFO("I've been moved on this new host: %s", h->name);
38   XBT_INFO("Uh, nothing to do here. Stopping now");
39   return 0;
40 }                               /* end_of_emigrant */
41
42
43 /* This function move the emigrant on Jacquelin */
44 static int policeman(int argc, char *argv[])
45 {
46   XBT_INFO("Wait a bit before migrating the emigrant.");
47   while (process_to_migrate == NULL) MSG_process_sleep(1);
48   MSG_process_migrate(process_to_migrate, MSG_get_host_by_name("Jacquelin"));
49   XBT_INFO("I moved the emigrant");
50   MSG_process_resume(process_to_migrate);
51   return 0;
52 }                               /* end_of_policeman */
53
54
55 /** Main function */
56 int main(int argc, char *argv[])
57 {
58   MSG_error_t res = MSG_OK;
59
60   /* Argument checking */
61   MSG_global_init(&argc, argv);
62   if (argc < 3) {
63     XBT_CRITICAL("Usage: %s platform_file deployment_file\n", argv[0]);
64     XBT_CRITICAL("example: %s msg_platform.xml msg_deployment_suspend.xml\n",
65               argv[0]);
66     exit(1);
67   }
68
69   /* Simulation setting */
70   MSG_create_environment(argv[1]);
71
72   /* Application deployment */
73   MSG_function_register("emigrant", emigrant);
74   MSG_function_register("policeman", policeman);
75   MSG_launch_application(argv[2]);
76
77   /* Run the simulation */
78   res = MSG_main();
79   XBT_INFO("Simulation time %g", MSG_get_clock());
80   if (res == MSG_OK)
81     res = MSG_clean();
82
83   if (res == MSG_OK)
84     return 0;
85   else
86     return 1;
87 }                               /* end_of_main */