Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
771bb6e7880f0bbe6828d58d117ab480bb7871b4
[simgrid.git] / teshsuite / msg / process-join / process-join.c
1 /* Copyright (c) 2010-2019. 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 "simgrid/msg.h"
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
10
11 static int slave(int argc, char* argv[])
12 {
13   XBT_INFO("Slave started");
14   MSG_process_sleep(3);
15   XBT_INFO("I'm done. See you!");
16   return 0;
17 }
18
19 static int master(int argc, char* argv[])
20 {
21   msg_process_t process;
22
23   XBT_INFO("Start slave");
24   process = MSG_process_create("slave from master", slave, NULL, MSG_host_self());
25   XBT_INFO("Join the slave (timeout 2)");
26   MSG_process_join(process, 2);
27
28   XBT_INFO("Start slave");
29   process = MSG_process_create("slave from master", slave, NULL, MSG_host_self());
30   XBT_INFO("Join the slave (timeout 4)");
31   MSG_process_join(process, 4);
32
33   XBT_INFO("Start slave");
34   process = MSG_process_create("slave from master", slave, NULL, MSG_host_self());
35   XBT_INFO("Join the slave (timeout 2)");
36   MSG_process_join(process, 2);
37
38   XBT_INFO("Start slave");
39   process = MSG_process_create("slave from master", slave, NULL, MSG_host_self());
40   MSG_process_ref(process); // We have to take that ref because the process will stop before we join it
41   XBT_INFO("Waiting 4");
42   MSG_process_sleep(4);
43   XBT_INFO("Join the slave after its end (timeout 1)");
44   MSG_process_join(process, 1);
45   MSG_process_unref(process); // Avoid to leak memory
46
47   XBT_INFO("Goodbye now!");
48
49   MSG_process_sleep(1);
50
51   XBT_INFO("Goodbye now!");
52   return 0;
53 }
54
55 int main(int argc, char* argv[])
56 {
57   msg_error_t res;
58
59   MSG_init(&argc, argv);
60   xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s msg_platform.xml\n", argv[0], argv[0]);
61
62   MSG_create_environment(argv[1]);
63
64   MSG_process_create("master", master, NULL, MSG_get_host_by_name("Tremblay"));
65
66   res = MSG_main();
67
68   XBT_INFO("Simulation time %g", MSG_get_clock());
69
70   return res != MSG_OK;
71 }