Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
e345301c88c4ad51e43123d746caa0039360f1c4
[simgrid.git] / examples / msg / process-kill / process-kill.c
1 /* Copyright (c) 2007, 2009-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include "simgrid/msg.h"
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_process_kill, "Messages specific for this msg example");
10 /** @addtogroup MSG_examples
11  *
12  *  - <b>Killing: process-kill/process-kill.c</b>. Processes can also be killed by another if needed thanks to
13  *    the @ref MSG_process_kill function.
14  */
15
16 static int victim(int argc, char *argv[])
17 {
18   XBT_INFO("Hello!");
19   XBT_INFO("Suspending myself");
20   MSG_process_suspend(MSG_process_self()); /** - First suspend itself */
21   XBT_INFO("OK, OK. Let's work");          /** - Then is resumed and start to execute a task */
22   MSG_task_execute(MSG_task_create("work", 1e9, 0, NULL));
23   XBT_INFO("Bye!");  /** - But will never reach the end of it */
24   return 0;
25 }
26
27 static int killer(int argc, char *argv[])
28 {
29   XBT_INFO("Hello!");         /** - First start a @ref victim process */
30   msg_process_t poor_victim = MSG_process_create("victim", victim, NULL, MSG_host_by_name("Fafard"));
31   MSG_process_sleep(10.0);
32
33   XBT_INFO("Resume process"); /** - Resume it from its suspended state */
34   MSG_process_resume(poor_victim);
35
36   XBT_INFO("Kill process");   /** - and then kill it */
37   MSG_process_kill(poor_victim);
38
39   XBT_INFO("OK, goodbye now.");
40   return 0;
41 }
42
43 int main(int argc, char *argv[])
44 {
45   msg_error_t res = MSG_OK;
46
47   MSG_init(&argc, argv);
48   xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s msg_platform.xml\n", argv[0], argv[0]);
49
50   MSG_create_environment(argv[1]);   /** - Load the platform description */
51   /** - Create and deploy @ref killer process */
52   MSG_function_register("killer", killer);
53   MSG_function_register("victim", victim);
54   MSG_process_create("killer", killer, NULL, MSG_host_by_name("Tremblay"));
55
56   res = MSG_main();                 /** - Run the simulation */
57
58   XBT_INFO("Simulation time %g", MSG_get_clock());
59   return res != MSG_OK;
60 }