Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
revise I/O examples doc
[simgrid.git] / examples / msg / io-file / io-file.c
1 /* Copyright (c) 2008-2010, 2012-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 /** @addtogroup MSG_examples
8  * 
9  * - <b>File management: io-file/io-file.c</b>. This example illustrates the use of operations on file
10  *   (@ref MSG_file_open, @ref MSG_file_read, @ref MSG_file_write, or @ref MSG_file_close).
11  */
12
13 #define FILENAME1 "/home/doc/simgrid/examples/platforms/g5k.xml"
14 #define FILENAME2 "c:\\Windows\\setupact.log"
15 #define FILENAME3 "/home/doc/simgrid/examples/platforms/g5k_cabinets.xml"
16 #define FILENAME4 "/home/doc/simgrid/examples/platforms/nancy.xml"
17
18 #include "simgrid/msg.h"
19
20 XBT_LOG_NEW_DEFAULT_CATEGORY(io_file, "Messages specific for this io example");
21
22 static int host(int argc, char *argv[])
23 {
24   msg_file_t file = NULL;
25   sg_size_t read,write;
26   msg_storage_t st;
27   const char* st_name;
28
29   if(!strcmp(MSG_process_get_name(MSG_process_self()),"0")){
30     file = MSG_file_open(FILENAME1, NULL);
31     MSG_file_dump(file);
32     st_name = "Disk4";
33   } else if(!strcmp(MSG_process_get_name(MSG_process_self()),"1")) {
34     file = MSG_file_open(FILENAME2, NULL);
35     st_name = "Disk2";
36   } else if(!strcmp(MSG_process_get_name(MSG_process_self()),"2")){
37     file = MSG_file_open(FILENAME3, NULL);
38     st_name = "Disk3";
39   } else if(!strcmp(MSG_process_get_name(MSG_process_self()),"3")){
40     file = MSG_file_open(FILENAME4, NULL);
41     st_name = "Disk1";
42   }
43   else xbt_die("FILENAME NOT DEFINED %s",MSG_process_get_name(MSG_process_self()));
44
45   const char* filename = MSG_file_get_name(file);
46   XBT_INFO("\tOpen file '%s'",filename);
47   st = MSG_storage_get_by_name(st_name);
48
49   XBT_INFO("\tCapacity of the storage element '%s' is stored on: %llu / %llu",
50             filename, MSG_storage_get_used_size(st), MSG_storage_get_size(st));
51
52   /* Try to read for 10MB */
53   read = MSG_file_read(file, 10000000);
54   XBT_INFO("\tHave read %llu from '%s'",read,filename);
55
56   /* Write 100KB in file from the current position, i.e, end of file or 10MB */
57   write = MSG_file_write(file, 100000);
58   XBT_INFO("\tHave written %llu in '%s'. Size now is: %llu",write,filename, MSG_file_get_size(file));
59
60
61   XBT_INFO("\tCapacity of the storage element '%s' is stored on: %llu / %llu",
62             filename, MSG_storage_get_used_size(st), MSG_storage_get_size(st));
63
64   /* rewind to the beginning of the file */
65   XBT_INFO("\tComing back to the beginning of the stream for file '%s'", filename);
66   MSG_file_seek(file, 0, SEEK_SET);
67
68   /* Try to read 110KB */
69   read = MSG_file_read(file, 110000);
70   XBT_INFO("\tHave read %llu from '%s' (of size %llu)",read,filename, MSG_file_get_size(file));
71
72   /* rewind once again to the beginning of the file */
73   XBT_INFO("\tComing back to the beginning of the stream for file '%s'", filename);
74   MSG_file_seek(file, 0, SEEK_SET);
75
76   /* Write 110KB in file from the current position, i.e, end of file or 10MB */
77   write = MSG_file_write(file, 110000);
78   XBT_INFO("\tHave written %llu in '%s'. Size now is: %llu", write,filename, MSG_file_get_size(file));
79
80   XBT_INFO("\tCapacity of the storage element '%s' is stored on: %llu / %llu",
81             filename, MSG_storage_get_used_size(st), MSG_storage_get_size(st));
82
83   if(!strcmp(MSG_process_get_name(MSG_process_self()),"0")){
84     XBT_INFO("\tUnlink file '%s'",MSG_file_get_name(file));
85     MSG_file_unlink(file);
86   } else {
87     XBT_INFO("\tClose file '%s'",filename);
88     MSG_file_close(file);
89   }
90   return 0;
91 }
92
93 int main(int argc, char **argv)
94 {
95   MSG_init(&argc, argv);
96   MSG_create_environment(argv[1]);
97   xbt_dynar_t hosts =  MSG_hosts_as_dynar();
98   MSG_function_register("host", host);
99   unsigned long nb_hosts = xbt_dynar_length(hosts);
100   XBT_INFO("Number of host '%lu'",nb_hosts);
101   for(int i = 0 ; i<nb_hosts; i++){
102     char* name_host = bprintf("%d",i);
103     MSG_process_create( name_host, host, NULL, xbt_dynar_get_as(hosts,i,msg_host_t) );
104     free(name_host);
105   }
106   xbt_dynar_free(&hosts);
107
108   int res = MSG_main();
109   XBT_INFO("Simulation time %g", MSG_get_clock());
110   return res != MSG_OK;
111 }