Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines with new year.
[simgrid.git] / teshsuite / msg / process-daemon / process-daemon.c
1 /* Copyright (c) 2017-2019. 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_daemon, "Messages specific for this msg example");
9
10 /* The worker process, working for a while before leaving */
11 static int worker_process(int argc, char* argv[])
12 {
13   XBT_INFO("Let's do some work (for 10 sec on Boivin).");
14   msg_task_t task = MSG_task_create("easy work", 980.95e6, 0, NULL);
15   MSG_task_execute(task);
16   MSG_task_destroy(task);
17
18   XBT_INFO("I'm done now. I leave even if it makes the daemon die.");
19   return 0;
20 }
21
22 /* The daemon, displaying a message every 3 seconds until all other processes stop */
23 static int daemon_process(int argc, char* argv[])
24 {
25   MSG_process_daemonize(MSG_process_self());
26
27   while (1) {
28     XBT_INFO("Hello from the infinite loop");
29     MSG_process_sleep(3.0);
30   }
31
32   XBT_INFO("I will never reach that point: daemons are killed when regular processes are done");
33   return 0;
34 }
35
36 int main(int argc, char* argv[])
37 {
38   MSG_init(&argc, argv);
39   xbt_assert(argc > 1, "Usage: %s platform_file\n\tExample: %s msg_platform.xml\n", argv[0], argv[0]);
40
41   MSG_create_environment(argv[1]);
42   xbt_dynar_t hosts = MSG_hosts_as_dynar();
43   MSG_process_create("worker", worker_process, NULL, xbt_dynar_getfirst_as(hosts, msg_host_t));
44   MSG_process_create("daemon", daemon_process, NULL, xbt_dynar_getlast_as(hosts, msg_host_t));
45   xbt_dynar_free(&hosts);
46   msg_error_t res = MSG_main();
47
48   return res != MSG_OK;
49 }