Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9b2e48b02a00ba2b242aef9c598038098ead93d2
[simgrid.git] / teshsuite / msg / process-kill / process-kill.c
1 /* Copyright (c) 2007, 2009-2017. 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_kill, "Messages specific for this msg example");
9
10 static int victimA_fun(XBT_ATTRIB_UNUSED int argc, XBT_ATTRIB_UNUSED char* argv[])
11 {
12   XBT_INFO("Hello!");
13   XBT_INFO("Suspending myself");
14   MSG_process_suspend(MSG_process_self()); /* - First suspend itself */
15   XBT_INFO("OK, OK. Let's work");          /* - Then is resumed and start to execute a task */
16   MSG_task_execute(MSG_task_create("work", 1e9, 0, NULL));
17   XBT_INFO("Bye!"); /* - But will never reach the end of it */
18   return 0;
19 }
20
21 static int victimB_fun(XBT_ATTRIB_UNUSED int argc, XBT_ATTRIB_UNUSED char* argv[])
22 {
23   XBT_INFO("Terminates before being killed");
24   return 0;
25 }
26
27 static int killer(XBT_ATTRIB_UNUSED int argc, XBT_ATTRIB_UNUSED char* argv[])
28 {
29   XBT_INFO("Hello!"); /* - First start a victim process */
30   msg_process_t victimA = MSG_process_create("victim A", victimA_fun, NULL, MSG_host_by_name("Fafard"));
31   msg_process_t victimB = MSG_process_create("victim B", victimB_fun, NULL, MSG_host_by_name("Bourassa"));
32   MSG_process_sleep(10.0);
33
34   XBT_INFO("Resume victimA"); /* - Resume it from its suspended state */
35   MSG_process_resume(victimA);
36
37   XBT_INFO("Kill victimA"); /* - and then kill it */
38   MSG_process_kill(victimA);
39
40   XBT_INFO("Kill victimB, even if it's already dead"); /* that's a no-op, there is no zombies in SimGrid */
41   MSG_process_kill(victimB);
42
43   XBT_INFO("OK, goodbye now. I commit a suicide.");
44   MSG_process_kill(MSG_process_self());
45
46   XBT_INFO("This line will never get displayed: I'm already dead since the previous line.");
47   return 0;
48 }
49
50 int main(int argc, char* argv[])
51 {
52   MSG_init(&argc, argv);
53   xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s msg_platform.xml\n", argv[0], argv[0]);
54
55   MSG_create_environment(argv[1]); /* - Load the platform description */
56   /* - Create and deploy killer process, that will create the victim process  */
57   MSG_process_create("killer", killer, NULL, MSG_host_by_name("Tremblay"));
58
59   msg_error_t res = MSG_main(); /* - Run the simulation */
60
61   XBT_INFO("Simulation time %g", MSG_get_clock());
62   return res != MSG_OK;
63 }