Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[simgrid.git] / examples / msg / io / file.c
1 /* Copyright (c) 2008, 2009, 2010. 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 <stdlib.h>
9 #include "msg/msg.h"
10 #include "surf/surf_private.h"
11
12 int host(int argc, char *argv[]);
13
14 XBT_LOG_NEW_DEFAULT_CATEGORY(io_file,
15                              "Messages specific for this io example");
16
17 int host(int argc, char *argv[])
18 {
19   m_file_t file;
20   file = MSG_file_open("test.txt","rw");
21   XBT_INFO("Host '%s' open %p",MSG_host_get_name(MSG_host_self()), file);
22
23   size_t read = MSG_file_read(NULL,0,0,file);
24   XBT_INFO("Host '%s' read %zu", MSG_host_get_name(MSG_host_self()), read);
25
26   size_t write = MSG_file_write(NULL,0,0,file);
27   XBT_INFO("Host '%s' write %zu", MSG_host_get_name(MSG_host_self()), write);
28
29   int res = MSG_file_stat(0,NULL);
30   XBT_INFO("Host '%s' stat %d",MSG_host_get_name(MSG_host_self()), res);
31
32   res = MSG_file_close(file);
33   XBT_INFO("Host '%s' close %d",MSG_host_get_name(MSG_host_self()), res);
34   return 0;
35 }
36
37 int main(int argc, char **argv)
38 {
39     int i,res;
40   MSG_global_init(&argc, argv);
41   MSG_create_environment(argv[1]);
42   m_host_t *host_table =  MSG_get_host_table();
43   int number_of_hosts = MSG_get_host_number();
44   MSG_function_register("host", host);
45
46   XBT_INFO("Number of host '%d'",number_of_hosts);
47   for(i = 0 ; i<number_of_hosts; i++)
48   {
49     char* name_host = bprintf("%d",i);
50     MSG_process_create( name_host, host, NULL, host_table[i] );
51     free(name_host);
52   }
53   xbt_free(host_table);
54
55   res = MSG_main();
56   XBT_INFO("Simulation time %g", MSG_get_clock());
57   MSG_clean();
58   if (res == MSG_OK)
59     return 0;
60   else
61     return 1;
62
63 }