Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
improve coverage in teshsuite
[simgrid.git] / teshsuite / msg / process / process.c
1 /* Copyright (c) 2010-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 <stdio.h>
8 #include "simgrid/msg.h"            /* Yeah! If you want to use msg, you need to include simgrid/msg.h */
9 #include "xbt/sysdep.h"         /* calloc, printf */
10
11 /* Create a log channel to have nice outputs. */
12 #include "xbt/log.h"
13 #include "xbt/asserts.h"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
15                              "Messages specific for this msg example");
16
17 int master(int argc, char *argv[]);
18 int slave(int argc, char *argv[]);
19
20 /** Emitter function  */
21 int master(int argc, char *argv[])
22 {
23   xbt_swag_t process_list = MSG_host_get_process_list(MSG_host_self());
24   msg_process_t process = NULL;
25   MSG_process_sleep(1);
26   xbt_swag_foreach(process, process_list) {
27     XBT_INFO("Process(pid=%d, ppid=%d, name=%s)",
28              MSG_process_get_PID(process), MSG_process_get_PPID(process),
29              MSG_process_get_name(process));
30     if (MSG_process_self_PID() != MSG_process_get_PID(process))
31       MSG_process_kill(process);
32   }
33   process = MSG_process_create("slave from master",
34                                slave, NULL, MSG_host_self());
35   MSG_process_sleep(2);
36
37   XBT_INFO("Suspend Process(pid=%d)", MSG_process_get_PID(process));
38   MSG_process_suspend(process);
39
40   XBT_INFO("Process(pid=%d) is %ssuspended",
41            MSG_process_get_PID(process),
42            (MSG_process_is_suspended(process)) ? "" : "not ");
43   MSG_process_sleep(2);
44
45   XBT_INFO("Resume Process(pid=%d)", MSG_process_get_PID(process));
46   MSG_process_resume(process);
47
48   XBT_INFO("Process(pid=%d) is %ssuspended",
49            MSG_process_get_PID(process),
50            (MSG_process_is_suspended(process)) ? "" : "not ");
51   MSG_process_sleep(2);
52   MSG_process_kill(process);
53
54   XBT_INFO("Goodbye now!");
55   return 0;
56 }                               /* end_of_master */
57
58 /** Receiver function  */
59 int slave(int argc, char *argv[])
60 {
61   MSG_process_sleep(.5);
62   XBT_INFO("Slave started (PID:%d, PPID:%d)",
63            MSG_process_self_PID(), MSG_process_self_PPID());
64   while(1){
65     XBT_INFO("Plop i am %ssuspended",
66              (MSG_process_is_suspended(MSG_process_self())) ? "" : "not ");
67     MSG_process_sleep(1);
68   }
69   XBT_INFO("I'm done. See you!");
70   return 0;
71 }                               /* end_of_slave */
72
73 /** Main function */
74 int main(int argc, char *argv[])
75 {
76   msg_error_t res;
77   const char *platform_file;
78   const char *application_file;
79
80   MSG_init(&argc, argv);
81   xbt_assert(argc == 3, "Usage: %s platform_file deployment_file\n"
82              "\n Example: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
83  
84   platform_file = argv[1];
85   application_file = argv[2];
86
87   {                             /*  Simulation setting */
88     MSG_create_environment(platform_file);
89   }
90   {                             /*   Application deployment */
91     MSG_function_register("master", master);
92     MSG_function_register("slave", slave);
93
94     MSG_launch_application(application_file);
95   }
96   res = MSG_main();
97
98   XBT_INFO("Simulation time %g", MSG_get_clock());
99
100   return res != MSG_OK;
101 }                               /* end_of_main */