Logo AND Algorithmique Numérique Distribuée

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