Re: Information on use of Netsys.shm_open

Johan Mazel <[email protected]> Thu, 25 Mar 2010 01:28:53 +0100
Newsgroups gmane.comp.lang.ocaml.lib.net.devel
Message-ID <[email protected]>
2010/3/24 Gerd Stolpmann <[email protected]>

> On Mi, 2010-03-24 at 13:25 +0100, Johan Mazel wrote:
> >
> >
> > 2010/3/24 Gerd Stolpmann <[email protected]>
> >
> >         On Mi, 2010-03-24 at 01:32 +0100, Johan Mazel wrote:
> >         >
> >         >
> >         > 2010/3/23 Gerd Stolpmann <[email protected]>
> >         >
> >         >         On Di, 2010-03-23 at 19:15 +0100, Johan Mazel wrote:
> >         >         > Hi
> >         >         > I'm trying this little piece of code to open a
> >         shared memory
> >         >         and
> >         >         > modify it.
> >         >         > The code looks like this :
> >         >         >
> >         >         > open Unix
> >         >         > open Bigarray.Array1
> >         >         >
> >         >         > let _ =
> >         >         >
> >         >         >     Printf.printf("Test shared memory\n");
> >         >         >
> >         >         >     if (Netsys.have_posix_shm () = false) then
> >         >         >
> >         >         >         Printf.printf "No POSIX SHM\n"
> >         >         >     else
> >         >         >         (
> >         >         >
> >         >         >             Printf.printf "POSIX SHM OK\n";
> >         >         >
> >         >         >             let shm_name = "/shm_test" in
> >         >         >
> >         >         >             let right_list = ref [] in
> >         >         >
> >         >         >             right_list := Netsys.SHM_O_CREAT :: !
> >         right_list;
> >         >         >             right_list := Netsys.SHM_O_RDWR :: !
> >         right_list;
> >         >         >
> >         >         >             ignore(
> >         >         >                     try
> >         >         >                         (
> >         >         >                             Printf.printf
> >         "Netsys.shm_open
> >         >         call\n";
> >         >         >
> >         >         >                             let sd =
> >         Netsys.shm_open
> >         >         shm_name !
> >         >         > right_list 1 in
> >         >         >
> >         >         >                             Printf.printf
> >         "Netsys.shm_open
> >         >         ok\n";
> >         >         >
> >         >         >                             let my_big_array =
> >         >         > Bigarray.Array1.map_file sd Bigarray.char
> >         Bigarray.c_layout
> >         >         true 100
> >         >         > in
> >         >         >
> >         >         >                             let big_array_size =
> >         >         Bigarray.Array1.dim
> >         >         > my_big_array in
> >         >         >
> >         >         >                             Printf.printf "Size of
> >         BigArray:
> >         >         %d\n"
> >         >         > big_array_size;
> >         >         >
> >         >         >                             Bigarray.Array1.set
> >         my_big_array
> >         >         0 'a';
> >         >         >                         )
> >         >         >                     with
> >         >         >                     | Unix_error (_ ,
> >         invoked_function ,
> >         >         > shm_name_failure) ->
> >         >         >                             (
> >         >         >                                 Printf.printf
> >         >         >                                     "Opening
> >         failed through
> >         >         using
> >         >         > function %s on SHM %s\n"
> >         >         >
> >         invoked_function
> >         >         >
> >         shm_name_failure
> >         >         >                                 ;
> >         >         >                             )
> >         >         >                 );
> >         >         >
> >         >         >             Printf.printf "Shared memory tested
> >         \n";
> >         >         >         )
> >         >         >
> >         >         > I can create the shared memory file descriptor,
> >         but only
> >         >         once.
> >         >         > When I try to access it again (y rzlaunching my
> >         program,
> >         >         there is an
> >         >         > error on shm_open.
> >         >         >
> >         >         > I have to guess about a cause to this problem:
> >         first, I did
> >         >         not set
> >         >         > the right in the right manner and second, I do not
> >         use
> >         >         unlink.
> >         >         > The doc about the permissions is very light in
> >         this page
> >         >         >
> >         >
> >         (
> http://projects.camlcity.org/projects/dl/ocamlnet-2.2.9/doc/html-main/Netsys.html).
> I tried with with 0, 1, 777 and none of these are working.
> >         >         > For the unlink function, my understanding is that
> >         it
> >         >         destroys the
> >         >         > file. But maybe, it has the same behviour as
> >         "close" in C
> >         >         for a file
> >         >         > descriptor.
> >         >
> >         >
> >         >         Well, shm_open just calls the C function with the
> >         same name,
> >         >         so for any
> >         >         detailed documentation see there.
> >         >
> >         > Which function does unlink call ? Because there is no unlink
> >         function
> >         > in the posix C shared memory API.
> >
> >
> >         shm_unlink
> >
> >         >         Your guess is right, the error in your code is the
> >         bad
> >         >         permissions
> >         >         bitfield. 0o666 instead of 1 should be just ok to
> >         give
> >         >         everybody
> >         >         read/write access. The bitfield is interpreted as
> >         for any
> >         >         other file.
> >         >
> >         > Is there any doc available to know how to choose this code ?
> >
> >
> >         Your favourite Unix book. It is the same bitstring you can
> >         pass to the
> >         chmod command.
> >
> >         Gerd
> >
> > Hi
> > I understood that.
> > But is there any reason why I should use 0o666 to have the same
> > behaviour as 777 for chmod ?
>
> The difference between 666 and 777 is that the latter also sets the x
> bits, not only the r and w bits. x is used for allowing execution. You
> would need it for shm if you wanted to run code there (provided the
> hardware supports the control of that at all - older PC hardware does
> not, and setting x would make no difference).
>
> > I have another question linked to the map_file command from Bigarray.
> > Does this command makes mandatory the use of the rights
> > Netsys.SHM_O_RDWR in the function shm_open and forbids the use of
> > Netsys.SHM_O_RDONLY ?
>
> Yes, map_file always maps read/write, so the file descriptor needs to be
> read/write, too.
>
> > It is a result of my tests, but I would like a confirmation.
> > I see this as a way to guarantee that the function set in
> > Bigarray.genarray (and others) will be available in any case, but I am
> > not sure of my interpretation.
>
> Yes, provided there is enough memory (as usual).
>
> > An extra question would be what are the case when Netsys.SHM_O_RDONLY
> > would be useful if I cannot the use the result of shm_open in
> > map_file ? In other words are there use cases where one can use
> > shm_open with Netsys.SHM_O_RDONLY ?
>
> I'm not sure whether there is a real-life application. Generally,
> O_RDONLY would be sufficient for reading the contents of the shm object
> with Unix.read, or if you only wanted to check the size of the object.
>
> Gerd
>
> >
> > Thanks for your time.
> >
> > Johan Mazel
> >
> >
> >
> ------------------------------------------------------------------------------
> > Download Intel&#174; Parallel Studio Eval
> > Try the new software tools for yourself. Speed compiling, find bugs
> > proactively, and fine-tune applications for parallel performance.
> > See why Intel Parallel Studio got high marks during beta.
> > http://p.sf.net/sfu/intel-sw-dev
> > _______________________________________________ Ocamlnet-devel mailing
> list [email protected]
> https://lists.sourceforge.net/lists/listinfo/ocamlnet-devel
>
>
> --
> ------------------------------------------------------------
> Gerd Stolpmann, Bad Nauheimer Str.3, 64289 Darmstadt,Germany
> [email protected]          http://www.gerd-stolpmann.de
> Phone: +49-6151-153855                  Fax: +49-6151-997714
> ------------------------------------------------------------
>
>
Thanks for a lot for all the help, everything is perfectly working now.
Johan Mazel

------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev

_______________________________________________
Ocamlnet-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ocamlnet-devel