1 /* Copyright (c) 2007, 2009-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. */
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 XBT_INFO("Let's get suspended.");
24 MSG_process_suspend(MSG_process_self());
26 XBT_INFO("The received exception resumed my execution. Good. Here is it: ----------------------->8----");
28 XBT_INFO("(end of the first exception) ----8<------------------------");
32 msg_error_t res = MSG_OK;
34 XBT_INFO("Let's sleep for 10 seconds.");
36 res = MSG_process_sleep(10);
38 XBT_INFO("Got the second exception: ----------------------->8----");
40 XBT_INFO("(end of the second exception) ----8<------------------------");
44 if (res != MSG_TASK_CANCELED)
45 xbt_die("Sleep action not canceled through the exception. This is not a method. (retval: %d)",res);
47 xbt_die("I was expecting to get an exception during my nap.");
48 XBT_INFO("My little nap got canceled through a raw exception. Excellent.");
50 XBT_INFO("That's enough now. I quit.");
54 /** Terrorist. This process sends a bunch of exceptions to the victim. */
55 static int terrorist(int argc, char *argv[])
57 msg_process_t victim_process = NULL;
59 XBT_INFO("Let's create a victim.");
60 victim_process = MSG_process_create("victim", victim, NULL, MSG_host_self());
61 if (MSG_process_sleep(1) != MSG_OK)
62 xbt_die("What's going on??? I failed to sleep!");
63 XBT_INFO("Send a first exception (host failure)");
64 SIMIX_process_throw(victim_process, host_error, 0, "Let's pretend that the host failed");
66 if (MSG_process_sleep(3) != MSG_OK)
67 xbt_die("What's going on??? I failed to sleep!");
68 XBT_INFO("Send a second exception (cancellation)");
69 SIMIX_process_throw(victim_process, cancel_error, 0, "Let's pretend this time that someone canceled something");
71 XBT_INFO("OK, goodbye now.");
75 int main(int argc, char *argv[]) {
76 msg_error_t res = MSG_OK;
78 MSG_init(&argc, argv);
80 XBT_CRITICAL("Usage: %s platform_file deployment_file\n", argv[0]);
84 MSG_function_register("terrorist", terrorist);
85 MSG_create_environment(argv[1]);
86 MSG_launch_application(argv[2]);
89 // Simplistic platform with only one host
91 s_sg_platf_AS_cbarg_t AS = SG_PLATF_AS_INITIALIZER;
92 sg_platf_new_AS_begin(&AS);
94 s_sg_platf_host_cbarg_t host = SG_PLATF_HOST_INITIALIZER;
96 sg_platf_new_host(&host);
98 sg_platf_new_AS_end();
101 // Add one process -- super heavy just to launch an application!
102 SIMIX_init_application();
105 s_sg_platf_process_cbarg_t process = SG_PLATF_PROCESS_INITIALIZER;
107 process.argv = malloc(sizeof(char*)*2);
108 process.argv[0] = "terrorist";
109 process.argv[1] = NULL;
110 process.host = "host0";
111 process.function = "terrorist";
112 process.start_time = 0;
113 sg_platf_new_process(&process);
117 // Launch the simulation
120 XBT_INFO("Simulation time %g", MSG_get_clock());