1 /* Copyright (c) 2010-2014. The SimGrid Team.
2 * All rights reserved. */
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. */
8 #include "simgrid/msg.h" /* Yeah! If you want to use msg, you need to include simgrid/msg.h */
9 #include "xbt/sysdep.h" /* calloc, printf */
11 /* Create a log channel to have nice outputs. */
13 #include "xbt/asserts.h"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
15 "Messages specific for this msg example");
17 int master(int argc, char *argv[]);
18 int slave(int argc, char *argv[]);
20 /** Emitter function */
21 int master(int argc, char *argv[])
23 xbt_swag_t process_list = MSG_host_get_process_list(MSG_host_self());
24 msg_process_t process = NULL;
26 xbt_swag_foreach(process, process_list) {
27 XBT_INFO("Process(pid=%d, ppid=%d, name=%s)",
28 MSG_process_get_PID(process), MSG_process_get_PPID(process),
29 MSG_process_get_name(process));
30 if (MSG_process_self_PID() != MSG_process_get_PID(process))
31 MSG_process_kill(process);
33 process = MSG_process_create("slave from master",
34 slave, NULL, MSG_host_self());
37 XBT_INFO("Suspend Process(pid=%d)", MSG_process_get_PID(process));
38 MSG_process_suspend(process);
40 XBT_INFO("Process(pid=%d) is %ssuspended",
41 MSG_process_get_PID(process),
42 (MSG_process_is_suspended(process)) ? "" : "not ");
45 XBT_INFO("Resume Process(pid=%d)", MSG_process_get_PID(process));
46 MSG_process_resume(process);
48 XBT_INFO("Process(pid=%d) is %ssuspended",
49 MSG_process_get_PID(process),
50 (MSG_process_is_suspended(process)) ? "" : "not ");
52 MSG_process_kill(process);
54 XBT_INFO("Goodbye now!");
58 /** Receiver function */
59 int slave(int argc, char *argv[])
61 MSG_process_sleep(.5);
62 XBT_INFO("Slave started (PID:%d, PPID:%d)",
63 MSG_process_self_PID(), MSG_process_self_PPID());
65 XBT_INFO("Plop i am %ssuspended",
66 (MSG_process_is_suspended(MSG_process_self())) ? "" : "not ");
69 XBT_INFO("I'm done. See you!");
74 int main(int argc, char *argv[])
77 const char *platform_file;
78 const char *application_file;
80 MSG_init(&argc, argv);
82 printf("Usage: %s platform_file deployment_file\n", argv[0]);
83 printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
86 platform_file = argv[1];
87 application_file = argv[2];
89 { /* Simulation setting */
90 MSG_create_environment(platform_file);
92 { /* Application deployment */
93 MSG_function_register("master", master);
94 MSG_function_register("slave", slave);
96 MSG_launch_application(application_file);
100 XBT_INFO("Simulation time %g", MSG_get_clock());