Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix dependency issue to prevent problems with make -jx
[simgrid.git] / teshsuite / msg / process-migration / process-migration.c
1 /* Copyright (c) 2009-2017. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include "simgrid/msg.h"
7
8 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_process_migration, "Messages specific for this msg example");
9
10 msg_bar_t barrier;
11 static msg_process_t controlled_process = NULL;
12
13 /* The Emigrant process will be moved from host to host. */
14 static int emigrant(int argc, char* argv[])
15 {
16   XBT_INFO("I'll look for a new job on another machine ('Boivin') where the grass is greener.");
17   MSG_process_migrate(MSG_process_self(), MSG_host_by_name("Boivin")); /* - First, move to another host by myself */
18
19   XBT_INFO("Yeah, found something to do");
20   msg_task_t task = MSG_task_create("job", 98095000, 0, NULL); /* - Execute some work there */
21   MSG_task_execute(task);
22   MSG_task_destroy(task);
23   MSG_process_sleep(2);
24   XBT_INFO("Moving back home after work");
25   MSG_process_migrate(MSG_process_self(), MSG_host_by_name("Jacquelin")); /* - Move back to original location */
26   MSG_process_migrate(MSG_process_self(), MSG_host_by_name("Boivin"));    /* - Go back to the other host to sleep*/
27   MSG_process_sleep(4);
28   controlled_process = MSG_process_self(); /* - Get controlled at checkpoint */
29   MSG_barrier_wait(barrier);
30   MSG_process_suspend(MSG_process_self());
31   msg_host_t h = MSG_process_get_host(MSG_process_self());
32   XBT_INFO("I've been moved on this new host: %s", MSG_host_get_name(h));
33   XBT_INFO("Uh, nothing to do here. Stopping now");
34   return 0;
35 }
36
37 /* The policeman check for emigrants and move them back to 'Jacquelin' */
38 static int policeman(int argc, char* argv[])
39 {
40   XBT_INFO("Wait at the checkpoint."); /* - block on the mutex+condition */
41   MSG_barrier_wait(barrier);
42   MSG_process_migrate(controlled_process, MSG_host_by_name("Jacquelin")); /* - Move an emigrant to Jacquelin */
43   XBT_INFO("I moved the emigrant");
44   MSG_process_resume(controlled_process);
45
46   return 0;
47 }
48
49 int main(int argc, char* argv[])
50 {
51   MSG_init(&argc, argv);
52   xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s msg_platform.xml\n", argv[0], argv[0]);
53
54   MSG_create_environment(argv[1]); /* - Load the platform description */
55   /* - Create and deploy the emigrant and policeman processes */
56   MSG_process_create("emigrant", emigrant, NULL, MSG_get_host_by_name("Jacquelin"));
57   MSG_process_create("policeman", policeman, NULL, MSG_get_host_by_name("Boivin"));
58
59   barrier         = MSG_barrier_init(2);
60   msg_error_t res = MSG_main(); /* - Run the simulation */
61   XBT_INFO("Simulation time %g", MSG_get_clock());
62   MSG_barrier_destroy(barrier);
63
64   return res != MSG_OK;
65 }