Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[smpi] add a gestion of non-contignous data
[simgrid.git] / examples / msg / io / file_unlink.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 #define FILENAME1 "./doc/simgrid/examples/platforms/g5k.xml"
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include "msg/msg.h"
23 #include "surf/surf_private.h"
24
25 int host(int argc, char *argv[]);
26
27 XBT_LOG_NEW_DEFAULT_CATEGORY(io_file,
28                              "Messages specific for this io example");
29
30 int host(int argc, char *argv[])
31 {
32   msg_file_t file = NULL;
33   s_msg_stat_t stat;
34   void *ptr = NULL;
35   char* mount = bprintf("/home");
36   double write;
37
38   // First open
39   XBT_INFO("\tOpen file '%s'",FILENAME1);
40   file = MSG_file_open(mount,FILENAME1,"rw");
41
42   // Print stat
43   MSG_file_stat(file,&stat);
44   XBT_INFO("\tFile stat %s Size %.1f",file->name,stat.size);
45   MSG_file_free_stat(&stat);
46
47   // Unlink the file
48   XBT_INFO("\tUnlink file '%s'",file->name);
49   MSG_file_unlink(file);
50
51   // Re Open the file wich is in fact created
52   XBT_INFO("\tOpen file '%s'",FILENAME1);
53   file = MSG_file_open(mount,FILENAME1,"rw");
54
55   // Print stat
56   MSG_file_stat(file,&stat);
57   XBT_INFO("\tFile stat %s Size %.1f",file->name,stat.size);
58   MSG_file_free_stat(&stat);
59
60   // Write into the new file
61   write = MSG_file_write(ptr,100000,sizeof(char*),file);  // Write for 100Ko
62   XBT_INFO("\tHaving write %.1f \ton %s",write,file->name);
63
64   // Print the stat
65   MSG_file_stat(file,&stat);
66   XBT_INFO("\tFile stat %s Size %.1f",file->name,stat.size);
67   MSG_file_free_stat(&stat);
68
69   // Close the file
70   XBT_INFO("\tClose file '%s'",file->name);
71   MSG_file_close(file);
72
73   xbt_dict_t dict_ls;
74   char* key;
75   surf_stat_t data = NULL;
76   xbt_dict_cursor_t cursor = NULL;
77
78   dict_ls = MSG_file_ls(mount,"./");
79   XBT_INFO(" ");XBT_INFO("ls ./");
80   xbt_dict_foreach(dict_ls,cursor,key,data){
81     if(data) XBT_INFO("FILE : %s",key);
82     else     XBT_INFO("DIR  : %s",key);
83   }
84   xbt_dict_free(&dict_ls);
85
86   dict_ls = MSG_file_ls(mount,"./doc/simgrid/examples/platforms/");
87   XBT_INFO(" ");XBT_INFO("ls ./doc/simgrid/examples/platforms/");
88   xbt_dict_foreach(dict_ls,cursor,key,data){
89     if(data) XBT_INFO("FILE : %s",key);
90     else     XBT_INFO("DIR  : %s",key);
91   }
92   xbt_dict_free(&dict_ls);
93
94   dict_ls = MSG_file_ls(mount,"./doc/simgrid/examples/msg/");
95   XBT_INFO(" ");XBT_INFO("ls ./doc/simgrid/examples/msg/");
96   xbt_dict_foreach(dict_ls,cursor,key,data){
97     if(data) XBT_INFO("FILE : %s",key);
98     else     XBT_INFO("DIR  : %s",key);
99   }
100   xbt_dict_free(&dict_ls);
101
102   free(mount);
103
104   return 0;
105 }
106
107 int main(int argc, char **argv)
108 {
109   int res;
110   MSG_init(&argc, argv);
111   MSG_create_environment(argv[1]);
112   xbt_dynar_t hosts =  MSG_hosts_as_dynar();
113   MSG_function_register("host", host);
114   unsigned long nb_hosts = xbt_dynar_length(hosts);
115   XBT_INFO("Number of host '%lu'",nb_hosts);
116   char* name_host = bprintf("0");
117   MSG_process_create( name_host, host, NULL, xbt_dynar_get_as(hosts,0,msg_host_t) );
118   free(name_host);
119
120   xbt_dynar_free(&hosts);
121
122   res = MSG_main();
123   XBT_INFO("Simulation time %g", MSG_get_clock());
124   MSG_clean();
125   if (res == MSG_OK)
126     return 0;
127   else
128     return 1;
129
130 }