Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
69e45db4fa459fe8e06d27a8eb1618770609a17d
[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   char* mount = bprintf("C:");
21
22   file = MSG_file_open(mount,"test.txt","rw");
23   XBT_INFO("Host '%s' open %p",MSG_host_get_name(MSG_host_self()), file);
24
25   size_t read = MSG_file_read(mount,NULL,0,0,file);
26   XBT_INFO("Host '%s' read %zu", MSG_host_get_name(MSG_host_self()), read);
27
28   size_t write = MSG_file_write(mount,NULL,0,0,file);
29   XBT_INFO("Host '%s' write %zu", MSG_host_get_name(MSG_host_self()), write);
30
31   int res = MSG_file_stat(mount,0,NULL);
32   XBT_INFO("Host '%s' stat %d",MSG_host_get_name(MSG_host_self()), res);
33
34   res = MSG_file_close(mount,file);
35   XBT_INFO("Host '%s' close %d",MSG_host_get_name(MSG_host_self()), res);
36
37   free(mount);
38   return 0;
39 }
40
41 int main(int argc, char **argv)
42 {
43     int i,res;
44   MSG_global_init(&argc, argv);
45   MSG_create_environment(argv[1]);
46   xbt_dynar_t hosts =  MSG_hosts_as_dynar();
47   MSG_function_register("host", host);
48
49   XBT_INFO("Number of host '%lu'",xbt_dynar_length(hosts));
50   for(i = 0 ; i<xbt_dynar_length(hosts); i++)
51   {
52     char* name_host = bprintf("%d",i);
53     MSG_process_create( name_host, host, NULL, xbt_dynar_get_as(hosts,i,m_host_t) );
54     free(name_host);
55   }
56   xbt_dynar_free(&hosts);
57
58   res = MSG_main();
59   XBT_INFO("Simulation time %g", MSG_get_clock());
60   MSG_clean();
61   if (res == MSG_OK)
62     return 0;
63   else
64     return 1;
65
66 }