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"
10 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test_exception,
11 "Messages specific for this msg example");
13 /** @addtogroup MSG_examples
15 * - <b>exception/exception.c</b>: Demonstrates how to send an exception to a remote guy
18 /** Victim. This process gets a lot of remote exceptions */
19 static int victim(int argc, char *argv[]) {
22 msg_error_t res = MSG_OK;
24 XBT_INFO("Let's work.");
26 res = MSG_task_execute(MSG_task_create("Task", 1e14, 0, NULL));
28 XBT_INFO("The MSG_task_execute caught the exception for me and returned %d)",res);
30 xbt_die("I was expecting an exception during my execution!");
33 XBT_INFO("The received exception resumed my execution. Good. Here is it: ----------------------->8----");
35 XBT_INFO("(end of the first exception) ----8<------------------------");
40 XBT_INFO("Let's get suspended.");
43 MSG_process_suspend(MSG_process_self());
45 XBT_INFO("The received exception resumed my suspension. Good. Here is it: ----------------------->8----");
47 XBT_INFO("(end of the second exception) ----8<------------------------");
52 xbt_die("I was expecting an exception during my suspension!");
55 XBT_INFO("Let's sleep for 10 seconds.");
57 res = MSG_process_sleep(10);
59 XBT_INFO("The MSG_process_sleep caught the exception for me and returned %d)",res);
61 xbt_die("I was expecting to get an exception during my nap.");
64 XBT_INFO("Got the second exception: ----------------------->8----");
66 XBT_INFO("(end of the third exception) ----8<------------------------");
70 XBT_INFO("Let's try a last time to do something on something");
71 MSG_process_sleep(10);
73 XBT_INFO("That's enough now. I quit.");
78 /** Terrorist. This process sends a bunch of exceptions to the victim. */
79 static int terrorist(int argc, char *argv[])
81 msg_process_t victim_process = NULL;
83 XBT_INFO("Let's create a victim.");
84 victim_process = MSG_process_create("victim", victim, NULL, MSG_host_self());
86 XBT_INFO("Going to sleep for 1 second");
87 if (MSG_process_sleep(1) != MSG_OK)
88 xbt_die("What's going on??? I failed to sleep!");
89 XBT_INFO("Send a first exception (host failure)");
90 SIMIX_process_throw(victim_process, host_error, 0, "First Trick: Let's pretend that the host failed");
93 XBT_INFO("Sweet, let's prepare a second trick!");
94 XBT_INFO("Going to sleep for 2 seconds");
96 if (MSG_process_sleep(2) != MSG_OK)
97 xbt_die("What's going on??? I failed to sleep!");
98 XBT_INFO("Send a second exception (host failure)");
99 SIMIX_process_throw(victim_process, host_error, 0, "Second Trick: Let's pretend again that the host failed");
101 XBT_INFO("Sweet, let's prepare a third trick!");
102 XBT_INFO("Going to sleep for 3 seconds");
104 if (MSG_process_sleep(3) != MSG_OK)
105 xbt_die("What's going on??? I failed to sleep!");
106 XBT_INFO("Send a third exception (cancellation)");
107 SIMIX_process_throw(victim_process, cancel_error, 0, "Third Trick: Let's pretend this time that someone canceled something");
109 XBT_INFO("OK, goodbye now.");
113 int main(int argc, char *argv[]) {
114 msg_error_t res = MSG_OK;
116 MSG_init(&argc, argv);
118 XBT_CRITICAL("Usage: %s platform_file deployment_file\n", argv[0]);
122 MSG_function_register("terrorist", terrorist);
123 MSG_create_environment(argv[1]);
124 MSG_launch_application(argv[2]);
127 // Simplistic platform with only one host
129 s_sg_platf_AS_cbarg_t AS = SG_PLATF_AS_INITIALIZER;
130 sg_platf_new_AS_begin(&AS);
132 s_sg_platf_host_cbarg_t host = SG_PLATF_HOST_INITIALIZER;
134 sg_platf_new_host(&host);
136 sg_platf_new_AS_end();
139 // Add one process -- super heavy just to launch an application!
140 SIMIX_init_application();
143 s_sg_platf_process_cbarg_t process = SG_PLATF_PROCESS_INITIALIZER;
145 process.argv = malloc(sizeof(char*)*2);
146 process.argv[0] = "terrorist";
147 process.argv[1] = NULL;
148 process.host = "host0";
149 process.function = "terrorist";
150 process.start_time = 0;
151 sg_platf_new_process(&process);
155 // Launch the simulation
158 XBT_INFO("Simulation time %g", MSG_get_clock());