Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
e01112b5f73ef95ced20d8f096eaa9f69d3e559a
[simgrid.git] / teshsuite / msg / process.c
1 /* Copyright (c) 2010-2014. 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 "msg/msg.h"            /* Yeah! If you want to use msg, you need to include msg/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)", MSG_process_get_PID(process), MSG_process_get_PPID(process), MSG_process_get_name(process));
28     if (MSG_process_self_PID()!=MSG_process_get_PID(process))
29       MSG_process_kill(process);
30   }
31   process = MSG_process_create("slave from master", slave, NULL, MSG_host_self());
32   MSG_process_sleep(2);
33
34   XBT_INFO("Suspend Process(pid=%d)", MSG_process_get_PID(process));
35   MSG_process_suspend(process);
36
37   XBT_INFO("Process(pid=%d) is %ssuspended"
38                   ,MSG_process_get_PID(process)
39                   ,(MSG_process_is_suspended(process)) ? "" : "not ");
40   MSG_process_sleep(2);
41
42   XBT_INFO("Resume Process(pid=%d)", MSG_process_get_PID(process));
43   MSG_process_resume(process);
44
45   XBT_INFO("Process(pid=%d) is %ssuspended"
46                   ,MSG_process_get_PID(process)
47                   ,(MSG_process_is_suspended(process)) ? "" : "not ");
48   MSG_process_sleep(2);
49   MSG_process_kill(process);
50
51   XBT_INFO("Goodbye now!");
52   return 0;
53 }                               /* end_of_master */
54
55 /** Receiver function  */
56 int slave(int argc, char *argv[])
57 {
58   MSG_process_sleep(.5);
59   XBT_INFO("Slave started (PID:%d, PPID:%d)", MSG_process_self_PID(), MSG_process_self_PPID());
60   while(1){
61         XBT_INFO("Plop i am %ssuspended", (MSG_process_is_suspended(MSG_process_self())) ? "" : "not ");
62     MSG_process_sleep(1);
63     //MSG_process_suspend(MSG_process_self());
64   }
65   XBT_INFO("I'm done. See you!");
66   return 0;
67 }                               /* end_of_slave */
68
69 /** Main function */
70 int main(int argc, char *argv[])
71 {
72   msg_error_t res;
73   const char *platform_file;
74   const char *application_file;
75
76   MSG_init(&argc, argv);
77   if (argc < 2) {
78     printf("Usage: %s platform_file deployment_file\n", argv[0]);
79     printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
80     exit(1);
81   }
82   platform_file = argv[1];
83   application_file = argv[1];
84
85   /* MSG_config("workstation/model","KCCFLN05"); */
86   {                             /*  Simulation setting */
87     MSG_create_environment(platform_file);
88   }
89   {                             /*   Application deployment */
90     MSG_function_register("master", master);
91     MSG_function_register("slave", slave);
92
93     MSG_launch_application(application_file);
94   }
95   res = MSG_main();
96
97   XBT_INFO("Simulation time %g", MSG_get_clock());
98
99   if (res == MSG_OK)
100     return 0;
101   else
102     return 1;
103 }                               /* end_of_main */