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 /** @addtogroup MSG_examples
8  * 
9  * @subsection MSG_ex_resources Other resource kinds
10  * 
11  * This section contains some sparse examples of how to use the other
12  * kind of resources, such as disk or GPU. These resources are quite
13  * experimental for now, but here we go anyway.
14  * 
15  * - <b>io/file.c</b> Example with the disk resource
16  */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include "msg/msg.h"
21 #include "surf/surf_private.h"
22
23 int host(int argc, char *argv[]);
24
25 XBT_LOG_NEW_DEFAULT_CATEGORY(io_file,
26                              "Messages specific for this io example");
27
28 int host(int argc, char *argv[])
29 {
30   m_file_t file;
31   char* mount = bprintf("C:");
32
33   file = MSG_file_open(mount,"test.txt","rw");
34   XBT_INFO("Host '%s' open %p",MSG_host_get_name(MSG_host_self()), file);
35
36   size_t read = MSG_file_read(mount,NULL,0,0,file);
37   XBT_INFO("Host '%s' read %zu", MSG_host_get_name(MSG_host_self()), read);
38
39   size_t write = MSG_file_write(mount,NULL,0,0,file);
40   XBT_INFO("Host '%s' write %zu", MSG_host_get_name(MSG_host_self()), write);
41
42   int res = MSG_file_stat(mount,0,NULL);
43   XBT_INFO("Host '%s' stat %d",MSG_host_get_name(MSG_host_self()), res);
44
45   res = MSG_file_close(mount,file);
46   XBT_INFO("Host '%s' close %d",MSG_host_get_name(MSG_host_self()), res);
47
48   free(mount);
49   return 0;
50 }
51
52 int main(int argc, char **argv)
53 {
54     int i,res;
55   MSG_global_init(&argc, argv);
56   MSG_create_environment(argv[1]);
57   xbt_dynar_t hosts =  MSG_hosts_as_dynar();
58   MSG_function_register("host", host);
59
60   XBT_INFO("Number of host '%lu'",xbt_dynar_length(hosts));
61   for(i = 0 ; i<xbt_dynar_length(hosts); i++)
62   {
63     char* name_host = bprintf("%d",i);
64     MSG_process_create( name_host, host, NULL, xbt_dynar_get_as(hosts,i,m_host_t) );
65     free(name_host);
66   }
67   xbt_dynar_free(&hosts);
68
69   res = MSG_main();
70   XBT_INFO("Simulation time %g", MSG_get_clock());
71   MSG_clean();
72   if (res == MSG_OK)
73     return 0;
74   else
75     return 1;
76
77 }