Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
bebb77d33f93505a11e8f4430175b7bbecefe765
[simgrid.git] / teshsuite / msg / process / process.cpp
1 /* Copyright (c) 2010-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_test, "Messages specific for this msg example");
9
10 static int slave(int argc, char* argv[])
11 {
12   MSG_process_sleep(.5);
13   XBT_INFO("Slave started (PID:%d, PPID:%d)", MSG_process_self_PID(), MSG_process_self_PPID());
14   while (1) {
15     XBT_INFO("Plop i am %ssuspended", (MSG_process_is_suspended(MSG_process_self())) ? "" : "not ");
16     MSG_process_sleep(1);
17   }
18   XBT_INFO("I'm done. See you!");
19   return 0;
20 }
21
22 static int master(int argc, char* argv[])
23 {
24   MSG_process_sleep(1);
25   xbt_dynar_t process_list = xbt_dynar_new(sizeof(msg_process_t), nullptr);
26   MSG_host_get_process_list(MSG_host_self(), process_list);
27
28   msg_process_t process = NULL;
29   unsigned int cursor;
30   xbt_dynar_foreach (process_list, cursor, process) {
31     XBT_INFO("Process(pid=%d, ppid=%d, name=%s)", MSG_process_get_PID(process), MSG_process_get_PPID(process),
32              MSG_process_get_name(process));
33     if (MSG_process_self_PID() != MSG_process_get_PID(process))
34       MSG_process_kill(process);
35   }
36   xbt_dynar_free(&process_list);
37
38   process = MSG_process_create("slave from master", slave, NULL, MSG_host_self());
39   MSG_process_sleep(2);
40
41   XBT_INFO("Suspend Process(pid=%d)", MSG_process_get_PID(process));
42   MSG_process_suspend(process);
43
44   XBT_INFO("Process(pid=%d) is %ssuspended", MSG_process_get_PID(process),
45            (MSG_process_is_suspended(process)) ? "" : "not ");
46   MSG_process_sleep(2);
47
48   XBT_INFO("Resume Process(pid=%d)", MSG_process_get_PID(process));
49   MSG_process_resume(process);
50
51   XBT_INFO("Process(pid=%d) is %ssuspended", MSG_process_get_PID(process),
52            (MSG_process_is_suspended(process)) ? "" : "not ");
53   MSG_process_sleep(2);
54   MSG_process_kill(process);
55
56   XBT_INFO("Goodbye now!");
57   return 0;
58 }
59
60 int main(int argc, char* argv[])
61 {
62   MSG_init(&argc, argv);
63   xbt_assert(argc == 2, "Usage: %s platform_file\n\t Example: %s msg_platform.xml\n", argv[0], argv[0]);
64
65   MSG_create_environment(argv[1]);
66
67   MSG_process_create("master", master, NULL, MSG_get_host_by_name("Tremblay"));
68   MSG_process_create("slave", slave, NULL, MSG_get_host_by_name("Tremblay"));
69
70   msg_error_t res = MSG_main();
71
72   XBT_INFO("Simulation time %g", MSG_get_clock());
73
74   return res != MSG_OK;
75 }