1 /* Copyright (c) 2007, 2009-2015. 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. */
7 #include "simgrid/msg.h"
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test_exception, "Messages specific for this msg example");
11 /** @addtogroup MSG_examples
13 * - <b>exception/exception.c</b>: Demonstrates how to send an exception to a remote guy
16 /** Victim. This process gets a lot of remote exceptions */
17 static int victim(int argc, char *argv[]) {
19 msg_error_t res = MSG_OK;
21 XBT_INFO("Let's work.");
23 res = MSG_task_execute(MSG_task_create("Task", 1e14, 0, NULL));
25 XBT_INFO("The MSG_task_execute caught the exception for me and returned %d)",res);
27 xbt_die("I was expecting an exception during my execution!");
30 XBT_INFO("The received exception resumed my execution. Good. Here is it: ----------------------->8----");
32 XBT_INFO("(end of the first exception) ----8<------------------------");
36 XBT_INFO("Let's get suspended.");
39 MSG_process_suspend(MSG_process_self());
41 XBT_INFO("The received exception resumed my suspension. Good. Here is it: ----------------------->8----");
43 XBT_INFO("(end of the second exception) ----8<------------------------");
48 xbt_die("I was expecting an exception during my suspension!");
51 XBT_INFO("Let's sleep for 10 seconds.");
53 res = MSG_process_sleep(10);
55 XBT_INFO("The MSG_process_sleep caught the exception for me and returned %d)",res);
57 xbt_die("I was expecting to get an exception during my nap.");
60 XBT_INFO("Got the second exception: ----------------------->8----");
62 XBT_INFO("(end of the third exception) ----8<------------------------");
66 XBT_INFO("Let's try a last time to do something on something");
67 MSG_process_sleep(10);
69 XBT_INFO("That's enough now. I quit.");
73 /** Terrorist. This process sends a bunch of exceptions to the victim. */
74 static int terrorist(int argc, char *argv[])
76 msg_process_t victim_process = NULL;
78 XBT_INFO("Let's create a victim.");
79 victim_process = MSG_process_create("victim", victim, NULL, MSG_host_self());
81 XBT_INFO("Going to sleep for 1 second");
82 if (MSG_process_sleep(1) != MSG_OK)
83 xbt_die("What's going on??? I failed to sleep!");
84 XBT_INFO("Send a first exception (host failure)");
85 SIMIX_process_throw(victim_process, host_error, 0, "First Trick: Let's pretend that the host failed");
87 XBT_INFO("Sweet, let's prepare a second trick!");
88 XBT_INFO("Going to sleep for 2 seconds");
90 if (MSG_process_sleep(2) != MSG_OK)
91 xbt_die("What's going on??? I failed to sleep!");
92 XBT_INFO("Send a second exception (host failure)");
93 SIMIX_process_throw(victim_process, host_error, 0, "Second Trick: Let's pretend again that the host failed");
95 XBT_INFO("Sweet, let's prepare a third trick!");
96 XBT_INFO("Going to sleep for 3 seconds");
98 if (MSG_process_sleep(3) != MSG_OK)
99 xbt_die("What's going on??? I failed to sleep!");
100 XBT_INFO("Send a third exception (cancellation)");
101 SIMIX_process_throw(victim_process, cancel_error, 0, "Third Trick: Let's pretend this time that someone canceled something");
103 XBT_INFO("OK, goodbye now.");
107 int main(int argc, char *argv[]) {
108 msg_error_t res = MSG_OK;
110 MSG_init(&argc, argv);
111 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
112 "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
114 MSG_function_register("terrorist", terrorist);
115 MSG_create_environment(argv[1]);
116 MSG_launch_application(argv[2]);
118 // Launch the simulation
121 XBT_INFO("Simulation time %g", MSG_get_clock());
122 return res != MSG_OK;