Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / teshsuite / msg / pid / pid.c
1 /* Copyright (c) 2009-2010, 2013-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_test, "Messages specific for this msg example");
10
11 const char* mailbox = "mailbox";
12 #define task_comp_size 1000
13 #define task_comm_size 100000
14
15 static int my_onexit(smx_process_exit_status_t status, int *pid){
16   XBT_INFO("Process \"%d\" killed.", *pid);
17   return 0;
18 }
19
20 static int sendpid(int argc, char *argv[])
21 {
22   int pid = MSG_process_self_PID();
23   MSG_process_on_exit((int_f_pvoid_pvoid_t)my_onexit, &pid);
24   msg_task_t task = MSG_task_create("pid", task_comp_size, task_comm_size, &pid);
25   XBT_INFO("Sending pid of \"%d\".", pid);
26   MSG_task_send(task, mailbox);
27   XBT_INFO("Send of pid \"%d\" done.", pid);
28   MSG_process_suspend(MSG_process_self());
29   return 0;
30 }
31
32 static int killall(int argc, char *argv[]){
33   msg_task_t task = NULL;
34   XBT_ATTRIB_UNUSED int res;
35
36   for (int i=0; i<3;i++) {
37     res = MSG_task_receive(&(task), mailbox);
38     int pid = *(int*)MSG_task_get_data(task);
39     MSG_task_destroy(task);
40     XBT_INFO("Killing process \"%d\".", pid);
41     MSG_process_kill(MSG_process_from_PID(pid));
42     task = NULL;
43   }
44   return 0;
45 }
46
47 int main(int argc, char *argv[])
48 {
49   msg_error_t res = MSG_OK;
50
51   MSG_init(&argc, argv);
52
53   MSG_process_killall(atoi(argv[2]));
54
55   MSG_create_environment(argv[1]);
56   MSG_process_create("sendpid", sendpid, NULL, MSG_get_host_by_name("Tremblay"));
57   MSG_process_create("sendpid", sendpid, NULL, MSG_get_host_by_name("Tremblay"));
58   MSG_process_create("sendpid", sendpid, NULL, MSG_get_host_by_name("Tremblay"));
59   MSG_process_create("killall", killall, NULL, MSG_get_host_by_name("Tremblay"));
60
61   res = MSG_main();
62
63   return res != MSG_OK;
64 }